2015-02-01 04:24:36 +00:00
|
|
|
#![allow(missing_copy_implementations)]
|
|
|
|
|
2020-08-27 13:45:01 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::fmt;
|
2021-01-31 13:52:57 +00:00
|
|
|
use crate::io::{
|
|
|
|
self, BufRead, Initializer, IoSlice, IoSliceMut, Read, Seek, SeekFrom, SizeHint, Write,
|
|
|
|
};
|
2015-02-01 04:24:36 +00:00
|
|
|
|
|
|
|
/// 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")]
|
2021-06-24 08:16:11 +00:00
|
|
|
#[non_exhaustive]
|
2021-06-30 10:14:59 +00:00
|
|
|
#[derive(Copy, Clone, Default)]
|
2021-06-24 08:16:11 +00:00
|
|
|
pub struct Empty;
|
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")]
|
2020-11-06 17:04:52 +00:00
|
|
|
#[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
|
2020-11-06 16:46:56 +00:00
|
|
|
pub const fn empty() -> Empty {
|
2021-06-24 08:16:11 +00:00
|
|
|
Empty
|
2019-11-27 18:29:00 +00:00
|
|
|
}
|
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
|
|
|
|
2020-10-17 10:08:59 +00:00
|
|
|
#[stable(feature = "empty_seek", since = "1.51.0")]
|
|
|
|
impl Seek for Empty {
|
|
|
|
fn seek(&mut self, _pos: SeekFrom) -> io::Result<u64> {
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stream_len(&mut self) -> io::Result<u64> {
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stream_position(&mut self) -> io::Result<u64> {
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-04-05 11:31:11 +00:00
|
|
|
f.debug_struct("Empty").finish_non_exhaustive()
|
2016-11-25 18:21:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-16 20:03:58 +00:00
|
|
|
impl SizeHint for Empty {
|
2021-06-10 17:09:04 +00:00
|
|
|
#[inline]
|
2021-01-16 20:03:58 +00:00
|
|
|
fn upper_bound(&self) -> Option<usize> {
|
2021-01-16 20:36:59 +00:00
|
|
|
Some(0)
|
2021-01-16 20:03:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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")]
|
2020-11-06 17:04:52 +00:00
|
|
|
#[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
|
2020-11-06 16:46:56 +00:00
|
|
|
pub const fn repeat(byte: u8) -> Repeat {
|
2019-11-27 18:29:00 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-06-10 17:09:04 +00:00
|
|
|
impl SizeHint for Repeat {
|
|
|
|
#[inline]
|
|
|
|
fn lower_bound(&self) -> usize {
|
|
|
|
usize::MAX
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn upper_bound(&self) -> Option<usize> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-04-05 11:31:11 +00:00
|
|
|
f.debug_struct("Repeat").finish_non_exhaustive()
|
2016-11-25 18:21:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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")]
|
2021-06-24 08:16:11 +00:00
|
|
|
#[non_exhaustive]
|
2021-06-30 10:14:59 +00:00
|
|
|
#[derive(Copy, Clone, Default)]
|
2021-06-24 08:16:11 +00:00
|
|
|
pub struct Sink;
|
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")]
|
2020-11-06 17:04:52 +00:00
|
|
|
#[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
|
2020-11-06 16:46:56 +00:00
|
|
|
pub const fn sink() -> Sink {
|
2021-06-24 08:16:11 +00:00
|
|
|
Sink
|
2019-11-27 18:29:00 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2020-09-21 06:52:59 +00:00
|
|
|
#[stable(feature = "write_mt", since = "1.48.0")]
|
2020-09-02 21:25:09 +00:00
|
|
|
impl Write for &Sink {
|
|
|
|
#[inline]
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
2019-11-27 18:29:00 +00:00
|
|
|
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 {
|
2021-04-05 11:31:11 +00:00
|
|
|
f.debug_struct("Sink").finish_non_exhaustive()
|
2016-11-25 18:21:49 +00:00
|
|
|
}
|
|
|
|
}
|