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::{
|
2022-06-07 07:43:54 +00:00
|
|
|
self, BorrowedCursor, BufRead, IoSlice, IoSliceMut, Read, Seek, SeekFrom, SizeHint, Write,
|
2021-01-31 13:52:57 +00:00
|
|
|
};
|
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
|
|
|
///
|
Apply 16 commits (squashed)
----------
Fix spacing for links inside code blocks, and improve link tooltips in alloc::fmt
----------
Fix spacing for links inside code blocks, and improve link tooltips in alloc::{rc, sync}
----------
Fix spacing for links inside code blocks, and improve link tooltips in alloc::string
----------
Fix spacing for links inside code blocks in alloc::vec
----------
Fix spacing for links inside code blocks in core::option
----------
Fix spacing for links inside code blocks, and improve a few link tooltips in core::result
----------
Fix spacing for links inside code blocks in core::{iter::{self, iterator}, stream::stream, poll}
----------
Fix spacing for links inside code blocks, and improve a few link tooltips in std::{fs, path}
----------
Fix spacing for links inside code blocks in std::{collections, time}
----------
Fix spacing for links inside code blocks in and make formatting of `&str`-like types consistent in std::ffi::{c_str, os_str}
----------
Fix spacing for links inside code blocks, and improve link tooltips in std::ffi
----------
Fix spacing for links inside code blocks, and improve a few link tooltips
in std::{io::{self, buffered::{bufreader, bufwriter}, cursor, util}, net::{self, addr}}
----------
Fix typo in link to `into` for `OsString` docs
----------
Remove tooltips that will probably become redundant in the future
----------
Apply suggestions from code review
Replacing `…std/primitive.reference.html` paths with just `reference`
Co-authored-by: Joshua Nelson <github@jyn.dev>
----------
Also replace `…std/primitive.reference.html` paths with just `reference` in `core::pin`
2021-08-25 09:45:08 +00:00
|
|
|
/// All reads from the returned reader will return <code>[Ok]\(0)</code>.
|
2017-09-19 16:10:38 +00:00
|
|
|
///
|
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
|
|
|
/// ```
|
2021-10-31 02:58:27 +00:00
|
|
|
#[must_use]
|
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]
|
2022-08-11 14:52:29 +00:00
|
|
|
fn read_buf(&mut self, _cursor: BorrowedCursor<'_>) -> io::Result<()> {
|
2021-01-18 06:28:18 +00:00
|
|
|
Ok(())
|
2017-05-15 01:29:18 +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")]
|
|
|
|
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]);
|
|
|
|
/// ```
|
2021-10-31 02:58:27 +00:00
|
|
|
#[must_use]
|
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
|
|
|
|
2022-08-11 14:52:29 +00:00
|
|
|
fn read_buf(&mut self, mut buf: BorrowedCursor<'_>) -> io::Result<()> {
|
2021-06-29 22:13:28 +00:00
|
|
|
// SAFETY: No uninit bytes are being written
|
2022-05-13 14:06:36 +00:00
|
|
|
for slot in unsafe { buf.as_mut() } {
|
2021-01-18 06:28:18 +00:00
|
|
|
slot.write(self.byte);
|
|
|
|
}
|
|
|
|
|
2022-05-13 14:06:36 +00:00
|
|
|
let remaining = buf.capacity();
|
2021-01-18 06:28:18 +00:00
|
|
|
|
2021-06-29 22:13:28 +00:00
|
|
|
// SAFETY: the entire unfilled portion of buf has been initialized
|
2021-01-18 06:28:18 +00:00
|
|
|
unsafe {
|
2022-05-13 14:06:36 +00:00
|
|
|
buf.advance(remaining);
|
2021-01-18 06:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
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);
|
|
|
|
/// ```
|
2021-10-31 02:58:27 +00:00
|
|
|
#[must_use]
|
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
|
|
|
}
|
|
|
|
}
|