core2 0.4.0 - mirror of yanked crates.io release for workspace patch
CI / test-nightly (macos-latest, nightly) (push) Has been cancelled
CI / test-nightly (ubuntu-latest, nightly) (push) Has been cancelled
CI / test-nightly (windows-latest, nightly) (push) Has been cancelled
CI / test-stable (macos-latest, stable) (push) Has been cancelled
CI / test-stable (ubuntu-latest, stable) (push) Has been cancelled
CI / test-stable (windows-latest, stable) (push) Has been cancelled
CI / test-msrv (macos-latest, 1.47) (push) Has been cancelled
CI / test-msrv (ubuntu-latest, 1.47) (push) Has been cancelled
CI / test-msrv (windows-latest, 1.47) (push) Has been cancelled

This commit is contained in:
PezkuwiChain
2026-04-25 05:45:12 +03:00
commit d1abdf1838
18 changed files with 4885 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
{
"git": {
"sha1": "545e84bcb0f235b12e21351e0c69767958efe2a7"
},
"path_in_vcs": ""
}
+91
View File
@@ -0,0 +1,91 @@
name: CI
on: [push, pull_request]
jobs:
test-nightly:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
rust: [nightly]
steps:
- uses: actions/checkout@v2
- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust }}
override: true
- name: Run tests (no_std)
uses: actions-rs/cargo@v1
with:
command: test
args: --tests --no-default-features --features nightly
- name: Run tests (std)
uses: actions-rs/cargo@v1
with:
command: test
args: --tests --no-default-features --features std,nightly
- name: Run tests (alloc)
uses: actions-rs/cargo@v1
with:
command: test
args: --tests --no-default-features --features alloc,nightly
test-stable:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
rust: [stable]
steps:
- uses: actions/checkout@v2
- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust }}
override: true
- name: Run tests (no_std)
uses: actions-rs/cargo@v1
with:
command: test
args: --no-default-features
- name: Run tests (std)
uses: actions-rs/cargo@v1
with:
command: test
args: --no-default-features --features std
- name: Run tests (alloc)
uses: actions-rs/cargo@v1
with:
command: test
args: --no-default-features --features alloc
test-msrv:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
rust: [1.47]
steps:
- uses: actions/checkout@v2
- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust }}
override: true
- name: Run tests (no_std)
uses: actions-rs/cargo@v1
with:
command: test
args: --no-default-features
- name: Run tests (std)
uses: actions-rs/cargo@v1
with:
command: test
args: --no-default-features --features std
- name: Run tests (alloc)
uses: actions-rs/cargo@v1
with:
command: test
args: --no-default-features --features alloc
+9
View File
@@ -0,0 +1,9 @@
/target
#Added by cargo
#
#already existing elements were commented out
#/target
Cargo.lock
+31
View File
@@ -0,0 +1,31 @@
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies.
#
# If you are reading this file be aware that the original Cargo.toml
# will likely look very different (and much more reasonable).
# See Cargo.toml.orig for the original contents.
[package]
edition = "2018"
name = "core2"
version = "0.4.0"
authors = ["Brendan Molloy <brendan@bbqsrc.net>"]
description = "The bare essentials of std::io for use in no_std. Alloc support is optional."
categories = ["no-std"]
license = "Apache-2.0 OR MIT"
repository = "https://github.com/bbqsrc/core2"
[package.metadata.docs.rs]
features = ["nightly"]
[dependencies.memchr]
version = "2"
default-features = false
[features]
alloc = []
default = ["std"]
nightly = []
std = ["alloc"]
+21
View File
@@ -0,0 +1,21 @@
[package]
name = "core2"
version = "0.4.0"
authors = ["Brendan Molloy <brendan@bbqsrc.net>"]
description = "The bare essentials of std::io for use in no_std. Alloc support is optional."
license = "Apache-2.0 OR MIT"
edition = "2018"
repository = "https://github.com/bbqsrc/core2"
categories = ["no-std"]
[dependencies]
memchr = { version = "2", default-features = false }
[features]
default = ["std"]
std = ["alloc"]
alloc = []
nightly = []
[package.metadata.docs.rs]
features = ["nightly"]
+202
View File
@@ -0,0 +1,202 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright {yyyy} {name of copyright owner}
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+19
View File
@@ -0,0 +1,19 @@
Copyright (c) 2020-2021 Brendan Molloy <brendan@bbqsrc.net>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+66
View File
@@ -0,0 +1,66 @@
# core2
[![Actions Status](https://github.com/bbqsrc/core2/workflows/CI/badge.svg)](https://github.com/bbqsrc/core2/actions)
[![Documentation](https://docs.rs/core2/badge.svg)](https://docs.rs/core2)
![Minimum Supported Rust Version (MSRV)](https://img.shields.io/badge/rust-v1.47.0+-blue)
Ever wanted a `Cursor` or the `Error` trait in `no_std`? Well now you can have it. A 'fork' of Rust's `std` modules for `no_std` environments, with the added benefit of optionally taking advantage of `alloc`.
The goal of this crate is to provide a stable interface for building I/O and error trait functionality in
`no_std` environments. The current code corresponds to the most recent stable API of Rust 1.47.0.
It is also a goal to achieve a true alloc-less experience, with opt-in alloc support.
This crate works on `stable` with some limitations in functionality, and `nightly` without limitations by adding
the relevant feature flag.
This crate is `std` by default -- use no default features to get `no_std` mode.
## Usage
```toml
[dependencies]
core2 = "0.3"
```
Add the crate, use the things you would usually want from `std::io`, but instead from `core2::io`, and
use `core2::error::Error` in place of `std::error::Error`.
### Features
- **std**: enables `std` pass-throughs for the polyfilled types, but allows accessing the new types
- **alloc**: enable aspects of the `Read` and `Write` traits that require `alloc` support (WIP)
- **nightly**: enables **nightly**-only features, such as `BufReader` and `BufWriter` with const generic buffers.
### Differences to `std::io`
- No `std::io::Error`, so we have our own copy without any `Os` error functions
- `IoSlice` and the `*_vectored` family of functions are not implemented.
- `BufReader` and `BufWriter` have a different signature, as they now use a const generic bounded array for the internal buffer. (Requires **nightly** feature)
Other than items perhaps being entirely missing or certain functions unavailable on some traits, no function signatures have been changed.
### Limitations
- Using the buffer types currently requires **nightly** due to the use of const generics.
- Using `copy` or the buffer types with `std` support currently requires **nightly** due to the `initializer` API.
## Where is it used?
All of the below are works in progress, but should help with demonstrating how to use this crate.
- [thiserror_core2](https://github.com/bbqsrc/thiserror-core2): fork of `thiserror` using the `core2::error::Error` trait.
## License
Licensed under either of
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your option.
---
Almost all of the code in this repository is a copy of the [Rust language codebase](https://github.com/rust-lang/rust) with minor modifications.
For attributions, see https://thanks.rust-lang.org/.
+473
View File
@@ -0,0 +1,473 @@
//! Traits for working with Errors.
// A note about crates and the facade:
//
// Originally, the `Error` trait was defined in libcore, and the impls
// were scattered about. However, coherence objected to this
// arrangement, because to create the blanket impls for `Box` required
// knowing that `&str: !Error`, and we have no means to deal with that
// sort of conflict just now. Therefore, for the time being, we have
// moved the `Error` trait into libstd. As we evolve a sol'n to the
// coherence challenge (e.g., specialization, neg impls, etc) we can
// reconsider what crate these items belong in.
#[allow(deprecated)]
use core::alloc::LayoutErr;
use core::any::TypeId;
use core::fmt::{Debug, Display};
#[cfg(feature = "alloc")]
use alloc::{string::String, borrow::Cow, boxed::Box};
/// `Error` is a trait representing the basic expectations for error values,
/// i.e., values of type `E` in [`Result<T, E>`]. Errors must describe
/// themselves through the [`Display`] and [`Debug`] traits, and may provide
/// cause chain information:
///
/// [`Error::source()`] is generally used when errors cross
/// "abstraction boundaries". If one module must report an error that is caused
/// by an error from a lower-level module, it can allow accessing that error
/// via [`Error::source()`]. This makes it possible for the high-level
/// module to provide its own errors while also revealing some of the
/// implementation for debugging via `source` chains.
///
/// [`Result<T, E>`]: Result
pub trait Error: Debug + Display {
/// The lower-level source of this error, if any.
///
/// # Examples
///
/// ```
/// use std::error::Error;
/// use std::fmt;
///
/// #[derive(Debug)]
/// struct SuperError {
/// side: SuperErrorSideKick,
/// }
///
/// impl fmt::Display for SuperError {
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// write!(f, "SuperError is here!")
/// }
/// }
///
/// impl Error for SuperError {
/// fn source(&self) -> Option<&(dyn Error + 'static)> {
/// Some(&self.side)
/// }
/// }
///
/// #[derive(Debug)]
/// struct SuperErrorSideKick;
///
/// impl fmt::Display for SuperErrorSideKick {
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// write!(f, "SuperErrorSideKick is here!")
/// }
/// }
///
/// impl Error for SuperErrorSideKick {}
///
/// fn get_super_error() -> Result<(), SuperError> {
/// Err(SuperError { side: SuperErrorSideKick })
/// }
///
/// fn main() {
/// match get_super_error() {
/// Err(e) => {
/// println!("Error: {}", e);
/// println!("Caused by: {}", e.source().unwrap());
/// }
/// _ => println!("No error"),
/// }
/// }
/// ```
fn source(&self) -> Option<&(dyn Error + 'static)> {
None
}
/// Gets the `TypeId` of `self`.
#[doc(hidden)]
fn type_id(&self, _: private::Internal) -> TypeId
where
Self: 'static,
{
TypeId::of::<Self>()
}
}
mod private {
// This is a hack to prevent `type_id` from being overridden by `Error`
// implementations, since that can enable unsound downcasting.
#[derive(Debug)]
pub struct Internal;
}
#[cfg(feature = "alloc")]
impl<'a, E: Error + 'a> From<E> for Box<dyn Error + 'a> {
/// Converts a type of [`Error`] into a box of dyn [`Error`].
///
/// # Examples
///
/// ```
/// use std::error::Error;
/// use std::fmt;
/// use std::mem;
///
/// #[derive(Debug)]
/// struct AnError;
///
/// impl fmt::Display for AnError {
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// write!(f , "An error")
/// }
/// }
///
/// impl Error for AnError {}
///
/// let an_error = AnError;
/// assert!(0 == mem::size_of_val(&an_error));
/// let a_boxed_error = Box::<dyn Error>::from(an_error);
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
/// ```
fn from(err: E) -> Box<dyn Error + 'a> {
Box::new(err)
}
}
#[cfg(feature = "alloc")]
impl<'a, E: Error + Send + Sync + 'a> From<E> for Box<dyn Error + Send + Sync + 'a> {
/// Converts a type of [`Error`] + [`Send`] + [`Sync`] into a box of
/// dyn [`Error`] + [`Send`] + [`Sync`].
///
/// # Examples
///
/// ```
/// use std::error::Error;
/// use std::fmt;
/// use std::mem;
///
/// #[derive(Debug)]
/// struct AnError;
///
/// impl fmt::Display for AnError {
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// write!(f , "An error")
/// }
/// }
///
/// impl Error for AnError {}
///
/// unsafe impl Send for AnError {}
///
/// unsafe impl Sync for AnError {}
///
/// let an_error = AnError;
/// assert!(0 == mem::size_of_val(&an_error));
/// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(an_error);
/// assert!(
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
/// ```
fn from(err: E) -> Box<dyn Error + Send + Sync + 'a> {
Box::new(err)
}
}
#[cfg(feature = "alloc")]
impl From<String> for Box<dyn Error + Send + Sync> {
/// Converts a [`String`] into a box of dyn [`Error`] + [`Send`] + [`Sync`].
///
/// # Examples
///
/// ```
/// use std::error::Error;
/// use std::mem;
///
/// let a_string_error = "a string error".to_string();
/// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(a_string_error);
/// assert!(
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
/// ```
#[inline]
fn from(err: String) -> Box<dyn Error + Send + Sync> {
struct StringError(String);
impl Error for StringError {}
impl Display for StringError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
Display::fmt(&self.0, f)
}
}
// Purposefully skip printing "StringError(..)"
impl Debug for StringError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
Debug::fmt(&self.0, f)
}
}
Box::new(StringError(err))
}
}
#[cfg(feature = "alloc")]
impl From<String> for Box<dyn Error> {
/// Converts a [`String`] into a box of dyn [`Error`].
///
/// # Examples
///
/// ```
/// use std::error::Error;
/// use std::mem;
///
/// let a_string_error = "a string error".to_string();
/// let a_boxed_error = Box::<dyn Error>::from(a_string_error);
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
/// ```
fn from(str_err: String) -> Box<dyn Error> {
let err1: Box<dyn Error + Send + Sync> = From::from(str_err);
let err2: Box<dyn Error> = err1;
err2
}
}
#[cfg(feature = "alloc")]
impl<'a> From<&str> for Box<dyn Error + Send + Sync + 'a> {
/// Converts a [`str`] into a box of dyn [`Error`] + [`Send`] + [`Sync`].
///
/// [`str`]: prim@str
///
/// # Examples
///
/// ```
/// use std::error::Error;
/// use std::mem;
///
/// let a_str_error = "a str error";
/// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(a_str_error);
/// assert!(
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
/// ```
#[inline]
fn from(err: &str) -> Box<dyn Error + Send + Sync + 'a> {
From::from(String::from(err))
}
}
#[cfg(feature = "alloc")]
impl From<&str> for Box<dyn Error> {
/// Converts a [`str`] into a box of dyn [`Error`].
///
/// [`str`]: prim@str
///
/// # Examples
///
/// ```
/// use std::error::Error;
/// use std::mem;
///
/// let a_str_error = "a str error";
/// let a_boxed_error = Box::<dyn Error>::from(a_str_error);
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
/// ```
fn from(err: &str) -> Box<dyn Error> {
From::from(String::from(err))
}
}
#[cfg(feature = "alloc")]
impl<'a, 'b> From<Cow<'b, str>> for Box<dyn Error + Send + Sync + 'a> {
/// Converts a [`Cow`] into a box of dyn [`Error`] + [`Send`] + [`Sync`].
///
/// # Examples
///
/// ```
/// use std::error::Error;
/// use std::mem;
/// use std::borrow::Cow;
///
/// let a_cow_str_error = Cow::from("a str error");
/// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(a_cow_str_error);
/// assert!(
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
/// ```
fn from(err: Cow<'b, str>) -> Box<dyn Error + Send + Sync + 'a> {
From::from(String::from(err))
}
}
#[cfg(feature = "alloc")]
impl<'a> From<Cow<'a, str>> for Box<dyn Error> {
/// Converts a [`Cow`] into a box of dyn [`Error`].
///
/// # Examples
///
/// ```
/// use std::error::Error;
/// use std::mem;
/// use std::borrow::Cow;
///
/// let a_cow_str_error = Cow::from("a str error");
/// let a_boxed_error = Box::<dyn Error>::from(a_cow_str_error);
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
/// ```
fn from(err: Cow<'a, str>) -> Box<dyn Error> {
From::from(String::from(err))
}
}
// #[unstable(feature = "never_type", issue = "35121")]
#[cfg(feature = "nightly")]
impl Error for ! {}
#[allow(deprecated)]
impl Error for LayoutErr {}
impl Error for core::str::ParseBoolError {}
impl Error for core::str::Utf8Error {}
impl Error for core::num::ParseIntError {}
impl Error for core::num::TryFromIntError {}
impl Error for core::array::TryFromSliceError {}
impl Error for core::num::ParseFloatError {}
#[cfg(feature = "alloc")]
impl Error for alloc::string::FromUtf8Error {}
#[cfg(feature = "alloc")]
impl Error for alloc::string::FromUtf16Error {}
#[cfg(feature = "alloc")]
impl<T: Error> Error for Box<T> {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Error::source(&**self)
}
}
// Copied from `any.rs`.
impl dyn Error + 'static {
/// Returns `true` if the boxed type is the same as `T`
#[inline]
pub fn is<T: Error + 'static>(&self) -> bool {
// Get `TypeId` of the type this function is instantiated with.
let t = TypeId::of::<T>();
// Get `TypeId` of the type in the trait object.
let boxed = self.type_id(private::Internal);
// Compare both `TypeId`s on equality.
t == boxed
}
/// Returns some reference to the boxed value if it is of type `T`, or
/// `None` if it isn't.
#[inline]
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
if self.is::<T>() {
unsafe { Some(&*(self as *const dyn Error as *const T)) }
} else {
None
}
}
/// Returns some mutable reference to the boxed value if it is of type `T`, or
/// `None` if it isn't.
#[inline]
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
if self.is::<T>() {
unsafe { Some(&mut *(self as *mut dyn Error as *mut T)) }
} else {
None
}
}
}
impl dyn Error + 'static + Send {
/// Forwards to the method defined on the type `dyn Error`.
#[inline]
pub fn is<T: Error + 'static>(&self) -> bool {
<dyn Error + 'static>::is::<T>(self)
}
/// Forwards to the method defined on the type `dyn Error`.
#[inline]
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
<dyn Error + 'static>::downcast_ref::<T>(self)
}
/// Forwards to the method defined on the type `dyn Error`.
#[inline]
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
<dyn Error + 'static>::downcast_mut::<T>(self)
}
}
impl dyn Error + 'static + Send + Sync {
/// Forwards to the method defined on the type `dyn Error`.
#[inline]
pub fn is<T: Error + 'static>(&self) -> bool {
<dyn Error + 'static>::is::<T>(self)
}
/// Forwards to the method defined on the type `dyn Error`.
#[inline]
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
<dyn Error + 'static>::downcast_ref::<T>(self)
}
/// Forwards to the method defined on the type `dyn Error`.
#[inline]
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
<dyn Error + 'static>::downcast_mut::<T>(self)
}
}
#[cfg(feature = "alloc")]
impl dyn Error {
#[inline]
/// Attempts to downcast the box to a concrete type.
pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<dyn Error>> {
if self.is::<T>() {
unsafe {
let raw: *mut dyn Error = Box::into_raw(self);
Ok(Box::from_raw(raw as *mut T))
}
} else {
Err(self)
}
}
}
#[cfg(feature = "alloc")]
impl dyn Error + Send {
#[inline]
/// Attempts to downcast the box to a concrete type.
pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<dyn Error + Send>> {
let err: Box<dyn Error> = self;
<dyn Error>::downcast(err).map_err(|s| unsafe {
// Reapply the `Send` marker.
core::mem::transmute::<Box<dyn Error>, Box<dyn Error + Send>>(s)
})
}
}
#[cfg(feature = "alloc")]
impl dyn Error + Send + Sync {
#[inline]
/// Attempts to downcast the box to a concrete type.
pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<Self>> {
let err: Box<dyn Error> = self;
<dyn Error>::downcast(err).map_err(|s| unsafe {
// Reapply the `Send + Sync` marker.
core::mem::transmute::<Box<dyn Error>, Box<dyn Error + Send + Sync>>(s)
})
}
}
+1111
View File
File diff suppressed because it is too large Load Diff
+272
View File
@@ -0,0 +1,272 @@
use super::{BufRead, Error, ErrorKind, Read, Result, Seek, SeekFrom, Write};
use core::cmp;
/// A `Cursor` wraps an in-memory buffer and provides it with a
/// [`Seek`] implementation.
///
/// `Cursor`s are used with in-memory buffers, anything implementing
/// [`AsRef`]`<[u8]>`, to allow them to implement [`Read`] and/or [`Write`],
/// allowing these buffers to be used anywhere you might use a reader or writer
/// that does actual I/O.
///
/// The standard library implements some I/O traits on various types which
/// are commonly used as a buffer, like `Cursor<`[`Vec`]`<u8>>` and
/// `Cursor<`[`&[u8]`][bytes]`>`.
///
/// # Examples
///
/// We may want to write bytes to a [`File`] in our production
/// code, but use an in-memory buffer in our tests. We can do this with
/// `Cursor`:
///
/// [bytes]: crate::slice
/// [`File`]: crate::fs::File
///
/// ```
/// use std::io::prelude::*;
/// use core2::io::{self, Seek, SeekFrom, Write};
/// use std::fs::File;
///
/// // a library function we've written
/// fn write_ten_bytes_at_end<W: Write + Seek>(writer: &mut W) -> io::Result<()> {
/// writer.seek(SeekFrom::End(-10))?;
///
/// for i in 0..10 {
/// writer.write(&[i])?;
/// }
///
/// // all went well
/// Ok(())
/// }
///
/// # #[cfg(feature = "std")]
/// # fn foo() -> io::Result<()> {
/// // Here's some code that uses this library function.
/// //
/// // We might want to use a BufReader here for efficiency, but let's
/// // keep this example focused.
/// let mut file = File::create("foo.txt").map_err(|e| io::Error::from(e))?;
///
/// write_ten_bytes_at_end(&mut file)?;
/// # Ok(())
/// # }
///
/// // now let's write a test
/// #[test]
/// fn test_writes_bytes() {
/// // setting up a real File is much slower than an in-memory buffer,
/// // let's use a cursor instead
/// use core2::io::Cursor;
/// let mut buff = Cursor::new(vec![0; 15]);
///
/// write_ten_bytes_at_end(&mut buff).unwrap();
///
/// assert_eq!(&buff.get_ref()[5..15], &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
/// }
/// ```
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Cursor<T> {
inner: T,
pos: u64,
}
impl<T> Cursor<T> {
/// Creates a new cursor wrapping the provided underlying in-memory buffer.
///
/// Cursor initial position is `0` even if underlying buffer (e.g., [`Vec`])
/// is not empty. So writing to cursor starts with overwriting [`Vec`]
/// content, not with appending to it.
///
/// # Examples
///
/// ```
/// use core2::io::Cursor;
///
/// let buff = Cursor::new(Vec::new());
/// # fn force_inference(_: &Cursor<Vec<u8>>) {}
/// # force_inference(&buff);
/// ```
pub fn new(inner: T) -> Cursor<T> {
Cursor { pos: 0, inner }
}
/// Consumes this cursor, returning the underlying value.
///
/// # Examples
///
/// ```
/// use core2::io::Cursor;
///
/// let buff = Cursor::new(Vec::new());
/// # fn force_inference(_: &Cursor<Vec<u8>>) {}
/// # force_inference(&buff);
///
/// let vec = buff.into_inner();
/// ```
pub fn into_inner(self) -> T {
self.inner
}
/// Gets a reference to the underlying value in this cursor.
///
/// # Examples
///
/// ```
/// use core2::io::Cursor;
///
/// let buff = Cursor::new(Vec::new());
/// # fn force_inference(_: &Cursor<Vec<u8>>) {}
/// # force_inference(&buff);
///
/// let reference = buff.get_ref();
/// ```
pub fn get_ref(&self) -> &T {
&self.inner
}
/// Gets a mutable reference to the underlying value in this cursor.
///
/// Care should be taken to avoid modifying the internal I/O state of the
/// underlying value as it may corrupt this cursor's position.
///
/// # Examples
///
/// ```
/// use core2::io::Cursor;
///
/// let mut buff = Cursor::new(Vec::new());
/// # fn force_inference(_: &Cursor<Vec<u8>>) {}
/// # force_inference(&buff);
///
/// let reference = buff.get_mut();
/// ```
pub fn get_mut(&mut self) -> &mut T {
&mut self.inner
}
/// Returns the current position of this cursor.
///
/// # Examples
///
/// ```
/// use core2::io::{Cursor, Seek, SeekFrom};
/// use std::io::prelude::*;
///
/// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
///
/// assert_eq!(buff.position(), 0);
///
/// buff.seek(SeekFrom::Current(2)).unwrap();
/// assert_eq!(buff.position(), 2);
///
/// buff.seek(SeekFrom::Current(-1)).unwrap();
/// assert_eq!(buff.position(), 1);
/// ```
pub fn position(&self) -> u64 {
self.pos
}
/// Sets the position of this cursor.
///
/// # Examples
///
/// ```
/// use core2::io::Cursor;
///
/// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
///
/// assert_eq!(buff.position(), 0);
///
/// buff.set_position(2);
/// assert_eq!(buff.position(), 2);
///
/// buff.set_position(4);
/// assert_eq!(buff.position(), 4);
/// ```
pub fn set_position(&mut self, pos: u64) {
self.pos = pos;
}
}
impl<T> Seek for Cursor<T>
where
T: AsRef<[u8]>,
{
fn seek(&mut self, style: SeekFrom) -> Result<u64> {
let (base_pos, offset) = match style {
SeekFrom::Start(n) => {
self.pos = n;
return Ok(n);
}
SeekFrom::End(n) => (self.inner.as_ref().len() as u64, n),
SeekFrom::Current(n) => (self.pos, n),
};
let new_pos = if offset >= 0 {
base_pos.checked_add(offset as u64)
} else {
base_pos.checked_sub((offset.wrapping_neg()) as u64)
};
match new_pos {
Some(n) => {
self.pos = n;
Ok(self.pos)
}
None => Err(Error::new(
ErrorKind::InvalidInput,
"invalid seek to a negative or overflowing position",
)),
}
}
}
impl<T> Read for Cursor<T>
where
T: AsRef<[u8]>,
{
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
let n = Read::read(&mut self.fill_buf()?, buf)?;
self.pos += n as u64;
Ok(n)
}
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
let n = buf.len();
Read::read_exact(&mut self.fill_buf()?, buf)?;
self.pos += n as u64;
Ok(())
}
}
impl<T> BufRead for Cursor<T>
where
T: AsRef<[u8]>,
{
fn fill_buf(&mut self) -> Result<&[u8]> {
let amt = cmp::min(self.pos, self.inner.as_ref().len() as u64);
Ok(&self.inner.as_ref()[(amt as usize)..])
}
fn consume(&mut self, amt: usize) {
self.pos += amt as u64;
}
}
// Non-resizing write implementation
#[inline]
fn slice_write(pos_mut: &mut u64, slice: &mut [u8], buf: &[u8]) -> Result<usize> {
let pos = cmp::min(*pos_mut, slice.len() as u64);
let amt = (&mut slice[(pos as usize)..]).write(buf)?;
*pos_mut += amt as u64;
Ok(amt)
}
impl Write for Cursor<&mut [u8]> {
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize> {
slice_write(&mut self.pos, self.inner, buf)
}
#[inline]
fn flush(&mut self) -> Result<()> {
Ok(())
}
}
+458
View File
@@ -0,0 +1,458 @@
use core::{convert::From, fmt, result};
/// A specialized [`Result`] type for I/O operations.
///
/// This type is broadly used across [`std::io`] for any operation which may
/// produce an error.
///
/// This typedef is generally used to avoid writing out [`io::Error`] directly and
/// is otherwise a direct mapping to [`Result`].
///
/// While usual Rust style is to import types directly, aliases of [`Result`]
/// often are not, to make it easier to distinguish between them. [`Result`] is
/// generally assumed to be [`std::result::Result`][`Result`], and so users of this alias
/// will generally use `io::Result` instead of shadowing the [prelude]'s import
/// of [`std::result::Result`][`Result`].
///
/// [`std::io`]: crate::io
/// [`io::Error`]: Error
/// [`Result`]: crate::result::Result
/// [prelude]: crate::prelude
///
/// # Examples
///
/// A convenience function that bubbles an `io::Result` to its caller:
///
/// ```
/// use core2::io;
///
/// #[cfg(feature = "std")]
/// fn get_string() -> io::Result<String> {
/// let mut buffer = String::new();
///
/// std::io::stdin().read_line(&mut buffer).map_err(|e| io::Error::from(e))?;
///
/// Ok(buffer)
/// }
/// ```
pub type Result<T> = result::Result<T, Error>;
/// The error type for I/O operations of the [`Read`], [`Write`], [`Seek`], and
/// associated traits.
///
/// Errors mostly originate from the underlying OS, but custom instances of
/// `Error` can be created with crafted error messages and a particular value of
/// [`ErrorKind`].
///
/// [`Read`]: crate::io::Read
/// [`Write`]: crate::io::Write
/// [`Seek`]: crate::io::Seek
pub struct Error {
repr: Repr,
}
impl crate::error::Error for Error {}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.repr, f)
}
}
enum Repr {
Simple(ErrorKind),
Custom(Custom),
}
#[derive(Debug)]
struct Custom {
kind: ErrorKind,
error: &'static str,
}
/// A list specifying general categories of I/O error.
///
/// This list is intended to grow over time and it is not recommended to
/// exhaustively match against it.
///
/// It is used with the [`io::Error`] type.
///
/// [`io::Error`]: Error
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
// #[allow(deprecated)]
#[non_exhaustive]
pub enum ErrorKind {
/// An entity was not found, often a file.
NotFound,
/// The operation lacked the necessary privileges to complete.
PermissionDenied,
/// The connection was refused by the remote server.
ConnectionRefused,
/// The connection was reset by the remote server.
ConnectionReset,
/// The connection was aborted (terminated) by the remote server.
ConnectionAborted,
/// The network operation failed because it was not connected yet.
NotConnected,
/// A socket address could not be bound because the address is already in
/// use elsewhere.
AddrInUse,
/// A nonexistent interface was requested or the requested address was not
/// local.
AddrNotAvailable,
/// The operation failed because a pipe was closed.
BrokenPipe,
/// An entity already exists, often a file.
AlreadyExists,
/// The operation needs to block to complete, but the blocking operation was
/// requested to not occur.
WouldBlock,
/// A parameter was incorrect.
InvalidInput,
/// Data not valid for the operation were encountered.
///
/// Unlike [`InvalidInput`], this typically means that the operation
/// parameters were valid, however the error was caused by malformed
/// input data.
///
/// For example, a function that reads a file into a string will error with
/// `InvalidData` if the file's contents are not valid UTF-8.
///
/// [`InvalidInput`]: ErrorKind::InvalidInput
InvalidData,
/// The I/O operation's timeout expired, causing it to be canceled.
TimedOut,
/// An error returned when an operation could not be completed because a
/// call to [`write`] returned [`Ok(0)`].
///
/// This typically means that an operation could only succeed if it wrote a
/// particular number of bytes but only a smaller number of bytes could be
/// written.
///
/// [`write`]: crate::io::Write::write
/// [`Ok(0)`]: Ok
WriteZero,
/// This operation was interrupted.
///
/// Interrupted operations can typically be retried.
Interrupted,
/// Any I/O error not part of this list.
///
/// Errors that are `Other` now may move to a different or a new
/// [`ErrorKind`] variant in the future. It is not recommended to match
/// an error against `Other` and to expect any additional characteristics,
/// e.g., a specific [`Error::raw_os_error`] return value.
Other,
/// An error returned when an operation could not be completed because an
/// "end of file" was reached prematurely.
///
/// This typically means that an operation could only succeed if it read a
/// particular number of bytes but only a smaller number of bytes could be
/// read.
UnexpectedEof,
/// Any I/O error from the standard library that's not part of this list.
///
/// Errors that are `Uncategorized` now may move to a different or a new
/// [`ErrorKind`] variant in the future. It is not recommended to match
/// an error against `Uncategorized`; use a wildcard match (`_`) instead.
#[doc(hidden)]
Uncategorized,
}
impl ErrorKind {
pub(crate) fn as_str(&self) -> &'static str {
match *self {
ErrorKind::NotFound => "entity not found",
ErrorKind::PermissionDenied => "permission denied",
ErrorKind::ConnectionRefused => "connection refused",
ErrorKind::ConnectionReset => "connection reset",
ErrorKind::ConnectionAborted => "connection aborted",
ErrorKind::NotConnected => "not connected",
ErrorKind::AddrInUse => "address in use",
ErrorKind::AddrNotAvailable => "address not available",
ErrorKind::BrokenPipe => "broken pipe",
ErrorKind::AlreadyExists => "entity already exists",
ErrorKind::WouldBlock => "operation would block",
ErrorKind::InvalidInput => "invalid input parameter",
ErrorKind::InvalidData => "invalid data",
ErrorKind::TimedOut => "timed out",
ErrorKind::WriteZero => "write zero",
ErrorKind::Interrupted => "operation interrupted",
ErrorKind::Other => "other os error",
ErrorKind::UnexpectedEof => "unexpected end of file",
ErrorKind::Uncategorized => "uncategorized",
}
}
}
#[cfg(feature = "std")]
impl From<std::io::ErrorKind> for ErrorKind {
/// Converts an [`std::io::ErrorKind`] into an [`ErrorKind`].
///
/// This conversion allocates a new error with a simple representation of error kind.
///
/// # Examples
///
/// ```
/// use core2::io::{Error, ErrorKind};
///
/// let not_found = ErrorKind::from(std::io::ErrorKind::NotFound);
/// let err: Error = not_found.into();
/// assert_eq!("entity not found", format!("{}", err));
/// ```
fn from(k: std::io::ErrorKind) -> Self {
match k {
std::io::ErrorKind::NotFound => ErrorKind::NotFound,
std::io::ErrorKind::PermissionDenied => ErrorKind::PermissionDenied,
std::io::ErrorKind::ConnectionRefused => ErrorKind::ConnectionRefused,
std::io::ErrorKind::ConnectionReset => ErrorKind::ConnectionReset,
std::io::ErrorKind::ConnectionAborted => ErrorKind::ConnectionAborted,
std::io::ErrorKind::NotConnected => ErrorKind::NotConnected,
std::io::ErrorKind::AddrInUse => ErrorKind::AddrInUse,
std::io::ErrorKind::AddrNotAvailable => ErrorKind::AddrNotAvailable,
std::io::ErrorKind::BrokenPipe => ErrorKind::BrokenPipe,
std::io::ErrorKind::AlreadyExists => ErrorKind::AlreadyExists,
std::io::ErrorKind::WouldBlock => ErrorKind::WouldBlock,
std::io::ErrorKind::InvalidInput => ErrorKind::InvalidInput,
std::io::ErrorKind::InvalidData => ErrorKind::InvalidData,
std::io::ErrorKind::TimedOut => ErrorKind::TimedOut,
std::io::ErrorKind::WriteZero => ErrorKind::WriteZero,
std::io::ErrorKind::Interrupted => ErrorKind::Interrupted,
std::io::ErrorKind::Other => ErrorKind::Other,
std::io::ErrorKind::UnexpectedEof => ErrorKind::UnexpectedEof,
_ => ErrorKind::Uncategorized,
}
}
}
/// Intended for use for errors not exposed to the user, where allocating onto
/// the heap (for normal construction via Error::new) is too costly.
impl From<ErrorKind> for Error {
/// Converts an [`ErrorKind`] into an [`Error`].
///
/// This conversion allocates a new error with a simple representation of error kind.
///
/// # Examples
///
/// ```
/// use core2::io::{Error, ErrorKind};
///
/// let not_found = ErrorKind::NotFound;
/// let error = Error::from(not_found);
/// assert_eq!("entity not found", format!("{}", error));
/// ```
#[inline]
fn from(kind: ErrorKind) -> Error {
Error {
repr: Repr::Simple(kind),
}
}
}
#[cfg(feature = "std")]
impl From<std::io::Error> for Error {
/// Converts an [`std::io::ErrorKind`] into an [`Error`].
///
/// This conversion allocates a new error with a simple representation of error kind.
///
/// # Examples
///
/// ```
/// use core2::io::{Error, ErrorKind};
///
/// let not_found = std::io::Error::from(std::io::ErrorKind::NotFound);
/// let error = Error::from(not_found);
/// assert_eq!("entity not found", format!("{}", error));
/// ```
#[inline]
fn from(err: std::io::Error) -> Self {
Self::from(ErrorKind::from(err.kind()))
}
}
impl Error {
/// Creates a new I/O error from a known kind of error as well as an
/// arbitrary error payload.
///
/// This function is used to generically create I/O errors which do not
/// originate from the OS itself. The `error` argument is an arbitrary
/// payload which will be contained in this [`Error`].
///
/// # Examples
///
/// ```
/// use core2::io::{Error, ErrorKind};
///
/// // errors can be created from strings
/// let custom_error = Error::new(ErrorKind::Other, "oh no!");
///
/// // errors can also be created from other errors
/// let custom_error2 = Error::new(ErrorKind::Interrupted, custom_error.into_inner().unwrap());
/// ```
pub fn new(kind: ErrorKind, error: &'static str) -> Error {
Self::_new(kind, error.into())
}
fn _new(kind: ErrorKind, error: &'static str) -> Error {
Error {
repr: Repr::Custom(Custom { kind, error }),
}
}
/// Returns a reference to the inner error wrapped by this error (if any).
///
/// If this [`Error`] was constructed via [`new`] then this function will
/// return [`Some`], otherwise it will return [`None`].
///
/// [`new`]: Error::new
///
/// # Examples
///
/// ```
/// use core2::io::{Error, ErrorKind};
///
/// fn print_error(err: &Error) {
/// if let Some(inner_err) = err.get_ref() {
/// println!("Inner error: {:?}", inner_err);
/// } else {
/// println!("No inner error");
/// }
/// }
///
/// #[cfg(feature = "std")]
/// fn emit_error() {
/// // Will print "No inner error".
/// print_error(&Error::from(std::io::Error::last_os_error()));
/// // Will print "Inner error: ...".
/// print_error(&Error::new(ErrorKind::Other, "oh no!"));
/// }
///
/// #[cfg(not(feature = "std"))]
/// fn emit_error() {
/// // Will print "No inner error".
/// print_error(&ErrorKind::Other.into());
/// // Will print "Inner error: ...".
/// print_error(&Error::new(ErrorKind::Other, "oh no!"));
/// }
///
/// fn main() {
/// emit_error();
/// }
/// ```
pub fn get_ref(&self) -> Option<&&'static str> {
match self.repr {
Repr::Simple(..) => None,
Repr::Custom(ref c) => Some(&c.error),
}
}
/// Consumes the `Error`, returning its inner error (if any).
///
/// If this [`Error`] was constructed via [`new`] then this function will
/// return [`Some`], otherwise it will return [`None`].
///
/// [`new`]: Error::new
///
/// # Examples
///
/// ```
/// use core2::io::{Error, ErrorKind};
///
/// fn print_error(err: Error) {
/// if let Some(inner_err) = err.into_inner() {
/// println!("Inner error: {}", inner_err);
/// } else {
/// println!("No inner error");
/// }
/// }
///
/// #[cfg(feature = "std")]
/// fn emit_error() {
/// // Will print "No inner error".
/// print_error(std::io::Error::last_os_error().into());
/// // Will print "Inner error: ...".
/// print_error(Error::new(ErrorKind::Other, "oh no!"));
/// }
///
/// #[cfg(not(feature = "std"))]
/// fn emit_error() {
/// // Will print "No inner error".
/// print_error(ErrorKind::Other.into());
/// // Will print "Inner error: ...".
/// print_error(Error::new(ErrorKind::Other, "oh no!"));
/// }
///
/// fn main() {
/// emit_error();
/// }
/// ```
pub fn into_inner(self) -> Option<&'static str> {
match self.repr {
Repr::Simple(..) => None,
Repr::Custom(c) => Some(c.error),
}
}
/// Returns the corresponding [`ErrorKind`] for this error.
///
/// # Examples
///
/// ```
/// use core2::io::{Error, ErrorKind};
///
/// fn print_error(err: Error) {
/// println!("{:?}", err.kind());
/// }
///
/// #[cfg(feature = "std")]
/// fn emit_error() {
/// // Will print "Other".
/// print_error(std::io::Error::last_os_error().into());
/// // Will print "AddrInUse".
/// print_error(Error::new(ErrorKind::AddrInUse, "oh no!"));
/// }
///
/// #[cfg(not(feature = "std"))]
/// fn emit_error() {
/// // Will print "Other".
/// print_error(ErrorKind::Other.into());
/// // Will print "AddrInUse".
/// print_error(Error::new(ErrorKind::AddrInUse, "oh no!"));
/// }
///
/// fn main() {
/// emit_error();
/// }
/// ```
pub fn kind(&self) -> ErrorKind {
match self.repr {
Repr::Custom(ref c) => c.kind,
Repr::Simple(kind) => kind,
}
}
}
impl fmt::Debug for Repr {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Repr::Custom(ref c) => fmt::Debug::fmt(&c, fmt),
Repr::Simple(kind) => fmt.debug_tuple("Kind").field(&kind).finish(),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.repr {
Repr::Custom(ref c) => c.error.fmt(fmt),
Repr::Simple(kind) => write!(fmt, "{}", kind.as_str()),
}
}
}
fn _assert_error_is_sync_send() {
fn _is_sync_send<T: Sync + Send>() {}
_is_sync_send::<Error>();
}
+176
View File
@@ -0,0 +1,176 @@
use super::error::{Error, ErrorKind, Result};
use super::traits::{BufRead, Read, Seek, SeekFrom, Write};
use core::{cmp, fmt, mem};
// =============================================================================
// Forwarding implementations
impl<R: Read + ?Sized> Read for &mut R {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
(**self).read(buf)
}
#[inline]
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
(**self).read_exact(buf)
}
}
impl<W: Write + ?Sized> Write for &mut W {
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize> {
(**self).write(buf)
}
#[inline]
fn flush(&mut self) -> Result<()> {
(**self).flush()
}
#[inline]
fn write_all(&mut self, buf: &[u8]) -> Result<()> {
(**self).write_all(buf)
}
#[inline]
fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> Result<()> {
(**self).write_fmt(fmt)
}
}
impl<S: Seek + ?Sized> Seek for &mut S {
#[inline]
fn seek(&mut self, pos: SeekFrom) -> Result<u64> {
(**self).seek(pos)
}
}
impl<B: BufRead + ?Sized> BufRead for &mut B {
#[inline]
fn fill_buf(&mut self) -> Result<&[u8]> {
(**self).fill_buf()
}
#[inline]
fn consume(&mut self, amt: usize) {
(**self).consume(amt)
}
}
// =============================================================================
// In-memory buffer implementations
/// Read is implemented for `&[u8]` by copying from the slice.
///
/// Note that reading updates the slice to point to the yet unread part.
/// The slice will be empty when EOF is reached.
impl Read for &[u8] {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
let amt = cmp::min(buf.len(), self.len());
let (a, b) = self.split_at(amt);
// First check if the amount of bytes we want to read is small:
// `copy_from_slice` will generally expand to a call to `memcpy`, and
// for a single byte the overhead is significant.
if amt == 1 {
buf[0] = a[0];
} else {
buf[..amt].copy_from_slice(a);
}
*self = b;
Ok(amt)
}
#[inline]
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
if buf.len() > self.len() {
return Err(Error::new(
ErrorKind::UnexpectedEof,
"failed to fill whole buffer",
));
}
let (a, b) = self.split_at(buf.len());
// First check if the amount of bytes we want to read is small:
// `copy_from_slice` will generally expand to a call to `memcpy`, and
// for a single byte the overhead is significant.
if buf.len() == 1 {
buf[0] = a[0];
} else {
buf.copy_from_slice(a);
}
*self = b;
Ok(())
}
}
impl BufRead for &[u8] {
#[inline]
fn fill_buf(&mut self) -> Result<&[u8]> {
Ok(*self)
}
#[inline]
fn consume(&mut self, amt: usize) {
*self = &self[amt..];
}
}
/// Write is implemented for `&mut [u8]` by copying into the slice, overwriting
/// its data.
///
/// Note that writing updates the slice to point to the yet unwritten part.
/// The slice will be empty when it has been completely overwritten.
impl Write for &mut [u8] {
#[inline]
fn write(&mut self, data: &[u8]) -> Result<usize> {
let amt = cmp::min(data.len(), self.len());
let (a, b) = mem::replace(self, &mut []).split_at_mut(amt);
a.copy_from_slice(&data[..amt]);
*self = b;
Ok(amt)
}
#[inline]
fn write_all(&mut self, data: &[u8]) -> Result<()> {
if self.write(data)? == data.len() {
Ok(())
} else {
Err(Error::new(
ErrorKind::WriteZero,
"failed to write whole buffer",
))
}
}
#[inline]
fn flush(&mut self) -> Result<()> {
Ok(())
}
}
/// Write is implemented for `Vec<u8>` by appending to the vector.
/// The vector will grow as needed.
#[cfg(feature = "alloc")]
impl Write for alloc::vec::Vec<u8> {
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize> {
self.extend_from_slice(buf);
Ok(buf.len())
}
#[inline]
fn write_all(&mut self, buf: &[u8]) -> Result<()> {
self.extend_from_slice(buf);
Ok(())
}
#[inline]
fn flush(&mut self) -> Result<()> {
Ok(())
}
}
+26
View File
@@ -0,0 +1,26 @@
#[cfg(feature = "nightly")]
mod buffered;
mod cursor;
mod error;
mod impls;
mod traits;
mod util;
#[cfg(not(feature = "std"))]
pub use cursor::Cursor;
#[cfg(not(feature = "std"))]
pub use error::{Error, ErrorKind, Result};
#[cfg(not(feature = "std"))]
pub use traits::{BufRead, Bytes, Chain, Read, Seek, SeekFrom, Take, Write};
#[cfg(feature = "std")]
pub use std::io::{
BufRead, Bytes, Chain, Cursor, Error, ErrorKind, Read, Result, Seek, SeekFrom, Take, Write,
};
// Use this crate's implementation on both std and no_std
#[cfg(feature = "nightly")]
pub use buffered::{BufReader, BufWriter, LineWriter};
#[cfg(feature = "nightly")]
pub use util::copy;
+1363
View File
File diff suppressed because it is too large Load Diff
+40
View File
@@ -0,0 +1,40 @@
#[cfg(feature = "nightly")]
use core::mem::MaybeUninit;
#[cfg(feature = "nightly")]
use crate::io::{ErrorKind, Read, Write};
#[cfg(feature = "nightly")]
pub fn copy<R: ?Sized, W: ?Sized, const S: usize>(
reader: &mut R,
writer: &mut W,
) -> crate::io::Result<u64>
where
R: Read,
W: Write,
{
let mut buf = MaybeUninit::<[u8; S]>::uninit();
// FIXME: #42788
//
// - This creates a (mut) reference to a slice of
// _uninitialized_ integers, which is **undefined behavior**
//
// - Only the standard library gets to soundly "ignore" this,
// based on its privileged knowledge of unstable rustc
// internals;
unsafe {
reader.initializer().initialize(buf.assume_init_mut());
}
let mut written = 0;
loop {
let len = match reader.read(unsafe { buf.assume_init_mut() }) {
Ok(0) => return Ok(written),
Ok(len) => len,
Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
Err(e) => return Err(e),
};
writer.write_all(unsafe { &buf.assume_init_ref()[..len] })?;
written += len as u64;
}
}
+20
View File
@@ -0,0 +1,20 @@
#![cfg_attr(feature = "nightly", feature(maybe_uninit_ref))]
#![cfg_attr(feature = "nightly", feature(never_type))]
#![cfg_attr(all(feature = "std", feature = "nightly"), feature(read_initializer))]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(feature = "std", allow(dead_code))]
#[cfg(not(feature = "std"))]
pub mod error;
#[cfg(feature = "std")]
pub use std::error as error;
#[cfg(not(feature = "std"))]
pub mod io;
#[cfg(feature = "std")]
pub use std::io as io;
#[cfg(feature = "alloc")]
extern crate alloc;
+501
View File
@@ -0,0 +1,501 @@
use core::cmp;
use core2::io::{self as io, BufRead, Cursor, Read, Write};
// #[test]
// #[cfg_attr(target_os = "emscripten", ignore)]
// fn read_until() {
// let mut buf = Cursor::new(&b"12"[..]);
// let mut v = Vec::new();
// assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 2);
// assert_eq!(v, b"12");
// let mut buf = Cursor::new(&b"1233"[..]);
// let mut v = Vec::new();
// assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 3);
// assert_eq!(v, b"123");
// v.truncate(0);
// assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 1);
// assert_eq!(v, b"3");
// v.truncate(0);
// assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 0);
// assert_eq!(v, []);
// }
// #[test]
// fn split() {
// let buf = Cursor::new(&b"12"[..]);
// let mut s = buf.split(b'3');
// assert_eq!(s.next().unwrap().unwrap(), vec![b'1', b'2']);
// assert!(s.next().is_none());
// let buf = Cursor::new(&b"1233"[..]);
// let mut s = buf.split(b'3');
// assert_eq!(s.next().unwrap().unwrap(), vec![b'1', b'2']);
// assert_eq!(s.next().unwrap().unwrap(), vec![]);
// assert!(s.next().is_none());
// }
// #[test]
// fn read_line() {
// let mut buf = Cursor::new(&b"12"[..]);
// let mut v = String::new();
// assert_eq!(buf.read_line(&mut v).unwrap(), 2);
// assert_eq!(v, "12");
// let mut buf = Cursor::new(&b"12\n\n"[..]);
// let mut v = String::new();
// assert_eq!(buf.read_line(&mut v).unwrap(), 3);
// assert_eq!(v, "12\n");
// v.truncate(0);
// assert_eq!(buf.read_line(&mut v).unwrap(), 1);
// assert_eq!(v, "\n");
// v.truncate(0);
// assert_eq!(buf.read_line(&mut v).unwrap(), 0);
// assert_eq!(v, "");
// }
// #[test]
// fn lines() {
// let buf = Cursor::new(&b"12\r"[..]);
// let mut s = buf.lines();
// assert_eq!(s.next().unwrap().unwrap(), "12\r".to_string());
// assert!(s.next().is_none());
// let buf = Cursor::new(&b"12\r\n\n"[..]);
// let mut s = buf.lines();
// assert_eq!(s.next().unwrap().unwrap(), "12".to_string());
// assert_eq!(s.next().unwrap().unwrap(), "".to_string());
// assert!(s.next().is_none());
// }
// #[test]
// fn read_to_end() {
// let mut c = Cursor::new(&b""[..]);
// let mut v = Vec::new();
// assert_eq!(c.read_to_end(&mut v).unwrap(), 0);
// assert_eq!(v, []);
// let mut c = Cursor::new(&b"1"[..]);
// let mut v = Vec::new();
// assert_eq!(c.read_to_end(&mut v).unwrap(), 1);
// assert_eq!(v, b"1");
// let cap = 1024 * 1024;
// let data = (0..cap).map(|i| (i / 3) as u8).collect::<Vec<_>>();
// let mut v = Vec::new();
// let (a, b) = data.split_at(data.len() / 2);
// assert_eq!(Cursor::new(a).read_to_end(&mut v).unwrap(), a.len());
// assert_eq!(Cursor::new(b).read_to_end(&mut v).unwrap(), b.len());
// assert_eq!(v, data);
// }
// #[test]
// fn read_to_string() {
// let mut c = Cursor::new(&b""[..]);
// let mut v = String::new();
// assert_eq!(c.read_to_string(&mut v).unwrap(), 0);
// assert_eq!(v, "");
// let mut c = Cursor::new(&b"1"[..]);
// let mut v = String::new();
// assert_eq!(c.read_to_string(&mut v).unwrap(), 1);
// assert_eq!(v, "1");
// let mut c = Cursor::new(&b"\xff"[..]);
// let mut v = String::new();
// assert!(c.read_to_string(&mut v).is_err());
// }
#[test]
fn read_exact() {
let mut buf = [0; 4];
let mut c = Cursor::new(&b""[..]);
assert_eq!(
c.read_exact(&mut buf).unwrap_err().kind(),
io::ErrorKind::UnexpectedEof
);
let mut c = Cursor::new(&b"123"[..]).chain(Cursor::new(&b"456789"[..]));
c.read_exact(&mut buf).unwrap();
assert_eq!(&buf, b"1234");
c.read_exact(&mut buf).unwrap();
assert_eq!(&buf, b"5678");
assert_eq!(
c.read_exact(&mut buf).unwrap_err().kind(),
io::ErrorKind::UnexpectedEof
);
}
#[test]
fn read_exact_slice() {
let mut buf = [0; 4];
let mut c = &b""[..];
assert_eq!(
c.read_exact(&mut buf).unwrap_err().kind(),
io::ErrorKind::UnexpectedEof
);
let mut c = &b"123"[..];
assert_eq!(
c.read_exact(&mut buf).unwrap_err().kind(),
io::ErrorKind::UnexpectedEof
);
// make sure the optimized (early returning) method is being used
assert_eq!(&buf, &[0; 4]);
let mut c = &b"1234"[..];
c.read_exact(&mut buf).unwrap();
assert_eq!(&buf, b"1234");
let mut c = &b"56789"[..];
c.read_exact(&mut buf).unwrap();
assert_eq!(&buf, b"5678");
assert_eq!(c, b"9");
}
#[test]
fn take_eof() {
struct R;
impl Read for R {
fn read(&mut self, _: &mut [u8]) -> io::Result<usize> {
Err(io::Error::new(io::ErrorKind::Other, ""))
}
}
impl BufRead for R {
fn fill_buf(&mut self) -> io::Result<&[u8]> {
Err(io::Error::new(io::ErrorKind::Other, ""))
}
fn consume(&mut self, _amt: usize) {}
}
let mut buf = [0; 1];
assert_eq!(0, R.take(0).read(&mut buf).unwrap());
assert_eq!(b"", R.take(0).fill_buf().unwrap());
}
fn cmp_bufread<Br1: BufRead, Br2: BufRead>(mut br1: Br1, mut br2: Br2, exp: &[u8]) {
let mut cat = Vec::new();
loop {
let consume = {
let buf1 = br1.fill_buf().unwrap();
let buf2 = br2.fill_buf().unwrap();
let minlen = if buf1.len() < buf2.len() {
buf1.len()
} else {
buf2.len()
};
assert_eq!(buf1[..minlen], buf2[..minlen]);
cat.extend_from_slice(&buf1[..minlen]);
minlen
};
if consume == 0 {
break;
}
br1.consume(consume);
br2.consume(consume);
}
assert_eq!(br1.fill_buf().unwrap().len(), 0);
assert_eq!(br2.fill_buf().unwrap().len(), 0);
assert_eq!(&cat[..], &exp[..])
}
#[test]
fn chain_bufread() {
let testdata = b"ABCDEFGHIJKL";
let chain1 = (&testdata[..3])
.chain(&testdata[3..6])
.chain(&testdata[6..9])
.chain(&testdata[9..]);
let chain2 = (&testdata[..4])
.chain(&testdata[4..8])
.chain(&testdata[8..]);
cmp_bufread(chain1, chain2, &testdata[..]);
}
// #[test]
// fn chain_zero_length_read_is_not_eof() {
// let a = b"A";
// let b = b"B";
// let mut s = String::new();
// let mut chain = (&a[..]).chain(&b[..]);
// chain.read(&mut []).unwrap();
// chain.read_to_string(&mut s).unwrap();
// assert_eq!("AB", s);
// }
// #[bench]
// #[cfg_attr(target_os = "emscripten", ignore)]
// fn bench_read_to_end(b: &mut test::Bencher) {
// b.iter(|| {
// let mut lr = repeat(1).take(10000000);
// let mut vec = Vec::with_capacity(1024);
// super::read_to_end(&mut lr, &mut vec)
// });
// }
// #[test]
// fn seek_len() -> io::Result<()> {
// let mut c = Cursor::new(vec![0; 15]);
// assert_eq!(c.stream_len()?, 15);
// c.seek(SeekFrom::End(0))?;
// let old_pos = c.stream_position()?;
// assert_eq!(c.stream_len()?, 15);
// assert_eq!(c.stream_position()?, old_pos);
// c.seek(SeekFrom::Start(7))?;
// c.seek(SeekFrom::Current(2))?;
// let old_pos = c.stream_position()?;
// assert_eq!(c.stream_len()?, 15);
// assert_eq!(c.stream_position()?, old_pos);
// Ok(())
// }
// #[test]
// fn seek_position() -> io::Result<()> {
// // All `asserts` are duplicated here to make sure the method does not
// // change anything about the seek state.
// let mut c = Cursor::new(vec![0; 15]);
// assert_eq!(c.stream_position()?, 0);
// assert_eq!(c.stream_position()?, 0);
// c.seek(SeekFrom::End(0))?;
// assert_eq!(c.stream_position()?, 15);
// assert_eq!(c.stream_position()?, 15);
// c.seek(SeekFrom::Start(7))?;
// c.seek(SeekFrom::Current(2))?;
// assert_eq!(c.stream_position()?, 9);
// assert_eq!(c.stream_position()?, 9);
// c.seek(SeekFrom::End(-3))?;
// c.seek(SeekFrom::Current(1))?;
// c.seek(SeekFrom::Current(-5))?;
// assert_eq!(c.stream_position()?, 8);
// assert_eq!(c.stream_position()?, 8);
// Ok(())
// }
// A simple example reader which uses the default implementation of
// read_to_end.
struct ExampleSliceReader<'a> {
slice: &'a [u8],
}
impl<'a> Read for ExampleSliceReader<'a> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let len = cmp::min(self.slice.len(), buf.len());
buf[..len].copy_from_slice(&self.slice[..len]);
self.slice = &self.slice[len..];
Ok(len)
}
}
// #[test]
// fn test_read_to_end_capacity() -> io::Result<()> {
// let input = &b"foo"[..];
// // read_to_end() generally needs to over-allocate, both for efficiency
// // and so that it can distinguish EOF. Assert that this is the case
// // with this simple ExampleSliceReader struct, which uses the default
// // implementation of read_to_end. Even though vec1 is allocated with
// // exactly enough capacity for the read, read_to_end will allocate more
// // space here.
// let mut vec1 = Vec::with_capacity(input.len());
// ExampleSliceReader { slice: input }.read_to_end(&mut vec1)?;
// assert_eq!(vec1.len(), input.len());
// assert!(vec1.capacity() > input.len(), "allocated more");
// // However, std::io::Take includes an implementation of read_to_end
// // that will not allocate when the limit has already been reached. In
// // this case, vec2 never grows.
// let mut vec2 = Vec::with_capacity(input.len());
// ExampleSliceReader { slice: input }.take(input.len() as u64).read_to_end(&mut vec2)?;
// assert_eq!(vec2.len(), input.len());
// assert_eq!(vec2.capacity(), input.len(), "did not allocate more");
// Ok(())
// }
// #[test]
// fn io_slice_mut_advance() {
// let mut buf1 = [1; 8];
// let mut buf2 = [2; 16];
// let mut buf3 = [3; 8];
// let mut bufs = &mut [
// IoSliceMut::new(&mut buf1),
// IoSliceMut::new(&mut buf2),
// IoSliceMut::new(&mut buf3),
// ][..];
// // Only in a single buffer..
// bufs = IoSliceMut::advance(bufs, 1);
// assert_eq!(bufs[0].deref(), [1; 7].as_ref());
// assert_eq!(bufs[1].deref(), [2; 16].as_ref());
// assert_eq!(bufs[2].deref(), [3; 8].as_ref());
// // Removing a buffer, leaving others as is.
// bufs = IoSliceMut::advance(bufs, 7);
// assert_eq!(bufs[0].deref(), [2; 16].as_ref());
// assert_eq!(bufs[1].deref(), [3; 8].as_ref());
// // Removing a buffer and removing from the next buffer.
// bufs = IoSliceMut::advance(bufs, 18);
// assert_eq!(bufs[0].deref(), [3; 6].as_ref());
// }
// #[test]
// fn io_slice_mut_advance_empty_slice() {
// let empty_bufs = &mut [][..];
// // Shouldn't panic.
// IoSliceMut::advance(empty_bufs, 1);
// }
// #[test]
// fn io_slice_mut_advance_beyond_total_length() {
// let mut buf1 = [1; 8];
// let mut bufs = &mut [IoSliceMut::new(&mut buf1)][..];
// // Going beyond the total length should be ok.
// bufs = IoSliceMut::advance(bufs, 9);
// assert!(bufs.is_empty());
// }
// #[test]
// fn io_slice_advance() {
// let buf1 = [1; 8];
// let buf2 = [2; 16];
// let buf3 = [3; 8];
// let mut bufs = &mut [IoSlice::new(&buf1), IoSlice::new(&buf2), IoSlice::new(&buf3)][..];
// // Only in a single buffer..
// bufs = IoSlice::advance(bufs, 1);
// assert_eq!(bufs[0].deref(), [1; 7].as_ref());
// assert_eq!(bufs[1].deref(), [2; 16].as_ref());
// assert_eq!(bufs[2].deref(), [3; 8].as_ref());
// // Removing a buffer, leaving others as is.
// bufs = IoSlice::advance(bufs, 7);
// assert_eq!(bufs[0].deref(), [2; 16].as_ref());
// assert_eq!(bufs[1].deref(), [3; 8].as_ref());
// // Removing a buffer and removing from the next buffer.
// bufs = IoSlice::advance(bufs, 18);
// assert_eq!(bufs[0].deref(), [3; 6].as_ref());
// }
// #[test]
// fn io_slice_advance_empty_slice() {
// let empty_bufs = &mut [][..];
// // Shouldn't panic.
// IoSlice::advance(empty_bufs, 1);
// }
// #[test]
// fn io_slice_advance_beyond_total_length() {
// let buf1 = [1; 8];
// let mut bufs = &mut [IoSlice::new(&buf1)][..];
// // Going beyond the total length should be ok.
// bufs = IoSlice::advance(bufs, 9);
// assert!(bufs.is_empty());
// }
/// Create a new writer that reads from at most `n_bufs` and reads
/// `per_call` bytes (in total) per call to write.
fn test_writer() -> TestWriter {
TestWriter {
written: Vec::new(),
per_call: 2,
}
}
struct TestWriter {
written: Vec<u8>,
per_call: usize,
}
impl Write for TestWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let n = self.per_call.min(buf.len());
self.written.extend_from_slice(&buf[..n]);
Ok(n)
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
#[test]
fn test_writer_read_from_one_buf() {
let mut writer = test_writer();
assert_eq!(writer.write(&[]).unwrap(), 0);
// assert_eq!(writer.write_vectored(&[]).unwrap(), 0);
// Read at most 2 bytes.
assert_eq!(writer.write(&[1, 1, 1]).unwrap(), 2);
assert_eq!(writer.write(&[2, 2, 2]).unwrap(), 2);
// Only read from first buf.
assert_eq!(writer.write(&[3]).unwrap(), 1);
assert_eq!(writer.written, &[1, 1, 2, 2, 3]);
}
// #[test]
// fn test_writer_read_from_multiple_bufs() {
// let mut writer = test_writer(3, 3);
// // Read at most 3 bytes from two buffers.
// let bufs = &[IoSlice::new(&[1]), IoSlice::new(&[2, 2, 2])];
// assert_eq!(writer.write_vectored(bufs).unwrap(), 3);
// // Read at most 3 bytes from three buffers.
// let bufs = &[IoSlice::new(&[3]), IoSlice::new(&[4]), IoSlice::new(&[5, 5])];
// assert_eq!(writer.write_vectored(bufs).unwrap(), 3);
// assert_eq!(writer.written, &[1, 2, 2, 3, 4, 5]);
// }
// #[test]
// fn test_write_all_vectored() {
// #[rustfmt::skip] // Becomes unreadable otherwise.
// let tests: Vec<(_, &'static [u8])> = vec![
// (vec![], &[]),
// (vec![IoSlice::new(&[]), IoSlice::new(&[])], &[]),
// (vec![IoSlice::new(&[1])], &[1]),
// (vec![IoSlice::new(&[1, 2])], &[1, 2]),
// (vec![IoSlice::new(&[1, 2, 3])], &[1, 2, 3]),
// (vec![IoSlice::new(&[1, 2, 3, 4])], &[1, 2, 3, 4]),
// (vec![IoSlice::new(&[1, 2, 3, 4, 5])], &[1, 2, 3, 4, 5]),
// (vec![IoSlice::new(&[1]), IoSlice::new(&[2])], &[1, 2]),
// (vec![IoSlice::new(&[1]), IoSlice::new(&[2, 2])], &[1, 2, 2]),
// (vec![IoSlice::new(&[1, 1]), IoSlice::new(&[2, 2])], &[1, 1, 2, 2]),
// (vec![IoSlice::new(&[1, 1]), IoSlice::new(&[2, 2, 2])], &[1, 1, 2, 2, 2]),
// (vec![IoSlice::new(&[1, 1]), IoSlice::new(&[2, 2, 2])], &[1, 1, 2, 2, 2]),
// (vec![IoSlice::new(&[1, 1, 1]), IoSlice::new(&[2, 2, 2])], &[1, 1, 1, 2, 2, 2]),
// (vec![IoSlice::new(&[1, 1, 1]), IoSlice::new(&[2, 2, 2, 2])], &[1, 1, 1, 2, 2, 2, 2]),
// (vec![IoSlice::new(&[1, 1, 1, 1]), IoSlice::new(&[2, 2, 2, 2])], &[1, 1, 1, 1, 2, 2, 2, 2]),
// (vec![IoSlice::new(&[1]), IoSlice::new(&[2]), IoSlice::new(&[3])], &[1, 2, 3]),
// (vec![IoSlice::new(&[1, 1]), IoSlice::new(&[2, 2]), IoSlice::new(&[3, 3])], &[1, 1, 2, 2, 3, 3]),
// (vec![IoSlice::new(&[1]), IoSlice::new(&[2, 2]), IoSlice::new(&[3, 3, 3])], &[1, 2, 2, 3, 3, 3]),
// (vec![IoSlice::new(&[1, 1, 1]), IoSlice::new(&[2, 2, 2]), IoSlice::new(&[3, 3, 3])], &[1, 1, 1, 2, 2, 2, 3, 3, 3]),
// ];
// let writer_configs = &[(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)];
// for (n_bufs, per_call) in writer_configs.iter().copied() {
// for (mut input, wanted) in tests.clone().into_iter() {
// let mut writer = test_writer(n_bufs, per_call);
// assert!(writer.write_all_vectored(&mut *input).is_ok());
// assert_eq!(&*writer.written, &*wanted);
// }
// }
// }