2015-02-01 04:24:36 +00:00
|
|
|
|
#![allow(missing_copy_implementations)]
|
|
|
|
|
|
2019-02-10 19:23:21 +00:00
|
|
|
|
use crate::fmt;
|
2019-11-27 18:29:00 +00:00
|
|
|
|
use crate::io::{self, BufRead, ErrorKind, Initializer, IoSlice, IoSliceMut, Read, Write};
|
2019-07-17 07:51:58 +00:00
|
|
|
|
use crate::mem::MaybeUninit;
|
2015-02-01 04:24:36 +00:00
|
|
|
|
|
|
|
|
|
/// Copies the entire contents of a reader into a writer.
|
|
|
|
|
///
|
2015-05-20 20:02:46 +00:00
|
|
|
|
/// This function will continuously read data from `reader` and then
|
|
|
|
|
/// write it into `writer` in a streaming fashion until `reader`
|
|
|
|
|
/// returns EOF.
|
2015-02-01 04:24:36 +00:00
|
|
|
|
///
|
2015-05-20 20:02:46 +00:00
|
|
|
|
/// On success, the total number of bytes that were copied from
|
|
|
|
|
/// `reader` to `writer` is returned.
|
2015-02-01 04:24:36 +00:00
|
|
|
|
///
|
2018-08-15 03:31:17 +00:00
|
|
|
|
/// If you’re wanting to copy the contents of one file to another and you’re
|
|
|
|
|
/// working with filesystem paths, see the [`fs::copy`] function.
|
|
|
|
|
///
|
2020-08-18 17:36:52 +00:00
|
|
|
|
/// [`fs::copy`]: crate::fs::copy
|
2018-08-15 03:31:17 +00:00
|
|
|
|
///
|
2015-02-01 04:24:36 +00:00
|
|
|
|
/// # Errors
|
|
|
|
|
///
|
2020-08-18 17:36:52 +00:00
|
|
|
|
/// This function will return an error immediately if any call to [`read`] or
|
|
|
|
|
/// [`write`] returns an error. All instances of [`ErrorKind::Interrupted`] are
|
2015-02-01 04:24:36 +00:00
|
|
|
|
/// handled by this function and the underlying operation is retried.
|
2015-07-08 22:31:08 +00:00
|
|
|
|
///
|
2020-08-18 17:36:52 +00:00
|
|
|
|
/// [`read`]: Read::read
|
|
|
|
|
/// [`write`]: Write::write
|
|
|
|
|
///
|
2015-07-08 22:31:08 +00:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::io;
|
|
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
|
/// let mut reader: &[u8] = b"hello";
|
|
|
|
|
/// let mut writer: Vec<u8> = vec![];
|
2015-07-08 22:31:08 +00:00
|
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
|
/// io::copy(&mut reader, &mut writer)?;
|
2015-07-08 22:31:08 +00:00
|
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
|
/// assert_eq!(&b"hello"[..], &writer[..]);
|
|
|
|
|
/// Ok(())
|
|
|
|
|
/// }
|
2015-07-08 22:31:08 +00:00
|
|
|
|
/// ```
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-08-05 11:31:05 +00:00
|
|
|
|
pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> io::Result<u64>
|
2019-11-27 18:29:00 +00:00
|
|
|
|
where
|
|
|
|
|
R: Read,
|
|
|
|
|
W: Write,
|
2015-08-05 11:31:05 +00:00
|
|
|
|
{
|
2019-07-17 07:51:58 +00:00
|
|
|
|
let mut buf = MaybeUninit::<[u8; super::DEFAULT_BUF_SIZE]>::uninit();
|
|
|
|
|
// FIXME(#53491): This is calling `get_mut` and `get_ref` on an uninitialized
|
|
|
|
|
// `MaybeUninit`. Revisit this once we decided whether that is valid or not.
|
|
|
|
|
// This is still technically undefined behavior due to creating a reference
|
|
|
|
|
// to uninitialized data, but within libstd we can rely on more guarantees
|
2019-07-17 08:34:34 +00:00
|
|
|
|
// than if this code were in an external lib.
|
2019-11-27 18:29:00 +00:00
|
|
|
|
unsafe {
|
|
|
|
|
reader.initializer().initialize(buf.get_mut());
|
|
|
|
|
}
|
2017-05-15 01:29:18 +00:00
|
|
|
|
|
2015-02-01 04:24:36 +00:00
|
|
|
|
let mut written = 0;
|
|
|
|
|
loop {
|
2019-07-17 07:51:58 +00:00
|
|
|
|
let len = match reader.read(unsafe { buf.get_mut() }) {
|
2015-02-01 04:24:36 +00:00
|
|
|
|
Ok(0) => return Ok(written),
|
|
|
|
|
Ok(len) => len,
|
|
|
|
|
Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
|
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
|
};
|
2019-07-17 07:51:58 +00:00
|
|
|
|
writer.write_all(unsafe { &buf.get_ref()[..len] })?;
|
2015-02-01 04:24:36 +00:00
|
|
|
|
written += len as u64;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A reader which is always at EOF.
|
2015-07-20 18:41:00 +00:00
|
|
|
|
///
|
2020-08-18 17:36:52 +00:00
|
|
|
|
/// This struct is generally created by calling [`empty()`]. Please see
|
|
|
|
|
/// the documentation of [`empty()`] for more details.
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-11-27 18:29:00 +00:00
|
|
|
|
pub struct Empty {
|
|
|
|
|
_priv: (),
|
|
|
|
|
}
|
2015-02-01 04:24:36 +00:00
|
|
|
|
|
2015-07-08 22:31:08 +00:00
|
|
|
|
/// Constructs a new handle to an empty reader.
|
2015-02-01 04:24:36 +00:00
|
|
|
|
///
|
2017-09-19 16:10:38 +00:00
|
|
|
|
/// All reads from the returned reader will return [`Ok`]`(0)`.
|
|
|
|
|
///
|
2015-07-08 22:31:08 +00:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// A slightly sad example of not reading anything into a buffer:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
2016-06-28 01:13:26 +00:00
|
|
|
|
/// use std::io::{self, Read};
|
2015-07-08 22:31:08 +00:00
|
|
|
|
///
|
|
|
|
|
/// let mut buffer = String::new();
|
2016-06-28 01:13:26 +00:00
|
|
|
|
/// io::empty().read_to_string(&mut buffer).unwrap();
|
|
|
|
|
/// assert!(buffer.is_empty());
|
2015-07-08 22:31:08 +00:00
|
|
|
|
/// ```
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-11-27 18:29:00 +00:00
|
|
|
|
pub fn empty() -> Empty {
|
|
|
|
|
Empty { _priv: () }
|
|
|
|
|
}
|
2015-02-01 04:24:36 +00:00
|
|
|
|
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-01 04:24:36 +00:00
|
|
|
|
impl Read for Empty {
|
2017-05-15 01:29:18 +00:00
|
|
|
|
#[inline]
|
2019-11-27 18:29:00 +00:00
|
|
|
|
fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
|
Ok(0)
|
|
|
|
|
}
|
2017-05-15 01:29:18 +00:00
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
unsafe fn initializer(&self) -> Initializer {
|
|
|
|
|
Initializer::nop()
|
|
|
|
|
}
|
2015-02-01 04:24:36 +00:00
|
|
|
|
}
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl BufRead for Empty {
|
2017-05-15 01:29:18 +00:00
|
|
|
|
#[inline]
|
2019-11-27 18:29:00 +00:00
|
|
|
|
fn fill_buf(&mut self) -> io::Result<&[u8]> {
|
|
|
|
|
Ok(&[])
|
|
|
|
|
}
|
2017-05-15 01:29:18 +00:00
|
|
|
|
#[inline]
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
|
fn consume(&mut self, _n: usize) {}
|
|
|
|
|
}
|
2015-02-01 04:24:36 +00:00
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 18:21:49 +00:00
|
|
|
|
impl fmt::Debug for Empty {
|
2019-03-01 08:34:11 +00:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-11-25 18:21:49 +00:00
|
|
|
|
f.pad("Empty { .. }")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-20 18:41:00 +00:00
|
|
|
|
/// A reader which yields one byte over and over and over and over and over and...
|
|
|
|
|
///
|
2020-08-18 17:36:52 +00:00
|
|
|
|
/// This struct is generally created by calling [`repeat()`]. Please
|
|
|
|
|
/// see the documentation of [`repeat()`] for more details.
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-11-27 18:29:00 +00:00
|
|
|
|
pub struct Repeat {
|
|
|
|
|
byte: u8,
|
|
|
|
|
}
|
2015-02-01 04:24:36 +00:00
|
|
|
|
|
|
|
|
|
/// Creates an instance of a reader that infinitely repeats one byte.
|
|
|
|
|
///
|
|
|
|
|
/// All reads from this reader will succeed by filling the specified buffer with
|
|
|
|
|
/// the given byte.
|
2016-06-28 01:23:53 +00:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::io::{self, Read};
|
|
|
|
|
///
|
|
|
|
|
/// let mut buffer = [0; 3];
|
|
|
|
|
/// io::repeat(0b101).read_exact(&mut buffer).unwrap();
|
|
|
|
|
/// assert_eq!(buffer, [0b101, 0b101, 0b101]);
|
|
|
|
|
/// ```
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-11-27 18:29:00 +00:00
|
|
|
|
pub fn repeat(byte: u8) -> Repeat {
|
|
|
|
|
Repeat { byte }
|
|
|
|
|
}
|
2015-02-01 04:24:36 +00:00
|
|
|
|
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-01 04:24:36 +00:00
|
|
|
|
impl Read for Repeat {
|
2017-05-15 01:29:18 +00:00
|
|
|
|
#[inline]
|
2015-02-01 04:24:36 +00:00
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
2015-06-10 16:22:20 +00:00
|
|
|
|
for slot in &mut *buf {
|
2015-02-01 04:24:36 +00:00
|
|
|
|
*slot = self.byte;
|
|
|
|
|
}
|
|
|
|
|
Ok(buf.len())
|
|
|
|
|
}
|
2017-05-15 01:29:18 +00:00
|
|
|
|
|
2019-02-08 19:42:34 +00:00
|
|
|
|
#[inline]
|
2019-04-27 15:34:08 +00:00
|
|
|
|
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
|
2019-02-08 19:42:34 +00:00
|
|
|
|
let mut nwritten = 0;
|
|
|
|
|
for buf in bufs {
|
2019-02-12 03:31:37 +00:00
|
|
|
|
nwritten += self.read(buf)?;
|
2019-02-08 19:42:34 +00:00
|
|
|
|
}
|
|
|
|
|
Ok(nwritten)
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 19:26:05 +00:00
|
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
|
fn is_read_vectored(&self) -> bool {
|
2020-01-03 19:26:05 +00:00
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-15 01:29:18 +00:00
|
|
|
|
#[inline]
|
|
|
|
|
unsafe fn initializer(&self) -> Initializer {
|
|
|
|
|
Initializer::nop()
|
|
|
|
|
}
|
2015-02-01 04:24:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 18:21:49 +00:00
|
|
|
|
impl fmt::Debug for Repeat {
|
2019-03-01 08:34:11 +00:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-11-25 18:21:49 +00:00
|
|
|
|
f.pad("Repeat { .. }")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-01 04:24:36 +00:00
|
|
|
|
/// A writer which will move data into the void.
|
2015-07-20 18:41:00 +00:00
|
|
|
|
///
|
2020-08-18 17:36:52 +00:00
|
|
|
|
/// This struct is generally created by calling [`sink`]. Please
|
|
|
|
|
/// see the documentation of [`sink()`] for more details.
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-11-27 18:29:00 +00:00
|
|
|
|
pub struct Sink {
|
|
|
|
|
_priv: (),
|
|
|
|
|
}
|
2015-02-01 04:24:36 +00:00
|
|
|
|
|
|
|
|
|
/// Creates an instance of a writer which will successfully consume all data.
|
|
|
|
|
///
|
2020-08-18 17:36:52 +00:00
|
|
|
|
/// All calls to [`write`] on the returned instance will return `Ok(buf.len())`
|
2015-02-01 04:24:36 +00:00
|
|
|
|
/// and the contents of the buffer will not be inspected.
|
2016-06-28 12:22:34 +00:00
|
|
|
|
///
|
2020-08-18 17:36:52 +00:00
|
|
|
|
/// [`write`]: Write::write
|
|
|
|
|
///
|
2016-06-28 12:22:34 +00:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
|
|
|
|
/// use std::io::{self, Write};
|
|
|
|
|
///
|
2016-07-08 01:37:31 +00:00
|
|
|
|
/// let buffer = vec![1, 2, 3, 5, 8];
|
|
|
|
|
/// let num_bytes = io::sink().write(&buffer).unwrap();
|
2016-06-28 12:22:34 +00:00
|
|
|
|
/// assert_eq!(num_bytes, 5);
|
|
|
|
|
/// ```
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-11-27 18:29:00 +00:00
|
|
|
|
pub fn sink() -> Sink {
|
|
|
|
|
Sink { _priv: () }
|
|
|
|
|
}
|
2015-02-01 04:24:36 +00:00
|
|
|
|
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-01 04:24:36 +00:00
|
|
|
|
impl Write for Sink {
|
2017-05-15 01:29:18 +00:00
|
|
|
|
#[inline]
|
2019-11-27 18:29:00 +00:00
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
|
Ok(buf.len())
|
|
|
|
|
}
|
2019-02-08 19:42:34 +00:00
|
|
|
|
|
|
|
|
|
#[inline]
|
2019-04-27 15:34:08 +00:00
|
|
|
|
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
2019-02-12 03:31:37 +00:00
|
|
|
|
let total_len = bufs.iter().map(|b| b.len()).sum();
|
2019-02-08 19:42:34 +00:00
|
|
|
|
Ok(total_len)
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 19:26:05 +00:00
|
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
|
fn is_write_vectored(&self) -> bool {
|
2020-01-03 19:26:05 +00:00
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-15 01:29:18 +00:00
|
|
|
|
#[inline]
|
2019-11-27 18:29:00 +00:00
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2015-02-01 04:24:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 18:21:49 +00:00
|
|
|
|
impl fmt::Debug for Sink {
|
2019-03-01 08:34:11 +00:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-11-25 18:21:49 +00:00
|
|
|
|
f.pad("Sink { .. }")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-01 04:24:36 +00:00
|
|
|
|
#[cfg(test)]
|
2015-04-24 15:30:41 +00:00
|
|
|
|
mod tests {
|
2019-02-10 19:23:21 +00:00
|
|
|
|
use crate::io::prelude::*;
|
2019-11-27 18:29:00 +00:00
|
|
|
|
use crate::io::{copy, empty, repeat, sink};
|
2015-08-05 11:31:05 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn copy_copies() {
|
|
|
|
|
let mut r = repeat(0).take(4);
|
|
|
|
|
let mut w = sink();
|
|
|
|
|
assert_eq!(copy(&mut r, &mut w).unwrap(), 4);
|
|
|
|
|
|
|
|
|
|
let mut r = repeat(0).take(1 << 17);
|
2018-07-11 07:11:39 +00:00
|
|
|
|
assert_eq!(copy(&mut r as &mut dyn Read, &mut w as &mut dyn Write).unwrap(), 1 << 17);
|
2015-08-05 11:31:05 +00:00
|
|
|
|
}
|
2015-02-01 04:24:36 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn sink_sinks() {
|
|
|
|
|
let mut s = sink();
|
2015-03-31 23:20:09 +00:00
|
|
|
|
assert_eq!(s.write(&[]).unwrap(), 0);
|
|
|
|
|
assert_eq!(s.write(&[0]).unwrap(), 1);
|
|
|
|
|
assert_eq!(s.write(&[0; 1024]).unwrap(), 1024);
|
|
|
|
|
assert_eq!(s.by_ref().write(&[0; 1024]).unwrap(), 1024);
|
2015-02-01 04:24:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn empty_reads() {
|
|
|
|
|
let mut e = empty();
|
2015-03-31 23:20:09 +00:00
|
|
|
|
assert_eq!(e.read(&mut []).unwrap(), 0);
|
|
|
|
|
assert_eq!(e.read(&mut [0]).unwrap(), 0);
|
|
|
|
|
assert_eq!(e.read(&mut [0; 1024]).unwrap(), 0);
|
|
|
|
|
assert_eq!(e.by_ref().read(&mut [0; 1024]).unwrap(), 0);
|
2015-02-01 04:24:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn repeat_repeats() {
|
|
|
|
|
let mut r = repeat(4);
|
|
|
|
|
let mut b = [0; 1024];
|
2015-03-31 23:20:09 +00:00
|
|
|
|
assert_eq!(r.read(&mut b).unwrap(), 1024);
|
2015-02-01 04:24:36 +00:00
|
|
|
|
assert!(b.iter().all(|b| *b == 4));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn take_some_bytes() {
|
|
|
|
|
assert_eq!(repeat(4).take(100).bytes().count(), 100);
|
2015-03-31 23:20:09 +00:00
|
|
|
|
assert_eq!(repeat(4).take(100).bytes().next().unwrap().unwrap(), 4);
|
2015-02-01 04:24:36 +00:00
|
|
|
|
assert_eq!(repeat(1).take(10).chain(repeat(2).take(10)).bytes().count(), 20);
|
|
|
|
|
}
|
|
|
|
|
}
|