2015-01-31 20:24:36 -08:00
|
|
|
#![allow(missing_copy_implementations)]
|
|
|
|
|
|
2020-08-27 13:45:01 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests;
|
|
|
|
|
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::fmt;
|
2020-10-17 12:08:59 +02:00
|
|
|
use crate::io::{self, BufRead, Initializer, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write};
|
2015-01-31 20:24:36 -08:00
|
|
|
|
|
|
|
|
/// A reader which is always at EOF.
|
2015-07-20 14:41:00 -04:00
|
|
|
///
|
2020-08-18 19:36:52 +02: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 14:49:03 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-11-27 10:29:00 -08:00
|
|
|
pub struct Empty {
|
|
|
|
|
_priv: (),
|
|
|
|
|
}
|
2015-01-31 20:24:36 -08:00
|
|
|
|
2015-07-08 18:31:08 -04:00
|
|
|
/// Constructs a new handle to an empty reader.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
2017-09-19 18:10:38 +02:00
|
|
|
/// All reads from the returned reader will return [`Ok`]`(0)`.
|
|
|
|
|
///
|
2015-07-08 18:31:08 -04:00
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// A slightly sad example of not reading anything into a buffer:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
2016-06-27 21:13:26 -04:00
|
|
|
/// use std::io::{self, Read};
|
2015-07-08 18:31:08 -04:00
|
|
|
///
|
|
|
|
|
/// let mut buffer = String::new();
|
2016-06-27 21:13:26 -04:00
|
|
|
/// io::empty().read_to_string(&mut buffer).unwrap();
|
|
|
|
|
/// assert!(buffer.is_empty());
|
2015-07-08 18:31:08 -04: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 14:49:03 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2020-11-06 18:04:52 +01:00
|
|
|
#[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
|
2020-11-06 17:46:56 +01:00
|
|
|
pub const fn empty() -> Empty {
|
2019-11-27 10:29:00 -08:00
|
|
|
Empty { _priv: () }
|
|
|
|
|
}
|
2015-01-31 20:24:36 -08: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 14:49:03 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-31 20:24:36 -08:00
|
|
|
impl Read for Empty {
|
2017-05-14 21:29:18 -04:00
|
|
|
#[inline]
|
2019-11-27 10:29:00 -08:00
|
|
|
fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
|
Ok(0)
|
|
|
|
|
}
|
2017-05-14 21:29:18 -04:00
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
unsafe fn initializer(&self) -> Initializer {
|
|
|
|
|
Initializer::nop()
|
|
|
|
|
}
|
2015-01-31 20:24:36 -08: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 14:49:03 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl BufRead for Empty {
|
2017-05-14 21:29:18 -04:00
|
|
|
#[inline]
|
2019-11-27 10:29:00 -08:00
|
|
|
fn fill_buf(&mut self) -> io::Result<&[u8]> {
|
|
|
|
|
Ok(&[])
|
|
|
|
|
}
|
2017-05-14 21:29:18 -04: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 14:49:03 -08:00
|
|
|
fn consume(&mut self, _n: usize) {}
|
|
|
|
|
}
|
2015-01-31 20:24:36 -08:00
|
|
|
|
2020-10-17 12:08:59 +02: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 13:21:49 -05:00
|
|
|
impl fmt::Debug for Empty {
|
2019-03-01 09:34:11 +01:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-11-25 13:21:49 -05:00
|
|
|
f.pad("Empty { .. }")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-20 14:41:00 -04:00
|
|
|
/// A reader which yields one byte over and over and over and over and over and...
|
|
|
|
|
///
|
2020-08-18 19:36:52 +02: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 14:49:03 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-11-27 10:29:00 -08:00
|
|
|
pub struct Repeat {
|
|
|
|
|
byte: u8,
|
|
|
|
|
}
|
2015-01-31 20:24:36 -08: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-27 21:23:53 -04: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 14:49:03 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2020-11-06 18:04:52 +01:00
|
|
|
#[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
|
2020-11-06 17:46:56 +01:00
|
|
|
pub const fn repeat(byte: u8) -> Repeat {
|
2019-11-27 10:29:00 -08:00
|
|
|
Repeat { byte }
|
|
|
|
|
}
|
2015-01-31 20:24:36 -08: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 14:49:03 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-31 20:24:36 -08:00
|
|
|
impl Read for Repeat {
|
2017-05-14 21:29:18 -04:00
|
|
|
#[inline]
|
2015-01-31 20:24:36 -08:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
2015-06-10 17:22:20 +01:00
|
|
|
for slot in &mut *buf {
|
2015-01-31 20:24:36 -08:00
|
|
|
*slot = self.byte;
|
|
|
|
|
}
|
|
|
|
|
Ok(buf.len())
|
|
|
|
|
}
|
2017-05-14 21:29:18 -04:00
|
|
|
|
2019-02-08 20:42:34 +01:00
|
|
|
#[inline]
|
2019-04-27 08:34:08 -07:00
|
|
|
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
|
2019-02-08 20:42:34 +01:00
|
|
|
let mut nwritten = 0;
|
|
|
|
|
for buf in bufs {
|
2019-02-11 19:31:37 -08:00
|
|
|
nwritten += self.read(buf)?;
|
2019-02-08 20:42:34 +01:00
|
|
|
}
|
|
|
|
|
Ok(nwritten)
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 11:26:05 -08:00
|
|
|
#[inline]
|
2020-03-11 18:02:52 -07:00
|
|
|
fn is_read_vectored(&self) -> bool {
|
2020-01-03 11:26:05 -08:00
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-14 21:29:18 -04:00
|
|
|
#[inline]
|
|
|
|
|
unsafe fn initializer(&self) -> Initializer {
|
|
|
|
|
Initializer::nop()
|
|
|
|
|
}
|
2015-01-31 20:24:36 -08:00
|
|
|
}
|
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 13:21:49 -05:00
|
|
|
impl fmt::Debug for Repeat {
|
2019-03-01 09:34:11 +01:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-11-25 13:21:49 -05:00
|
|
|
f.pad("Repeat { .. }")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-31 20:24:36 -08:00
|
|
|
/// A writer which will move data into the void.
|
2015-07-20 14:41:00 -04:00
|
|
|
///
|
2020-08-18 19:36:52 +02: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 14:49:03 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-11-27 10:29:00 -08:00
|
|
|
pub struct Sink {
|
|
|
|
|
_priv: (),
|
|
|
|
|
}
|
2015-01-31 20:24:36 -08:00
|
|
|
|
|
|
|
|
/// Creates an instance of a writer which will successfully consume all data.
|
|
|
|
|
///
|
2020-08-18 19:36:52 +02:00
|
|
|
/// All calls to [`write`] on the returned instance will return `Ok(buf.len())`
|
2015-01-31 20:24:36 -08:00
|
|
|
/// and the contents of the buffer will not be inspected.
|
2016-06-28 08:22:34 -04:00
|
|
|
///
|
2020-08-18 19:36:52 +02:00
|
|
|
/// [`write`]: Write::write
|
|
|
|
|
///
|
2016-06-28 08:22:34 -04:00
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
|
|
|
|
/// use std::io::{self, Write};
|
|
|
|
|
///
|
2016-07-07 21:37:31 -04:00
|
|
|
/// let buffer = vec![1, 2, 3, 5, 8];
|
|
|
|
|
/// let num_bytes = io::sink().write(&buffer).unwrap();
|
2016-06-28 08:22:34 -04: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 14:49:03 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2020-11-06 18:04:52 +01:00
|
|
|
#[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
|
2020-11-06 17:46:56 +01:00
|
|
|
pub const fn sink() -> Sink {
|
2019-11-27 10:29:00 -08:00
|
|
|
Sink { _priv: () }
|
|
|
|
|
}
|
2015-01-31 20:24:36 -08: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 14:49:03 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-31 20:24:36 -08:00
|
|
|
impl Write for Sink {
|
2017-05-14 21:29:18 -04:00
|
|
|
#[inline]
|
2019-11-27 10:29:00 -08:00
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
|
Ok(buf.len())
|
|
|
|
|
}
|
2019-02-08 20:42:34 +01:00
|
|
|
|
|
|
|
|
#[inline]
|
2019-04-27 08:34:08 -07:00
|
|
|
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
2019-02-11 19:31:37 -08:00
|
|
|
let total_len = bufs.iter().map(|b| b.len()).sum();
|
2019-02-08 20:42:34 +01:00
|
|
|
Ok(total_len)
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 11:26:05 -08:00
|
|
|
#[inline]
|
2020-03-11 18:02:52 -07:00
|
|
|
fn is_write_vectored(&self) -> bool {
|
2020-01-03 11:26:05 -08:00
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-14 21:29:18 -04:00
|
|
|
#[inline]
|
2019-11-27 10:29:00 -08:00
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2015-01-31 20:24:36 -08:00
|
|
|
}
|
|
|
|
|
|
2020-09-21 08:52:59 +02:00
|
|
|
#[stable(feature = "write_mt", since = "1.48.0")]
|
2020-09-02 23:25:09 +02:00
|
|
|
impl Write for &Sink {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
2019-11-27 10:29:00 -08:00
|
|
|
Ok(buf.len())
|
|
|
|
|
}
|
2019-02-08 20:42:34 +01:00
|
|
|
|
|
|
|
|
#[inline]
|
2019-04-27 08:34:08 -07:00
|
|
|
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
2019-02-11 19:31:37 -08:00
|
|
|
let total_len = bufs.iter().map(|b| b.len()).sum();
|
2019-02-08 20:42:34 +01:00
|
|
|
Ok(total_len)
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 11:26:05 -08:00
|
|
|
#[inline]
|
2020-03-11 18:02:52 -07:00
|
|
|
fn is_write_vectored(&self) -> bool {
|
2020-01-03 11:26:05 -08:00
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-14 21:29:18 -04:00
|
|
|
#[inline]
|
2019-11-27 10:29:00 -08:00
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2015-01-31 20:24:36 -08:00
|
|
|
}
|
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 13:21:49 -05:00
|
|
|
impl fmt::Debug for Sink {
|
2019-03-01 09:34:11 +01:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-11-25 13:21:49 -05:00
|
|
|
f.pad("Sink { .. }")
|
|
|
|
|
}
|
|
|
|
|
}
|