summaryrefslogtreecommitdiffstats
path: root/library/std/src/io
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /library/std/src/io
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/std/src/io')
-rw-r--r--library/std/src/io/buffered/bufreader.rs496
-rw-r--r--library/std/src/io/buffered/bufreader/buffer.rs105
-rw-r--r--library/std/src/io/buffered/bufwriter.rs674
-rw-r--r--library/std/src/io/buffered/linewriter.rs232
-rw-r--r--library/std/src/io/buffered/linewritershim.rs276
-rw-r--r--library/std/src/io/buffered/mod.rs196
-rw-r--r--library/std/src/io/buffered/tests.rs1039
-rw-r--r--library/std/src/io/copy.rs161
-rw-r--r--library/std/src/io/cursor.rs640
-rw-r--r--library/std/src/io/cursor/tests.rs567
-rw-r--r--library/std/src/io/error.rs960
-rw-r--r--library/std/src/io/error/repr_bitpacked.rs409
-rw-r--r--library/std/src/io/error/repr_unpacked.rs54
-rw-r--r--library/std/src/io/error/tests.rs194
-rw-r--r--library/std/src/io/impls.rs458
-rw-r--r--library/std/src/io/impls/tests.rs57
-rw-r--r--library/std/src/io/mod.rs2827
-rw-r--r--library/std/src/io/prelude.rs14
-rw-r--r--library/std/src/io/readbuf.rs249
-rw-r--r--library/std/src/io/readbuf/tests.rs181
-rw-r--r--library/std/src/io/stdio.rs1042
-rw-r--r--library/std/src/io/stdio/tests.rs166
-rw-r--r--library/std/src/io/tests.rs623
-rw-r--r--library/std/src/io/util.rs270
-rw-r--r--library/std/src/io/util/tests.rs147
25 files changed, 12037 insertions, 0 deletions
diff --git a/library/std/src/io/buffered/bufreader.rs b/library/std/src/io/buffered/bufreader.rs
new file mode 100644
index 000000000..f7fbaa9c2
--- /dev/null
+++ b/library/std/src/io/buffered/bufreader.rs
@@ -0,0 +1,496 @@
+mod buffer;
+
+use crate::fmt;
+use crate::io::{
+ self, BufRead, IoSliceMut, Read, ReadBuf, Seek, SeekFrom, SizeHint, DEFAULT_BUF_SIZE,
+};
+use buffer::Buffer;
+
+/// The `BufReader<R>` struct adds buffering to any reader.
+///
+/// It can be excessively inefficient to work directly with a [`Read`] instance.
+/// For example, every call to [`read`][`TcpStream::read`] on [`TcpStream`]
+/// results in a system call. A `BufReader<R>` performs large, infrequent reads on
+/// the underlying [`Read`] and maintains an in-memory buffer of the results.
+///
+/// `BufReader<R>` can improve the speed of programs that make *small* and
+/// *repeated* read calls to the same file or network socket. It does not
+/// help when reading very large amounts at once, or reading just one or a few
+/// times. It also provides no advantage when reading from a source that is
+/// already in memory, like a <code>[Vec]\<u8></code>.
+///
+/// When the `BufReader<R>` is dropped, the contents of its buffer will be
+/// discarded. Creating multiple instances of a `BufReader<R>` on the same
+/// stream can cause data loss. Reading from the underlying reader after
+/// unwrapping the `BufReader<R>` with [`BufReader::into_inner`] can also cause
+/// data loss.
+///
+// HACK(#78696): can't use `crate` for associated items
+/// [`TcpStream::read`]: super::super::super::net::TcpStream::read
+/// [`TcpStream`]: crate::net::TcpStream
+///
+/// # Examples
+///
+/// ```no_run
+/// use std::io::prelude::*;
+/// use std::io::BufReader;
+/// use std::fs::File;
+///
+/// fn main() -> std::io::Result<()> {
+/// let f = File::open("log.txt")?;
+/// let mut reader = BufReader::new(f);
+///
+/// let mut line = String::new();
+/// let len = reader.read_line(&mut line)?;
+/// println!("First line is {len} bytes long");
+/// Ok(())
+/// }
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct BufReader<R> {
+ inner: R,
+ buf: Buffer,
+}
+
+impl<R: Read> BufReader<R> {
+ /// Creates a new `BufReader<R>` with a default buffer capacity. The default is currently 8 KB,
+ /// but may change in the future.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::BufReader;
+ /// use std::fs::File;
+ ///
+ /// fn main() -> std::io::Result<()> {
+ /// let f = File::open("log.txt")?;
+ /// let reader = BufReader::new(f);
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn new(inner: R) -> BufReader<R> {
+ BufReader::with_capacity(DEFAULT_BUF_SIZE, inner)
+ }
+
+ /// Creates a new `BufReader<R>` with the specified buffer capacity.
+ ///
+ /// # Examples
+ ///
+ /// Creating a buffer with ten bytes of capacity:
+ ///
+ /// ```no_run
+ /// use std::io::BufReader;
+ /// use std::fs::File;
+ ///
+ /// fn main() -> std::io::Result<()> {
+ /// let f = File::open("log.txt")?;
+ /// let reader = BufReader::with_capacity(10, f);
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn with_capacity(capacity: usize, inner: R) -> BufReader<R> {
+ BufReader { inner, buf: Buffer::with_capacity(capacity) }
+ }
+}
+
+impl<R> BufReader<R> {
+ /// Gets a reference to the underlying reader.
+ ///
+ /// It is inadvisable to directly read from the underlying reader.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::BufReader;
+ /// use std::fs::File;
+ ///
+ /// fn main() -> std::io::Result<()> {
+ /// let f1 = File::open("log.txt")?;
+ /// let reader = BufReader::new(f1);
+ ///
+ /// let f2 = reader.get_ref();
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn get_ref(&self) -> &R {
+ &self.inner
+ }
+
+ /// Gets a mutable reference to the underlying reader.
+ ///
+ /// It is inadvisable to directly read from the underlying reader.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::BufReader;
+ /// use std::fs::File;
+ ///
+ /// fn main() -> std::io::Result<()> {
+ /// let f1 = File::open("log.txt")?;
+ /// let mut reader = BufReader::new(f1);
+ ///
+ /// let f2 = reader.get_mut();
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn get_mut(&mut self) -> &mut R {
+ &mut self.inner
+ }
+
+ /// Returns a reference to the internally buffered data.
+ ///
+ /// Unlike [`fill_buf`], this will not attempt to fill the buffer if it is empty.
+ ///
+ /// [`fill_buf`]: BufRead::fill_buf
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::{BufReader, BufRead};
+ /// use std::fs::File;
+ ///
+ /// fn main() -> std::io::Result<()> {
+ /// let f = File::open("log.txt")?;
+ /// let mut reader = BufReader::new(f);
+ /// assert!(reader.buffer().is_empty());
+ ///
+ /// if reader.fill_buf()?.len() > 0 {
+ /// assert!(!reader.buffer().is_empty());
+ /// }
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "bufreader_buffer", since = "1.37.0")]
+ pub fn buffer(&self) -> &[u8] {
+ self.buf.buffer()
+ }
+
+ /// Returns the number of bytes the internal buffer can hold at once.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::{BufReader, BufRead};
+ /// use std::fs::File;
+ ///
+ /// fn main() -> std::io::Result<()> {
+ /// let f = File::open("log.txt")?;
+ /// let mut reader = BufReader::new(f);
+ ///
+ /// let capacity = reader.capacity();
+ /// let buffer = reader.fill_buf()?;
+ /// assert!(buffer.len() <= capacity);
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "buffered_io_capacity", since = "1.46.0")]
+ pub fn capacity(&self) -> usize {
+ self.buf.capacity()
+ }
+
+ /// Unwraps this `BufReader<R>`, returning the underlying reader.
+ ///
+ /// Note that any leftover data in the internal buffer is lost. Therefore,
+ /// a following read from the underlying reader may lead to data loss.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::BufReader;
+ /// use std::fs::File;
+ ///
+ /// fn main() -> std::io::Result<()> {
+ /// let f1 = File::open("log.txt")?;
+ /// let reader = BufReader::new(f1);
+ ///
+ /// let f2 = reader.into_inner();
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn into_inner(self) -> R {
+ self.inner
+ }
+
+ /// Invalidates all data in the internal buffer.
+ #[inline]
+ fn discard_buffer(&mut self) {
+ self.buf.discard_buffer()
+ }
+}
+
+impl<R: Seek> BufReader<R> {
+ /// Seeks relative to the current position. If the new position lies within the buffer,
+ /// the buffer will not be flushed, allowing for more efficient seeks.
+ /// This method does not return the location of the underlying reader, so the caller
+ /// must track this information themselves if it is required.
+ #[stable(feature = "bufreader_seek_relative", since = "1.53.0")]
+ pub fn seek_relative(&mut self, offset: i64) -> io::Result<()> {
+ let pos = self.buf.pos() as u64;
+ if offset < 0 {
+ if let Some(_) = pos.checked_sub((-offset) as u64) {
+ self.buf.unconsume((-offset) as usize);
+ return Ok(());
+ }
+ } else if let Some(new_pos) = pos.checked_add(offset as u64) {
+ if new_pos <= self.buf.filled() as u64 {
+ self.buf.consume(offset as usize);
+ return Ok(());
+ }
+ }
+
+ self.seek(SeekFrom::Current(offset)).map(drop)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<R: Read> Read for BufReader<R> {
+ fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+ // If we don't have any buffered data and we're doing a massive read
+ // (larger than our internal buffer), bypass our internal buffer
+ // entirely.
+ if self.buf.pos() == self.buf.filled() && buf.len() >= self.capacity() {
+ self.discard_buffer();
+ return self.inner.read(buf);
+ }
+ let nread = {
+ let mut rem = self.fill_buf()?;
+ rem.read(buf)?
+ };
+ self.consume(nread);
+ Ok(nread)
+ }
+
+ fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> {
+ // If we don't have any buffered data and we're doing a massive read
+ // (larger than our internal buffer), bypass our internal buffer
+ // entirely.
+ if self.buf.pos() == self.buf.filled() && buf.remaining() >= self.capacity() {
+ self.discard_buffer();
+ return self.inner.read_buf(buf);
+ }
+
+ let prev = buf.filled_len();
+
+ let mut rem = self.fill_buf()?;
+ rem.read_buf(buf)?;
+
+ self.consume(buf.filled_len() - prev); //slice impl of read_buf known to never unfill buf
+
+ Ok(())
+ }
+
+ // Small read_exacts from a BufReader are extremely common when used with a deserializer.
+ // The default implementation calls read in a loop, which results in surprisingly poor code
+ // generation for the common path where the buffer has enough bytes to fill the passed-in
+ // buffer.
+ fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
+ if self.buf.consume_with(buf.len(), |claimed| buf.copy_from_slice(claimed)) {
+ return Ok(());
+ }
+
+ crate::io::default_read_exact(self, buf)
+ }
+
+ fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
+ let total_len = bufs.iter().map(|b| b.len()).sum::<usize>();
+ if self.buf.pos() == self.buf.filled() && total_len >= self.capacity() {
+ self.discard_buffer();
+ return self.inner.read_vectored(bufs);
+ }
+ let nread = {
+ let mut rem = self.fill_buf()?;
+ rem.read_vectored(bufs)?
+ };
+ self.consume(nread);
+ Ok(nread)
+ }
+
+ fn is_read_vectored(&self) -> bool {
+ self.inner.is_read_vectored()
+ }
+
+ // The inner reader might have an optimized `read_to_end`. Drain our buffer and then
+ // delegate to the inner implementation.
+ fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
+ let inner_buf = self.buffer();
+ buf.extend_from_slice(inner_buf);
+ let nread = inner_buf.len();
+ self.discard_buffer();
+ Ok(nread + self.inner.read_to_end(buf)?)
+ }
+
+ // The inner reader might have an optimized `read_to_end`. Drain our buffer and then
+ // delegate to the inner implementation.
+ fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
+ // In the general `else` case below we must read bytes into a side buffer, check
+ // that they are valid UTF-8, and then append them to `buf`. This requires a
+ // potentially large memcpy.
+ //
+ // If `buf` is empty--the most common case--we can leverage `append_to_string`
+ // to read directly into `buf`'s internal byte buffer, saving an allocation and
+ // a memcpy.
+ if buf.is_empty() {
+ // `append_to_string`'s safety relies on the buffer only being appended to since
+ // it only checks the UTF-8 validity of new data. If there were existing content in
+ // `buf` then an untrustworthy reader (i.e. `self.inner`) could not only append
+ // bytes but also modify existing bytes and render them invalid. On the other hand,
+ // if `buf` is empty then by definition any writes must be appends and
+ // `append_to_string` will validate all of the new bytes.
+ unsafe { crate::io::append_to_string(buf, |b| self.read_to_end(b)) }
+ } else {
+ // We cannot append our byte buffer directly onto the `buf` String as there could
+ // be an incomplete UTF-8 sequence that has only been partially read. We must read
+ // everything into a side buffer first and then call `from_utf8` on the complete
+ // buffer.
+ let mut bytes = Vec::new();
+ self.read_to_end(&mut bytes)?;
+ let string = crate::str::from_utf8(&bytes).map_err(|_| {
+ io::const_io_error!(
+ io::ErrorKind::InvalidData,
+ "stream did not contain valid UTF-8",
+ )
+ })?;
+ *buf += string;
+ Ok(string.len())
+ }
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<R: Read> BufRead for BufReader<R> {
+ fn fill_buf(&mut self) -> io::Result<&[u8]> {
+ self.buf.fill_buf(&mut self.inner)
+ }
+
+ fn consume(&mut self, amt: usize) {
+ self.buf.consume(amt)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<R> fmt::Debug for BufReader<R>
+where
+ R: fmt::Debug,
+{
+ fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt.debug_struct("BufReader")
+ .field("reader", &self.inner)
+ .field(
+ "buffer",
+ &format_args!("{}/{}", self.buf.filled() - self.buf.pos(), self.capacity()),
+ )
+ .finish()
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<R: Seek> Seek for BufReader<R> {
+ /// Seek to an offset, in bytes, in the underlying reader.
+ ///
+ /// The position used for seeking with <code>[SeekFrom::Current]\(_)</code> is the
+ /// position the underlying reader would be at if the `BufReader<R>` had no
+ /// internal buffer.
+ ///
+ /// Seeking always discards the internal buffer, even if the seek position
+ /// would otherwise fall within it. This guarantees that calling
+ /// [`BufReader::into_inner()`] immediately after a seek yields the underlying reader
+ /// at the same position.
+ ///
+ /// To seek without discarding the internal buffer, use [`BufReader::seek_relative`].
+ ///
+ /// See [`std::io::Seek`] for more details.
+ ///
+ /// Note: In the edge case where you're seeking with <code>[SeekFrom::Current]\(n)</code>
+ /// where `n` minus the internal buffer length overflows an `i64`, two
+ /// seeks will be performed instead of one. If the second seek returns
+ /// [`Err`], the underlying reader will be left at the same position it would
+ /// have if you called `seek` with <code>[SeekFrom::Current]\(0)</code>.
+ ///
+ /// [`std::io::Seek`]: Seek
+ fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
+ let result: u64;
+ if let SeekFrom::Current(n) = pos {
+ let remainder = (self.buf.filled() - self.buf.pos()) as i64;
+ // it should be safe to assume that remainder fits within an i64 as the alternative
+ // means we managed to allocate 8 exbibytes and that's absurd.
+ // But it's not out of the realm of possibility for some weird underlying reader to
+ // support seeking by i64::MIN so we need to handle underflow when subtracting
+ // remainder.
+ if let Some(offset) = n.checked_sub(remainder) {
+ result = self.inner.seek(SeekFrom::Current(offset))?;
+ } else {
+ // seek backwards by our remainder, and then by the offset
+ self.inner.seek(SeekFrom::Current(-remainder))?;
+ self.discard_buffer();
+ result = self.inner.seek(SeekFrom::Current(n))?;
+ }
+ } else {
+ // Seeking with Start/End doesn't care about our buffer length.
+ result = self.inner.seek(pos)?;
+ }
+ self.discard_buffer();
+ Ok(result)
+ }
+
+ /// Returns the current seek position from the start of the stream.
+ ///
+ /// The value returned is equivalent to `self.seek(SeekFrom::Current(0))`
+ /// but does not flush the internal buffer. Due to this optimization the
+ /// function does not guarantee that calling `.into_inner()` immediately
+ /// afterwards will yield the underlying reader at the same position. Use
+ /// [`BufReader::seek`] instead if you require that guarantee.
+ ///
+ /// # Panics
+ ///
+ /// This function will panic if the position of the inner reader is smaller
+ /// than the amount of buffered data. That can happen if the inner reader
+ /// has an incorrect implementation of [`Seek::stream_position`], or if the
+ /// position has gone out of sync due to calling [`Seek::seek`] directly on
+ /// the underlying reader.
+ ///
+ /// # Example
+ ///
+ /// ```no_run
+ /// use std::{
+ /// io::{self, BufRead, BufReader, Seek},
+ /// fs::File,
+ /// };
+ ///
+ /// fn main() -> io::Result<()> {
+ /// let mut f = BufReader::new(File::open("foo.txt")?);
+ ///
+ /// let before = f.stream_position()?;
+ /// f.read_line(&mut String::new())?;
+ /// let after = f.stream_position()?;
+ ///
+ /// println!("The first line was {} bytes long", after - before);
+ /// Ok(())
+ /// }
+ /// ```
+ fn stream_position(&mut self) -> io::Result<u64> {
+ let remainder = (self.buf.filled() - self.buf.pos()) as u64;
+ self.inner.stream_position().map(|pos| {
+ pos.checked_sub(remainder).expect(
+ "overflow when subtracting remaining buffer size from inner stream position",
+ )
+ })
+ }
+}
+
+impl<T> SizeHint for BufReader<T> {
+ #[inline]
+ fn lower_bound(&self) -> usize {
+ SizeHint::lower_bound(self.get_ref()) + self.buffer().len()
+ }
+
+ #[inline]
+ fn upper_bound(&self) -> Option<usize> {
+ SizeHint::upper_bound(self.get_ref()).and_then(|up| self.buffer().len().checked_add(up))
+ }
+}
diff --git a/library/std/src/io/buffered/bufreader/buffer.rs b/library/std/src/io/buffered/bufreader/buffer.rs
new file mode 100644
index 000000000..8ae01f3b0
--- /dev/null
+++ b/library/std/src/io/buffered/bufreader/buffer.rs
@@ -0,0 +1,105 @@
+///! An encapsulation of `BufReader`'s buffer management logic.
+///
+/// This module factors out the basic functionality of `BufReader` in order to protect two core
+/// invariants:
+/// * `filled` bytes of `buf` are always initialized
+/// * `pos` is always <= `filled`
+/// Since this module encapsulates the buffer management logic, we can ensure that the range
+/// `pos..filled` is always a valid index into the initialized region of the buffer. This means
+/// that user code which wants to do reads from a `BufReader` via `buffer` + `consume` can do so
+/// without encountering any runtime bounds checks.
+use crate::cmp;
+use crate::io::{self, Read, ReadBuf};
+use crate::mem::MaybeUninit;
+
+pub struct Buffer {
+ // The buffer.
+ buf: Box<[MaybeUninit<u8>]>,
+ // The current seek offset into `buf`, must always be <= `filled`.
+ pos: usize,
+ // Each call to `fill_buf` sets `filled` to indicate how many bytes at the start of `buf` are
+ // initialized with bytes from a read.
+ filled: usize,
+}
+
+impl Buffer {
+ #[inline]
+ pub fn with_capacity(capacity: usize) -> Self {
+ let buf = Box::new_uninit_slice(capacity);
+ Self { buf, pos: 0, filled: 0 }
+ }
+
+ #[inline]
+ pub fn buffer(&self) -> &[u8] {
+ // SAFETY: self.pos and self.cap are valid, and self.cap => self.pos, and
+ // that region is initialized because those are all invariants of this type.
+ unsafe { MaybeUninit::slice_assume_init_ref(self.buf.get_unchecked(self.pos..self.filled)) }
+ }
+
+ #[inline]
+ pub fn capacity(&self) -> usize {
+ self.buf.len()
+ }
+
+ #[inline]
+ pub fn filled(&self) -> usize {
+ self.filled
+ }
+
+ #[inline]
+ pub fn pos(&self) -> usize {
+ self.pos
+ }
+
+ #[inline]
+ pub fn discard_buffer(&mut self) {
+ self.pos = 0;
+ self.filled = 0;
+ }
+
+ #[inline]
+ pub fn consume(&mut self, amt: usize) {
+ self.pos = cmp::min(self.pos + amt, self.filled);
+ }
+
+ /// If there are `amt` bytes available in the buffer, pass a slice containing those bytes to
+ /// `visitor` and return true. If there are not enough bytes available, return false.
+ #[inline]
+ pub fn consume_with<V>(&mut self, amt: usize, mut visitor: V) -> bool
+ where
+ V: FnMut(&[u8]),
+ {
+ if let Some(claimed) = self.buffer().get(..amt) {
+ visitor(claimed);
+ // If the indexing into self.buffer() succeeds, amt must be a valid increment.
+ self.pos += amt;
+ true
+ } else {
+ false
+ }
+ }
+
+ #[inline]
+ pub fn unconsume(&mut self, amt: usize) {
+ self.pos = self.pos.saturating_sub(amt);
+ }
+
+ #[inline]
+ pub fn fill_buf(&mut self, mut reader: impl Read) -> io::Result<&[u8]> {
+ // If we've reached the end of our internal buffer then we need to fetch
+ // some more data from the reader.
+ // Branch using `>=` instead of the more correct `==`
+ // to tell the compiler that the pos..cap slice is always valid.
+ if self.pos >= self.filled {
+ debug_assert!(self.pos == self.filled);
+
+ let mut readbuf = ReadBuf::uninit(&mut self.buf);
+
+ reader.read_buf(&mut readbuf)?;
+
+ self.filled = readbuf.filled_len();
+ self.pos = 0;
+ }
+ Ok(self.buffer())
+ }
+}
diff --git a/library/std/src/io/buffered/bufwriter.rs b/library/std/src/io/buffered/bufwriter.rs
new file mode 100644
index 000000000..6acb937e7
--- /dev/null
+++ b/library/std/src/io/buffered/bufwriter.rs
@@ -0,0 +1,674 @@
+use crate::error;
+use crate::fmt;
+use crate::io::{
+ self, ErrorKind, IntoInnerError, IoSlice, Seek, SeekFrom, Write, DEFAULT_BUF_SIZE,
+};
+use crate::mem;
+use crate::ptr;
+
+/// Wraps a writer and buffers its output.
+///
+/// It can be excessively inefficient to work directly with something that
+/// implements [`Write`]. For example, every call to
+/// [`write`][`TcpStream::write`] on [`TcpStream`] results in a system call. A
+/// `BufWriter<W>` keeps an in-memory buffer of data and writes it to an underlying
+/// writer in large, infrequent batches.
+///
+/// `BufWriter<W>` can improve the speed of programs that make *small* and
+/// *repeated* write calls to the same file or network socket. It does not
+/// help when writing very large amounts at once, or writing just one or a few
+/// times. It also provides no advantage when writing to a destination that is
+/// in memory, like a <code>[Vec]\<u8></code>.
+///
+/// It is critical to call [`flush`] before `BufWriter<W>` is dropped. Though
+/// dropping will attempt to flush the contents of the buffer, any errors
+/// that happen in the process of dropping will be ignored. Calling [`flush`]
+/// ensures that the buffer is empty and thus dropping will not even attempt
+/// file operations.
+///
+/// # Examples
+///
+/// Let's write the numbers one through ten to a [`TcpStream`]:
+///
+/// ```no_run
+/// use std::io::prelude::*;
+/// use std::net::TcpStream;
+///
+/// let mut stream = TcpStream::connect("127.0.0.1:34254").unwrap();
+///
+/// for i in 0..10 {
+/// stream.write(&[i+1]).unwrap();
+/// }
+/// ```
+///
+/// Because we're not buffering, we write each one in turn, incurring the
+/// overhead of a system call per byte written. We can fix this with a
+/// `BufWriter<W>`:
+///
+/// ```no_run
+/// use std::io::prelude::*;
+/// use std::io::BufWriter;
+/// use std::net::TcpStream;
+///
+/// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+///
+/// for i in 0..10 {
+/// stream.write(&[i+1]).unwrap();
+/// }
+/// stream.flush().unwrap();
+/// ```
+///
+/// By wrapping the stream with a `BufWriter<W>`, these ten writes are all grouped
+/// together by the buffer and will all be written out in one system call when
+/// the `stream` is flushed.
+///
+// HACK(#78696): can't use `crate` for associated items
+/// [`TcpStream::write`]: super::super::super::net::TcpStream::write
+/// [`TcpStream`]: crate::net::TcpStream
+/// [`flush`]: BufWriter::flush
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct BufWriter<W: Write> {
+ inner: W,
+ // The buffer. Avoid using this like a normal `Vec` in common code paths.
+ // That is, don't use `buf.push`, `buf.extend_from_slice`, or any other
+ // methods that require bounds checking or the like. This makes an enormous
+ // difference to performance (we may want to stop using a `Vec` entirely).
+ buf: Vec<u8>,
+ // #30888: If the inner writer panics in a call to write, we don't want to
+ // write the buffered data a second time in BufWriter's destructor. This
+ // flag tells the Drop impl if it should skip the flush.
+ panicked: bool,
+}
+
+impl<W: Write> BufWriter<W> {
+ /// Creates a new `BufWriter<W>` with a default buffer capacity. The default is currently 8 KB,
+ /// but may change in the future.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::BufWriter;
+ /// use std::net::TcpStream;
+ ///
+ /// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn new(inner: W) -> BufWriter<W> {
+ BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner)
+ }
+
+ /// Creates a new `BufWriter<W>` with at least the specified buffer capacity.
+ ///
+ /// # Examples
+ ///
+ /// Creating a buffer with a buffer of at least a hundred bytes.
+ ///
+ /// ```no_run
+ /// use std::io::BufWriter;
+ /// use std::net::TcpStream;
+ ///
+ /// let stream = TcpStream::connect("127.0.0.1:34254").unwrap();
+ /// let mut buffer = BufWriter::with_capacity(100, stream);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn with_capacity(capacity: usize, inner: W) -> BufWriter<W> {
+ BufWriter { inner, buf: Vec::with_capacity(capacity), panicked: false }
+ }
+
+ /// Send data in our local buffer into the inner writer, looping as
+ /// necessary until either it's all been sent or an error occurs.
+ ///
+ /// Because all the data in the buffer has been reported to our owner as
+ /// "successfully written" (by returning nonzero success values from
+ /// `write`), any 0-length writes from `inner` must be reported as i/o
+ /// errors from this method.
+ pub(in crate::io) fn flush_buf(&mut self) -> io::Result<()> {
+ /// Helper struct to ensure the buffer is updated after all the writes
+ /// are complete. It tracks the number of written bytes and drains them
+ /// all from the front of the buffer when dropped.
+ struct BufGuard<'a> {
+ buffer: &'a mut Vec<u8>,
+ written: usize,
+ }
+
+ impl<'a> BufGuard<'a> {
+ fn new(buffer: &'a mut Vec<u8>) -> Self {
+ Self { buffer, written: 0 }
+ }
+
+ /// The unwritten part of the buffer
+ fn remaining(&self) -> &[u8] {
+ &self.buffer[self.written..]
+ }
+
+ /// Flag some bytes as removed from the front of the buffer
+ fn consume(&mut self, amt: usize) {
+ self.written += amt;
+ }
+
+ /// true if all of the bytes have been written
+ fn done(&self) -> bool {
+ self.written >= self.buffer.len()
+ }
+ }
+
+ impl Drop for BufGuard<'_> {
+ fn drop(&mut self) {
+ if self.written > 0 {
+ self.buffer.drain(..self.written);
+ }
+ }
+ }
+
+ let mut guard = BufGuard::new(&mut self.buf);
+ while !guard.done() {
+ self.panicked = true;
+ let r = self.inner.write(guard.remaining());
+ self.panicked = false;
+
+ match r {
+ Ok(0) => {
+ return Err(io::const_io_error!(
+ ErrorKind::WriteZero,
+ "failed to write the buffered data",
+ ));
+ }
+ Ok(n) => guard.consume(n),
+ Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
+ Err(e) => return Err(e),
+ }
+ }
+ Ok(())
+ }
+
+ /// Buffer some data without flushing it, regardless of the size of the
+ /// data. Writes as much as possible without exceeding capacity. Returns
+ /// the number of bytes written.
+ pub(super) fn write_to_buf(&mut self, buf: &[u8]) -> usize {
+ let available = self.spare_capacity();
+ let amt_to_buffer = available.min(buf.len());
+
+ // SAFETY: `amt_to_buffer` is <= buffer's spare capacity by construction.
+ unsafe {
+ self.write_to_buffer_unchecked(&buf[..amt_to_buffer]);
+ }
+
+ amt_to_buffer
+ }
+
+ /// Gets a reference to the underlying writer.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::BufWriter;
+ /// use std::net::TcpStream;
+ ///
+ /// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+ ///
+ /// // we can use reference just like buffer
+ /// let reference = buffer.get_ref();
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn get_ref(&self) -> &W {
+ &self.inner
+ }
+
+ /// Gets a mutable reference to the underlying writer.
+ ///
+ /// It is inadvisable to directly write to the underlying writer.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::BufWriter;
+ /// use std::net::TcpStream;
+ ///
+ /// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+ ///
+ /// // we can use reference just like buffer
+ /// let reference = buffer.get_mut();
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn get_mut(&mut self) -> &mut W {
+ &mut self.inner
+ }
+
+ /// Returns a reference to the internally buffered data.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::BufWriter;
+ /// use std::net::TcpStream;
+ ///
+ /// let buf_writer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+ ///
+ /// // See how many bytes are currently buffered
+ /// let bytes_buffered = buf_writer.buffer().len();
+ /// ```
+ #[stable(feature = "bufreader_buffer", since = "1.37.0")]
+ pub fn buffer(&self) -> &[u8] {
+ &self.buf
+ }
+
+ /// Returns a mutable reference to the internal buffer.
+ ///
+ /// This can be used to write data directly into the buffer without triggering writers
+ /// to the underlying writer.
+ ///
+ /// That the buffer is a `Vec` is an implementation detail.
+ /// Callers should not modify the capacity as there currently is no public API to do so
+ /// and thus any capacity changes would be unexpected by the user.
+ pub(in crate::io) fn buffer_mut(&mut self) -> &mut Vec<u8> {
+ &mut self.buf
+ }
+
+ /// Returns the number of bytes the internal buffer can hold without flushing.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::BufWriter;
+ /// use std::net::TcpStream;
+ ///
+ /// let buf_writer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+ ///
+ /// // Check the capacity of the inner buffer
+ /// let capacity = buf_writer.capacity();
+ /// // Calculate how many bytes can be written without flushing
+ /// let without_flush = capacity - buf_writer.buffer().len();
+ /// ```
+ #[stable(feature = "buffered_io_capacity", since = "1.46.0")]
+ pub fn capacity(&self) -> usize {
+ self.buf.capacity()
+ }
+
+ /// Unwraps this `BufWriter<W>`, returning the underlying writer.
+ ///
+ /// The buffer is written out before returning the writer.
+ ///
+ /// # Errors
+ ///
+ /// An [`Err`] will be returned if an error occurs while flushing the buffer.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::BufWriter;
+ /// use std::net::TcpStream;
+ ///
+ /// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+ ///
+ /// // unwrap the TcpStream and flush the buffer
+ /// let stream = buffer.into_inner().unwrap();
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn into_inner(mut self) -> Result<W, IntoInnerError<BufWriter<W>>> {
+ match self.flush_buf() {
+ Err(e) => Err(IntoInnerError::new(self, e)),
+ Ok(()) => Ok(self.into_parts().0),
+ }
+ }
+
+ /// Disassembles this `BufWriter<W>`, returning the underlying writer, and any buffered but
+ /// unwritten data.
+ ///
+ /// If the underlying writer panicked, it is not known what portion of the data was written.
+ /// In this case, we return `WriterPanicked` for the buffered data (from which the buffer
+ /// contents can still be recovered).
+ ///
+ /// `into_parts` makes no attempt to flush data and cannot fail.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::{BufWriter, Write};
+ ///
+ /// let mut buffer = [0u8; 10];
+ /// let mut stream = BufWriter::new(buffer.as_mut());
+ /// write!(stream, "too much data").unwrap();
+ /// stream.flush().expect_err("it doesn't fit");
+ /// let (recovered_writer, buffered_data) = stream.into_parts();
+ /// assert_eq!(recovered_writer.len(), 0);
+ /// assert_eq!(&buffered_data.unwrap(), b"ata");
+ /// ```
+ #[stable(feature = "bufwriter_into_parts", since = "1.56.0")]
+ pub fn into_parts(mut self) -> (W, Result<Vec<u8>, WriterPanicked>) {
+ let buf = mem::take(&mut self.buf);
+ let buf = if !self.panicked { Ok(buf) } else { Err(WriterPanicked { buf }) };
+
+ // SAFETY: forget(self) prevents double dropping inner
+ let inner = unsafe { ptr::read(&mut self.inner) };
+ mem::forget(self);
+
+ (inner, buf)
+ }
+
+ // Ensure this function does not get inlined into `write`, so that it
+ // remains inlineable and its common path remains as short as possible.
+ // If this function ends up being called frequently relative to `write`,
+ // it's likely a sign that the client is using an improperly sized buffer
+ // or their write patterns are somewhat pathological.
+ #[cold]
+ #[inline(never)]
+ fn write_cold(&mut self, buf: &[u8]) -> io::Result<usize> {
+ if buf.len() > self.spare_capacity() {
+ self.flush_buf()?;
+ }
+
+ // Why not len > capacity? To avoid a needless trip through the buffer when the input
+ // exactly fills it. We'd just need to flush it to the underlying writer anyway.
+ if buf.len() >= self.buf.capacity() {
+ self.panicked = true;
+ let r = self.get_mut().write(buf);
+ self.panicked = false;
+ r
+ } else {
+ // Write to the buffer. In this case, we write to the buffer even if it fills it
+ // exactly. Doing otherwise would mean flushing the buffer, then writing this
+ // input to the inner writer, which in many cases would be a worse strategy.
+
+ // SAFETY: There was either enough spare capacity already, or there wasn't and we
+ // flushed the buffer to ensure that there is. In the latter case, we know that there
+ // is because flushing ensured that our entire buffer is spare capacity, and we entered
+ // this block because the input buffer length is less than that capacity. In either
+ // case, it's safe to write the input buffer to our buffer.
+ unsafe {
+ self.write_to_buffer_unchecked(buf);
+ }
+
+ Ok(buf.len())
+ }
+ }
+
+ // Ensure this function does not get inlined into `write_all`, so that it
+ // remains inlineable and its common path remains as short as possible.
+ // If this function ends up being called frequently relative to `write_all`,
+ // it's likely a sign that the client is using an improperly sized buffer
+ // or their write patterns are somewhat pathological.
+ #[cold]
+ #[inline(never)]
+ fn write_all_cold(&mut self, buf: &[u8]) -> io::Result<()> {
+ // Normally, `write_all` just calls `write` in a loop. We can do better
+ // by calling `self.get_mut().write_all()` directly, which avoids
+ // round trips through the buffer in the event of a series of partial
+ // writes in some circumstances.
+
+ if buf.len() > self.spare_capacity() {
+ self.flush_buf()?;
+ }
+
+ // Why not len > capacity? To avoid a needless trip through the buffer when the input
+ // exactly fills it. We'd just need to flush it to the underlying writer anyway.
+ if buf.len() >= self.buf.capacity() {
+ self.panicked = true;
+ let r = self.get_mut().write_all(buf);
+ self.panicked = false;
+ r
+ } else {
+ // Write to the buffer. In this case, we write to the buffer even if it fills it
+ // exactly. Doing otherwise would mean flushing the buffer, then writing this
+ // input to the inner writer, which in many cases would be a worse strategy.
+
+ // SAFETY: There was either enough spare capacity already, or there wasn't and we
+ // flushed the buffer to ensure that there is. In the latter case, we know that there
+ // is because flushing ensured that our entire buffer is spare capacity, and we entered
+ // this block because the input buffer length is less than that capacity. In either
+ // case, it's safe to write the input buffer to our buffer.
+ unsafe {
+ self.write_to_buffer_unchecked(buf);
+ }
+
+ Ok(())
+ }
+ }
+
+ // SAFETY: Requires `buf.len() <= self.buf.capacity() - self.buf.len()`,
+ // i.e., that input buffer length is less than or equal to spare capacity.
+ #[inline]
+ unsafe fn write_to_buffer_unchecked(&mut self, buf: &[u8]) {
+ debug_assert!(buf.len() <= self.spare_capacity());
+ let old_len = self.buf.len();
+ let buf_len = buf.len();
+ let src = buf.as_ptr();
+ let dst = self.buf.as_mut_ptr().add(old_len);
+ ptr::copy_nonoverlapping(src, dst, buf_len);
+ self.buf.set_len(old_len + buf_len);
+ }
+
+ #[inline]
+ fn spare_capacity(&self) -> usize {
+ self.buf.capacity() - self.buf.len()
+ }
+}
+
+#[stable(feature = "bufwriter_into_parts", since = "1.56.0")]
+/// Error returned for the buffered data from `BufWriter::into_parts`, when the underlying
+/// writer has previously panicked. Contains the (possibly partly written) buffered data.
+///
+/// # Example
+///
+/// ```
+/// use std::io::{self, BufWriter, Write};
+/// use std::panic::{catch_unwind, AssertUnwindSafe};
+///
+/// struct PanickingWriter;
+/// impl Write for PanickingWriter {
+/// fn write(&mut self, buf: &[u8]) -> io::Result<usize> { panic!() }
+/// fn flush(&mut self) -> io::Result<()> { panic!() }
+/// }
+///
+/// let mut stream = BufWriter::new(PanickingWriter);
+/// write!(stream, "some data").unwrap();
+/// let result = catch_unwind(AssertUnwindSafe(|| {
+/// stream.flush().unwrap()
+/// }));
+/// assert!(result.is_err());
+/// let (recovered_writer, buffered_data) = stream.into_parts();
+/// assert!(matches!(recovered_writer, PanickingWriter));
+/// assert_eq!(buffered_data.unwrap_err().into_inner(), b"some data");
+/// ```
+pub struct WriterPanicked {
+ buf: Vec<u8>,
+}
+
+impl WriterPanicked {
+ /// Returns the perhaps-unwritten data. Some of this data may have been written by the
+ /// panicking call(s) to the underlying writer, so simply writing it again is not a good idea.
+ #[must_use = "`self` will be dropped if the result is not used"]
+ #[stable(feature = "bufwriter_into_parts", since = "1.56.0")]
+ pub fn into_inner(self) -> Vec<u8> {
+ self.buf
+ }
+
+ const DESCRIPTION: &'static str =
+ "BufWriter inner writer panicked, what data remains unwritten is not known";
+}
+
+#[stable(feature = "bufwriter_into_parts", since = "1.56.0")]
+impl error::Error for WriterPanicked {
+ #[allow(deprecated, deprecated_in_future)]
+ fn description(&self) -> &str {
+ Self::DESCRIPTION
+ }
+}
+
+#[stable(feature = "bufwriter_into_parts", since = "1.56.0")]
+impl fmt::Display for WriterPanicked {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "{}", Self::DESCRIPTION)
+ }
+}
+
+#[stable(feature = "bufwriter_into_parts", since = "1.56.0")]
+impl fmt::Debug for WriterPanicked {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("WriterPanicked")
+ .field("buffer", &format_args!("{}/{}", self.buf.len(), self.buf.capacity()))
+ .finish()
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<W: Write> Write for BufWriter<W> {
+ #[inline]
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ // Use < instead of <= to avoid a needless trip through the buffer in some cases.
+ // See `write_cold` for details.
+ if buf.len() < self.spare_capacity() {
+ // SAFETY: safe by above conditional.
+ unsafe {
+ self.write_to_buffer_unchecked(buf);
+ }
+
+ Ok(buf.len())
+ } else {
+ self.write_cold(buf)
+ }
+ }
+
+ #[inline]
+ fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
+ // Use < instead of <= to avoid a needless trip through the buffer in some cases.
+ // See `write_all_cold` for details.
+ if buf.len() < self.spare_capacity() {
+ // SAFETY: safe by above conditional.
+ unsafe {
+ self.write_to_buffer_unchecked(buf);
+ }
+
+ Ok(())
+ } else {
+ self.write_all_cold(buf)
+ }
+ }
+
+ fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+ // FIXME: Consider applying `#[inline]` / `#[inline(never)]` optimizations already applied
+ // to `write` and `write_all`. The performance benefits can be significant. See #79930.
+ if self.get_ref().is_write_vectored() {
+ // We have to handle the possibility that the total length of the buffers overflows
+ // `usize` (even though this can only happen if multiple `IoSlice`s reference the
+ // same underlying buffer, as otherwise the buffers wouldn't fit in memory). If the
+ // computation overflows, then surely the input cannot fit in our buffer, so we forward
+ // to the inner writer's `write_vectored` method to let it handle it appropriately.
+ let saturated_total_len =
+ bufs.iter().fold(0usize, |acc, b| acc.saturating_add(b.len()));
+
+ if saturated_total_len > self.spare_capacity() {
+ // Flush if the total length of the input exceeds our buffer's spare capacity.
+ // If we would have overflowed, this condition also holds, and we need to flush.
+ self.flush_buf()?;
+ }
+
+ if saturated_total_len >= self.buf.capacity() {
+ // Forward to our inner writer if the total length of the input is greater than or
+ // equal to our buffer capacity. If we would have overflowed, this condition also
+ // holds, and we punt to the inner writer.
+ self.panicked = true;
+ let r = self.get_mut().write_vectored(bufs);
+ self.panicked = false;
+ r
+ } else {
+ // `saturated_total_len < self.buf.capacity()` implies that we did not saturate.
+
+ // SAFETY: We checked whether or not the spare capacity was large enough above. If
+ // it was, then we're safe already. If it wasn't, we flushed, making sufficient
+ // room for any input <= the buffer size, which includes this input.
+ unsafe {
+ bufs.iter().for_each(|b| self.write_to_buffer_unchecked(b));
+ };
+
+ Ok(saturated_total_len)
+ }
+ } else {
+ let mut iter = bufs.iter();
+ let mut total_written = if let Some(buf) = iter.by_ref().find(|&buf| !buf.is_empty()) {
+ // This is the first non-empty slice to write, so if it does
+ // not fit in the buffer, we still get to flush and proceed.
+ if buf.len() > self.spare_capacity() {
+ self.flush_buf()?;
+ }
+ if buf.len() >= self.buf.capacity() {
+ // The slice is at least as large as the buffering capacity,
+ // so it's better to write it directly, bypassing the buffer.
+ self.panicked = true;
+ let r = self.get_mut().write(buf);
+ self.panicked = false;
+ return r;
+ } else {
+ // SAFETY: We checked whether or not the spare capacity was large enough above.
+ // If it was, then we're safe already. If it wasn't, we flushed, making
+ // sufficient room for any input <= the buffer size, which includes this input.
+ unsafe {
+ self.write_to_buffer_unchecked(buf);
+ }
+
+ buf.len()
+ }
+ } else {
+ return Ok(0);
+ };
+ debug_assert!(total_written != 0);
+ for buf in iter {
+ if buf.len() <= self.spare_capacity() {
+ // SAFETY: safe by above conditional.
+ unsafe {
+ self.write_to_buffer_unchecked(buf);
+ }
+
+ // This cannot overflow `usize`. If we are here, we've written all of the bytes
+ // so far to our buffer, and we've ensured that we never exceed the buffer's
+ // capacity. Therefore, `total_written` <= `self.buf.capacity()` <= `usize::MAX`.
+ total_written += buf.len();
+ } else {
+ break;
+ }
+ }
+ Ok(total_written)
+ }
+ }
+
+ fn is_write_vectored(&self) -> bool {
+ true
+ }
+
+ fn flush(&mut self) -> io::Result<()> {
+ self.flush_buf().and_then(|()| self.get_mut().flush())
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<W: Write> fmt::Debug for BufWriter<W>
+where
+ W: fmt::Debug,
+{
+ fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt.debug_struct("BufWriter")
+ .field("writer", &self.inner)
+ .field("buffer", &format_args!("{}/{}", self.buf.len(), self.buf.capacity()))
+ .finish()
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<W: Write + Seek> Seek for BufWriter<W> {
+ /// Seek to the offset, in bytes, in the underlying writer.
+ ///
+ /// Seeking always writes out the internal buffer before seeking.
+ fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
+ self.flush_buf()?;
+ self.get_mut().seek(pos)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<W: Write> Drop for BufWriter<W> {
+ fn drop(&mut self) {
+ if !self.panicked {
+ // dtors should not panic, so we ignore a failed flush
+ let _r = self.flush_buf();
+ }
+ }
+}
diff --git a/library/std/src/io/buffered/linewriter.rs b/library/std/src/io/buffered/linewriter.rs
new file mode 100644
index 000000000..a26a4ab33
--- /dev/null
+++ b/library/std/src/io/buffered/linewriter.rs
@@ -0,0 +1,232 @@
+use crate::fmt;
+use crate::io::{self, buffered::LineWriterShim, BufWriter, IntoInnerError, IoSlice, Write};
+
+/// Wraps a writer and buffers output to it, flushing whenever a newline
+/// (`0x0a`, `'\n'`) is detected.
+///
+/// The [`BufWriter`] struct wraps a writer and buffers its output.
+/// But it only does this batched write when it goes out of scope, or when the
+/// internal buffer is full. Sometimes, you'd prefer to write each line as it's
+/// completed, rather than the entire buffer at once. Enter `LineWriter`. It
+/// does exactly that.
+///
+/// Like [`BufWriter`], a `LineWriter`’s buffer will also be flushed when the
+/// `LineWriter` goes out of scope or when its internal buffer is full.
+///
+/// If there's still a partial line in the buffer when the `LineWriter` is
+/// dropped, it will flush those contents.
+///
+/// # Examples
+///
+/// We can use `LineWriter` to write one line at a time, significantly
+/// reducing the number of actual writes to the file.
+///
+/// ```no_run
+/// use std::fs::{self, File};
+/// use std::io::prelude::*;
+/// use std::io::LineWriter;
+///
+/// fn main() -> std::io::Result<()> {
+/// let road_not_taken = b"I shall be telling this with a sigh
+/// Somewhere ages and ages hence:
+/// Two roads diverged in a wood, and I -
+/// I took the one less traveled by,
+/// And that has made all the difference.";
+///
+/// let file = File::create("poem.txt")?;
+/// let mut file = LineWriter::new(file);
+///
+/// file.write_all(b"I shall be telling this with a sigh")?;
+///
+/// // No bytes are written until a newline is encountered (or
+/// // the internal buffer is filled).
+/// assert_eq!(fs::read_to_string("poem.txt")?, "");
+/// file.write_all(b"\n")?;
+/// assert_eq!(
+/// fs::read_to_string("poem.txt")?,
+/// "I shall be telling this with a sigh\n",
+/// );
+///
+/// // Write the rest of the poem.
+/// file.write_all(b"Somewhere ages and ages hence:
+/// Two roads diverged in a wood, and I -
+/// I took the one less traveled by,
+/// And that has made all the difference.")?;
+///
+/// // The last line of the poem doesn't end in a newline, so
+/// // we have to flush or drop the `LineWriter` to finish
+/// // writing.
+/// file.flush()?;
+///
+/// // Confirm the whole poem was written.
+/// assert_eq!(fs::read("poem.txt")?, &road_not_taken[..]);
+/// Ok(())
+/// }
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct LineWriter<W: Write> {
+ inner: BufWriter<W>,
+}
+
+impl<W: Write> LineWriter<W> {
+ /// Creates a new `LineWriter`.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::fs::File;
+ /// use std::io::LineWriter;
+ ///
+ /// fn main() -> std::io::Result<()> {
+ /// let file = File::create("poem.txt")?;
+ /// let file = LineWriter::new(file);
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn new(inner: W) -> LineWriter<W> {
+ // Lines typically aren't that long, don't use a giant buffer
+ LineWriter::with_capacity(1024, inner)
+ }
+
+ /// Creates a new `LineWriter` with at least the specified capacity for the
+ /// internal buffer.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::fs::File;
+ /// use std::io::LineWriter;
+ ///
+ /// fn main() -> std::io::Result<()> {
+ /// let file = File::create("poem.txt")?;
+ /// let file = LineWriter::with_capacity(100, file);
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn with_capacity(capacity: usize, inner: W) -> LineWriter<W> {
+ LineWriter { inner: BufWriter::with_capacity(capacity, inner) }
+ }
+
+ /// Gets a reference to the underlying writer.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::fs::File;
+ /// use std::io::LineWriter;
+ ///
+ /// fn main() -> std::io::Result<()> {
+ /// let file = File::create("poem.txt")?;
+ /// let file = LineWriter::new(file);
+ ///
+ /// let reference = file.get_ref();
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn get_ref(&self) -> &W {
+ self.inner.get_ref()
+ }
+
+ /// Gets a mutable reference to the underlying writer.
+ ///
+ /// Caution must be taken when calling methods on the mutable reference
+ /// returned as extra writes could corrupt the output stream.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::fs::File;
+ /// use std::io::LineWriter;
+ ///
+ /// fn main() -> std::io::Result<()> {
+ /// let file = File::create("poem.txt")?;
+ /// let mut file = LineWriter::new(file);
+ ///
+ /// // we can use reference just like file
+ /// let reference = file.get_mut();
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn get_mut(&mut self) -> &mut W {
+ self.inner.get_mut()
+ }
+
+ /// Unwraps this `LineWriter`, returning the underlying writer.
+ ///
+ /// The internal buffer is written out before returning the writer.
+ ///
+ /// # Errors
+ ///
+ /// An [`Err`] will be returned if an error occurs while flushing the buffer.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::fs::File;
+ /// use std::io::LineWriter;
+ ///
+ /// fn main() -> std::io::Result<()> {
+ /// let file = File::create("poem.txt")?;
+ ///
+ /// let writer: LineWriter<File> = LineWriter::new(file);
+ ///
+ /// let file: File = writer.into_inner()?;
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn into_inner(self) -> Result<W, IntoInnerError<LineWriter<W>>> {
+ self.inner.into_inner().map_err(|err| err.new_wrapped(|inner| LineWriter { inner }))
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<W: Write> Write for LineWriter<W> {
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ LineWriterShim::new(&mut self.inner).write(buf)
+ }
+
+ fn flush(&mut self) -> io::Result<()> {
+ self.inner.flush()
+ }
+
+ fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+ LineWriterShim::new(&mut self.inner).write_vectored(bufs)
+ }
+
+ fn is_write_vectored(&self) -> bool {
+ self.inner.is_write_vectored()
+ }
+
+ fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
+ LineWriterShim::new(&mut self.inner).write_all(buf)
+ }
+
+ fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
+ LineWriterShim::new(&mut self.inner).write_all_vectored(bufs)
+ }
+
+ fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
+ LineWriterShim::new(&mut self.inner).write_fmt(fmt)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<W: Write> fmt::Debug for LineWriter<W>
+where
+ W: fmt::Debug,
+{
+ fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt.debug_struct("LineWriter")
+ .field("writer", &self.get_ref())
+ .field(
+ "buffer",
+ &format_args!("{}/{}", self.inner.buffer().len(), self.inner.capacity()),
+ )
+ .finish_non_exhaustive()
+ }
+}
diff --git a/library/std/src/io/buffered/linewritershim.rs b/library/std/src/io/buffered/linewritershim.rs
new file mode 100644
index 000000000..0175d2693
--- /dev/null
+++ b/library/std/src/io/buffered/linewritershim.rs
@@ -0,0 +1,276 @@
+use crate::io::{self, BufWriter, IoSlice, Write};
+use crate::sys_common::memchr;
+
+/// Private helper struct for implementing the line-buffered writing logic.
+/// This shim temporarily wraps a BufWriter, and uses its internals to
+/// implement a line-buffered writer (specifically by using the internal
+/// methods like write_to_buf and flush_buf). In this way, a more
+/// efficient abstraction can be created than one that only had access to
+/// `write` and `flush`, without needlessly duplicating a lot of the
+/// implementation details of BufWriter. This also allows existing
+/// `BufWriters` to be temporarily given line-buffering logic; this is what
+/// enables Stdout to be alternately in line-buffered or block-buffered mode.
+#[derive(Debug)]
+pub struct LineWriterShim<'a, W: Write> {
+ buffer: &'a mut BufWriter<W>,
+}
+
+impl<'a, W: Write> LineWriterShim<'a, W> {
+ pub fn new(buffer: &'a mut BufWriter<W>) -> Self {
+ Self { buffer }
+ }
+
+ /// Get a reference to the inner writer (that is, the writer
+ /// wrapped by the BufWriter).
+ fn inner(&self) -> &W {
+ self.buffer.get_ref()
+ }
+
+ /// Get a mutable reference to the inner writer (that is, the writer
+ /// wrapped by the BufWriter). Be careful with this writer, as writes to
+ /// it will bypass the buffer.
+ fn inner_mut(&mut self) -> &mut W {
+ self.buffer.get_mut()
+ }
+
+ /// Get the content currently buffered in self.buffer
+ fn buffered(&self) -> &[u8] {
+ self.buffer.buffer()
+ }
+
+ /// Flush the buffer iff the last byte is a newline (indicating that an
+ /// earlier write only succeeded partially, and we want to retry flushing
+ /// the buffered line before continuing with a subsequent write)
+ fn flush_if_completed_line(&mut self) -> io::Result<()> {
+ match self.buffered().last().copied() {
+ Some(b'\n') => self.buffer.flush_buf(),
+ _ => Ok(()),
+ }
+ }
+}
+
+impl<'a, W: Write> Write for LineWriterShim<'a, W> {
+ /// Write some data into this BufReader with line buffering. This means
+ /// that, if any newlines are present in the data, the data up to the last
+ /// newline is sent directly to the underlying writer, and data after it
+ /// is buffered. Returns the number of bytes written.
+ ///
+ /// This function operates on a "best effort basis"; in keeping with the
+ /// convention of `Write::write`, it makes at most one attempt to write
+ /// new data to the underlying writer. If that write only reports a partial
+ /// success, the remaining data will be buffered.
+ ///
+ /// Because this function attempts to send completed lines to the underlying
+ /// writer, it will also flush the existing buffer if it ends with a
+ /// newline, even if the incoming data does not contain any newlines.
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ let newline_idx = match memchr::memrchr(b'\n', buf) {
+ // If there are no new newlines (that is, if this write is less than
+ // one line), just do a regular buffered write (which may flush if
+ // we exceed the inner buffer's size)
+ None => {
+ self.flush_if_completed_line()?;
+ return self.buffer.write(buf);
+ }
+ // Otherwise, arrange for the lines to be written directly to the
+ // inner writer.
+ Some(newline_idx) => newline_idx + 1,
+ };
+
+ // Flush existing content to prepare for our write. We have to do this
+ // before attempting to write `buf` in order to maintain consistency;
+ // if we add `buf` to the buffer then try to flush it all at once,
+ // we're obligated to return Ok(), which would mean suppressing any
+ // errors that occur during flush.
+ self.buffer.flush_buf()?;
+
+ // This is what we're going to try to write directly to the inner
+ // writer. The rest will be buffered, if nothing goes wrong.
+ let lines = &buf[..newline_idx];
+
+ // Write `lines` directly to the inner writer. In keeping with the
+ // `write` convention, make at most one attempt to add new (unbuffered)
+ // data. Because this write doesn't touch the BufWriter state directly,
+ // and the buffer is known to be empty, we don't need to worry about
+ // self.buffer.panicked here.
+ let flushed = self.inner_mut().write(lines)?;
+
+ // If buffer returns Ok(0), propagate that to the caller without
+ // doing additional buffering; otherwise we're just guaranteeing
+ // an "ErrorKind::WriteZero" later.
+ if flushed == 0 {
+ return Ok(0);
+ }
+
+ // Now that the write has succeeded, buffer the rest (or as much of
+ // the rest as possible). If there were any unwritten newlines, we
+ // only buffer out to the last unwritten newline that fits in the
+ // buffer; this helps prevent flushing partial lines on subsequent
+ // calls to LineWriterShim::write.
+
+ // Handle the cases in order of most-common to least-common, under
+ // the presumption that most writes succeed in totality, and that most
+ // writes are smaller than the buffer.
+ // - Is this a partial line (ie, no newlines left in the unwritten tail)
+ // - If not, does the data out to the last unwritten newline fit in
+ // the buffer?
+ // - If not, scan for the last newline that *does* fit in the buffer
+ let tail = if flushed >= newline_idx {
+ &buf[flushed..]
+ } else if newline_idx - flushed <= self.buffer.capacity() {
+ &buf[flushed..newline_idx]
+ } else {
+ let scan_area = &buf[flushed..];
+ let scan_area = &scan_area[..self.buffer.capacity()];
+ match memchr::memrchr(b'\n', scan_area) {
+ Some(newline_idx) => &scan_area[..newline_idx + 1],
+ None => scan_area,
+ }
+ };
+
+ let buffered = self.buffer.write_to_buf(tail);
+ Ok(flushed + buffered)
+ }
+
+ fn flush(&mut self) -> io::Result<()> {
+ self.buffer.flush()
+ }
+
+ /// Write some vectored data into this BufReader with line buffering. This
+ /// means that, if any newlines are present in the data, the data up to
+ /// and including the buffer containing the last newline is sent directly
+ /// to the inner writer, and the data after it is buffered. Returns the
+ /// number of bytes written.
+ ///
+ /// This function operates on a "best effort basis"; in keeping with the
+ /// convention of `Write::write`, it makes at most one attempt to write
+ /// new data to the underlying writer.
+ ///
+ /// Because this function attempts to send completed lines to the underlying
+ /// writer, it will also flush the existing buffer if it contains any
+ /// newlines.
+ ///
+ /// Because sorting through an array of `IoSlice` can be a bit convoluted,
+ /// This method differs from write in the following ways:
+ ///
+ /// - It attempts to write the full content of all the buffers up to and
+ /// including the one containing the last newline. This means that it
+ /// may attempt to write a partial line, that buffer has data past the
+ /// newline.
+ /// - If the write only reports partial success, it does not attempt to
+ /// find the precise location of the written bytes and buffer the rest.
+ ///
+ /// If the underlying vector doesn't support vectored writing, we instead
+ /// simply write the first non-empty buffer with `write`. This way, we
+ /// get the benefits of more granular partial-line handling without losing
+ /// anything in efficiency
+ fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+ // If there's no specialized behavior for write_vectored, just use
+ // write. This has the benefit of more granular partial-line handling.
+ if !self.is_write_vectored() {
+ return match bufs.iter().find(|buf| !buf.is_empty()) {
+ Some(buf) => self.write(buf),
+ None => Ok(0),
+ };
+ }
+
+ // Find the buffer containing the last newline
+ let last_newline_buf_idx = bufs
+ .iter()
+ .enumerate()
+ .rev()
+ .find_map(|(i, buf)| memchr::memchr(b'\n', buf).map(|_| i));
+
+ // If there are no new newlines (that is, if this write is less than
+ // one line), just do a regular buffered write
+ let last_newline_buf_idx = match last_newline_buf_idx {
+ // No newlines; just do a normal buffered write
+ None => {
+ self.flush_if_completed_line()?;
+ return self.buffer.write_vectored(bufs);
+ }
+ Some(i) => i,
+ };
+
+ // Flush existing content to prepare for our write
+ self.buffer.flush_buf()?;
+
+ // This is what we're going to try to write directly to the inner
+ // writer. The rest will be buffered, if nothing goes wrong.
+ let (lines, tail) = bufs.split_at(last_newline_buf_idx + 1);
+
+ // Write `lines` directly to the inner writer. In keeping with the
+ // `write` convention, make at most one attempt to add new (unbuffered)
+ // data. Because this write doesn't touch the BufWriter state directly,
+ // and the buffer is known to be empty, we don't need to worry about
+ // self.panicked here.
+ let flushed = self.inner_mut().write_vectored(lines)?;
+
+ // If inner returns Ok(0), propagate that to the caller without
+ // doing additional buffering; otherwise we're just guaranteeing
+ // an "ErrorKind::WriteZero" later.
+ if flushed == 0 {
+ return Ok(0);
+ }
+
+ // Don't try to reconstruct the exact amount written; just bail
+ // in the event of a partial write
+ let lines_len = lines.iter().map(|buf| buf.len()).sum();
+ if flushed < lines_len {
+ return Ok(flushed);
+ }
+
+ // Now that the write has succeeded, buffer the rest (or as much of the
+ // rest as possible)
+ let buffered: usize = tail
+ .iter()
+ .filter(|buf| !buf.is_empty())
+ .map(|buf| self.buffer.write_to_buf(buf))
+ .take_while(|&n| n > 0)
+ .sum();
+
+ Ok(flushed + buffered)
+ }
+
+ fn is_write_vectored(&self) -> bool {
+ self.inner().is_write_vectored()
+ }
+
+ /// Write some data into this BufReader with line buffering. This means
+ /// that, if any newlines are present in the data, the data up to the last
+ /// newline is sent directly to the underlying writer, and data after it
+ /// is buffered.
+ ///
+ /// Because this function attempts to send completed lines to the underlying
+ /// writer, it will also flush the existing buffer if it contains any
+ /// newlines, even if the incoming data does not contain any newlines.
+ fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
+ match memchr::memrchr(b'\n', buf) {
+ // If there are no new newlines (that is, if this write is less than
+ // one line), just do a regular buffered write (which may flush if
+ // we exceed the inner buffer's size)
+ None => {
+ self.flush_if_completed_line()?;
+ self.buffer.write_all(buf)
+ }
+ Some(newline_idx) => {
+ let (lines, tail) = buf.split_at(newline_idx + 1);
+
+ if self.buffered().is_empty() {
+ self.inner_mut().write_all(lines)?;
+ } else {
+ // If there is any buffered data, we add the incoming lines
+ // to that buffer before flushing, which saves us at least
+ // one write call. We can't really do this with `write`,
+ // since we can't do this *and* not suppress errors *and*
+ // report a consistent state to the caller in a return
+ // value, but here in write_all it's fine.
+ self.buffer.write_all(lines)?;
+ self.buffer.flush_buf()?;
+ }
+
+ self.buffer.write_all(tail)
+ }
+ }
+ }
+}
diff --git a/library/std/src/io/buffered/mod.rs b/library/std/src/io/buffered/mod.rs
new file mode 100644
index 000000000..100dab1e2
--- /dev/null
+++ b/library/std/src/io/buffered/mod.rs
@@ -0,0 +1,196 @@
+//! Buffering wrappers for I/O traits
+
+mod bufreader;
+mod bufwriter;
+mod linewriter;
+mod linewritershim;
+
+#[cfg(test)]
+mod tests;
+
+use crate::error;
+use crate::fmt;
+use crate::io::Error;
+
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use self::{bufreader::BufReader, bufwriter::BufWriter, linewriter::LineWriter};
+use linewritershim::LineWriterShim;
+
+#[stable(feature = "bufwriter_into_parts", since = "1.56.0")]
+pub use bufwriter::WriterPanicked;
+
+/// An error returned by [`BufWriter::into_inner`] which combines an error that
+/// happened while writing out the buffer, and the buffered writer object
+/// which may be used to recover from the condition.
+///
+/// # Examples
+///
+/// ```no_run
+/// use std::io::BufWriter;
+/// use std::net::TcpStream;
+///
+/// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+///
+/// // do stuff with the stream
+///
+/// // we want to get our `TcpStream` back, so let's try:
+///
+/// let stream = match stream.into_inner() {
+/// Ok(s) => s,
+/// Err(e) => {
+/// // Here, e is an IntoInnerError
+/// panic!("An error occurred");
+/// }
+/// };
+/// ```
+#[derive(Debug)]
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct IntoInnerError<W>(W, Error);
+
+impl<W> IntoInnerError<W> {
+ /// Construct a new IntoInnerError
+ fn new(writer: W, error: Error) -> Self {
+ Self(writer, error)
+ }
+
+ /// Helper to construct a new IntoInnerError; intended to help with
+ /// adapters that wrap other adapters
+ fn new_wrapped<W2>(self, f: impl FnOnce(W) -> W2) -> IntoInnerError<W2> {
+ let Self(writer, error) = self;
+ IntoInnerError::new(f(writer), error)
+ }
+
+ /// Returns the error which caused the call to [`BufWriter::into_inner()`]
+ /// to fail.
+ ///
+ /// This error was returned when attempting to write the internal buffer.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::BufWriter;
+ /// use std::net::TcpStream;
+ ///
+ /// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+ ///
+ /// // do stuff with the stream
+ ///
+ /// // we want to get our `TcpStream` back, so let's try:
+ ///
+ /// let stream = match stream.into_inner() {
+ /// Ok(s) => s,
+ /// Err(e) => {
+ /// // Here, e is an IntoInnerError, let's log the inner error.
+ /// //
+ /// // We'll just 'log' to stdout for this example.
+ /// println!("{}", e.error());
+ ///
+ /// panic!("An unexpected error occurred.");
+ /// }
+ /// };
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn error(&self) -> &Error {
+ &self.1
+ }
+
+ /// Returns the buffered writer instance which generated the error.
+ ///
+ /// The returned object can be used for error recovery, such as
+ /// re-inspecting the buffer.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::BufWriter;
+ /// use std::net::TcpStream;
+ ///
+ /// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+ ///
+ /// // do stuff with the stream
+ ///
+ /// // we want to get our `TcpStream` back, so let's try:
+ ///
+ /// let stream = match stream.into_inner() {
+ /// Ok(s) => s,
+ /// Err(e) => {
+ /// // Here, e is an IntoInnerError, let's re-examine the buffer:
+ /// let buffer = e.into_inner();
+ ///
+ /// // do stuff to try to recover
+ ///
+ /// // afterwards, let's just return the stream
+ /// buffer.into_inner().unwrap()
+ /// }
+ /// };
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn into_inner(self) -> W {
+ self.0
+ }
+
+ /// Consumes the [`IntoInnerError`] and returns the error which caused the call to
+ /// [`BufWriter::into_inner()`] to fail. Unlike `error`, this can be used to
+ /// obtain ownership of the underlying error.
+ ///
+ /// # Example
+ /// ```
+ /// use std::io::{BufWriter, ErrorKind, Write};
+ ///
+ /// let mut not_enough_space = [0u8; 10];
+ /// let mut stream = BufWriter::new(not_enough_space.as_mut());
+ /// write!(stream, "this cannot be actually written").unwrap();
+ /// let into_inner_err = stream.into_inner().expect_err("now we discover it's too small");
+ /// let err = into_inner_err.into_error();
+ /// assert_eq!(err.kind(), ErrorKind::WriteZero);
+ /// ```
+ #[stable(feature = "io_into_inner_error_parts", since = "1.55.0")]
+ pub fn into_error(self) -> Error {
+ self.1
+ }
+
+ /// Consumes the [`IntoInnerError`] and returns the error which caused the call to
+ /// [`BufWriter::into_inner()`] to fail, and the underlying writer.
+ ///
+ /// This can be used to simply obtain ownership of the underlying error; it can also be used for
+ /// advanced error recovery.
+ ///
+ /// # Example
+ /// ```
+ /// use std::io::{BufWriter, ErrorKind, Write};
+ ///
+ /// let mut not_enough_space = [0u8; 10];
+ /// let mut stream = BufWriter::new(not_enough_space.as_mut());
+ /// write!(stream, "this cannot be actually written").unwrap();
+ /// let into_inner_err = stream.into_inner().expect_err("now we discover it's too small");
+ /// let (err, recovered_writer) = into_inner_err.into_parts();
+ /// assert_eq!(err.kind(), ErrorKind::WriteZero);
+ /// assert_eq!(recovered_writer.buffer(), b"t be actually written");
+ /// ```
+ #[stable(feature = "io_into_inner_error_parts", since = "1.55.0")]
+ pub fn into_parts(self) -> (Error, W) {
+ (self.1, self.0)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<W> From<IntoInnerError<W>> for Error {
+ fn from(iie: IntoInnerError<W>) -> Error {
+ iie.1
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<W: Send + fmt::Debug> error::Error for IntoInnerError<W> {
+ #[allow(deprecated, deprecated_in_future)]
+ fn description(&self) -> &str {
+ error::Error::description(self.error())
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<W> fmt::Display for IntoInnerError<W> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ self.error().fmt(f)
+ }
+}
diff --git a/library/std/src/io/buffered/tests.rs b/library/std/src/io/buffered/tests.rs
new file mode 100644
index 000000000..fe45b1326
--- /dev/null
+++ b/library/std/src/io/buffered/tests.rs
@@ -0,0 +1,1039 @@
+use crate::io::prelude::*;
+use crate::io::{self, BufReader, BufWriter, ErrorKind, IoSlice, LineWriter, ReadBuf, SeekFrom};
+use crate::mem::MaybeUninit;
+use crate::panic;
+use crate::sync::atomic::{AtomicUsize, Ordering};
+use crate::thread;
+
+/// A dummy reader intended at testing short-reads propagation.
+pub struct ShortReader {
+ lengths: Vec<usize>,
+}
+
+// FIXME: rustfmt and tidy disagree about the correct formatting of this
+// function. This leads to issues for users with editors configured to
+// rustfmt-on-save.
+impl Read for ShortReader {
+ fn read(&mut self, _: &mut [u8]) -> io::Result<usize> {
+ if self.lengths.is_empty() { Ok(0) } else { Ok(self.lengths.remove(0)) }
+ }
+}
+
+#[test]
+fn test_buffered_reader() {
+ let inner: &[u8] = &[5, 6, 7, 0, 1, 2, 3, 4];
+ let mut reader = BufReader::with_capacity(2, inner);
+
+ let mut buf = [0, 0, 0];
+ let nread = reader.read(&mut buf);
+ assert_eq!(nread.unwrap(), 3);
+ assert_eq!(buf, [5, 6, 7]);
+ assert_eq!(reader.buffer(), []);
+
+ let mut buf = [0, 0];
+ let nread = reader.read(&mut buf);
+ assert_eq!(nread.unwrap(), 2);
+ assert_eq!(buf, [0, 1]);
+ assert_eq!(reader.buffer(), []);
+
+ let mut buf = [0];
+ let nread = reader.read(&mut buf);
+ assert_eq!(nread.unwrap(), 1);
+ assert_eq!(buf, [2]);
+ assert_eq!(reader.buffer(), [3]);
+
+ let mut buf = [0, 0, 0];
+ let nread = reader.read(&mut buf);
+ assert_eq!(nread.unwrap(), 1);
+ assert_eq!(buf, [3, 0, 0]);
+ assert_eq!(reader.buffer(), []);
+
+ let nread = reader.read(&mut buf);
+ assert_eq!(nread.unwrap(), 1);
+ assert_eq!(buf, [4, 0, 0]);
+ assert_eq!(reader.buffer(), []);
+
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+}
+
+#[test]
+fn test_buffered_reader_read_buf() {
+ let inner: &[u8] = &[5, 6, 7, 0, 1, 2, 3, 4];
+ let mut reader = BufReader::with_capacity(2, inner);
+
+ let mut buf = [MaybeUninit::uninit(); 3];
+ let mut buf = ReadBuf::uninit(&mut buf);
+
+ reader.read_buf(&mut buf).unwrap();
+
+ assert_eq!(buf.filled(), [5, 6, 7]);
+ assert_eq!(reader.buffer(), []);
+
+ let mut buf = [MaybeUninit::uninit(); 2];
+ let mut buf = ReadBuf::uninit(&mut buf);
+
+ reader.read_buf(&mut buf).unwrap();
+
+ assert_eq!(buf.filled(), [0, 1]);
+ assert_eq!(reader.buffer(), []);
+
+ let mut buf = [MaybeUninit::uninit(); 1];
+ let mut buf = ReadBuf::uninit(&mut buf);
+
+ reader.read_buf(&mut buf).unwrap();
+
+ assert_eq!(buf.filled(), [2]);
+ assert_eq!(reader.buffer(), [3]);
+
+ let mut buf = [MaybeUninit::uninit(); 3];
+ let mut buf = ReadBuf::uninit(&mut buf);
+
+ reader.read_buf(&mut buf).unwrap();
+
+ assert_eq!(buf.filled(), [3]);
+ assert_eq!(reader.buffer(), []);
+
+ reader.read_buf(&mut buf).unwrap();
+
+ assert_eq!(buf.filled(), [3, 4]);
+ assert_eq!(reader.buffer(), []);
+
+ buf.clear();
+
+ reader.read_buf(&mut buf).unwrap();
+
+ assert_eq!(buf.filled_len(), 0);
+}
+
+#[test]
+fn test_buffered_reader_seek() {
+ let inner: &[u8] = &[5, 6, 7, 0, 1, 2, 3, 4];
+ let mut reader = BufReader::with_capacity(2, io::Cursor::new(inner));
+
+ assert_eq!(reader.seek(SeekFrom::Start(3)).ok(), Some(3));
+ assert_eq!(reader.fill_buf().ok(), Some(&[0, 1][..]));
+ assert_eq!(reader.seek(SeekFrom::Current(0)).ok(), Some(3));
+ assert_eq!(reader.fill_buf().ok(), Some(&[0, 1][..]));
+ assert_eq!(reader.seek(SeekFrom::Current(1)).ok(), Some(4));
+ assert_eq!(reader.fill_buf().ok(), Some(&[1, 2][..]));
+ reader.consume(1);
+ assert_eq!(reader.seek(SeekFrom::Current(-2)).ok(), Some(3));
+}
+
+#[test]
+fn test_buffered_reader_seek_relative() {
+ let inner: &[u8] = &[5, 6, 7, 0, 1, 2, 3, 4];
+ let mut reader = BufReader::with_capacity(2, io::Cursor::new(inner));
+
+ assert!(reader.seek_relative(3).is_ok());
+ assert_eq!(reader.fill_buf().ok(), Some(&[0, 1][..]));
+ assert!(reader.seek_relative(0).is_ok());
+ assert_eq!(reader.fill_buf().ok(), Some(&[0, 1][..]));
+ assert!(reader.seek_relative(1).is_ok());
+ assert_eq!(reader.fill_buf().ok(), Some(&[1][..]));
+ assert!(reader.seek_relative(-1).is_ok());
+ assert_eq!(reader.fill_buf().ok(), Some(&[0, 1][..]));
+ assert!(reader.seek_relative(2).is_ok());
+ assert_eq!(reader.fill_buf().ok(), Some(&[2, 3][..]));
+}
+
+#[test]
+fn test_buffered_reader_stream_position() {
+ let inner: &[u8] = &[5, 6, 7, 0, 1, 2, 3, 4];
+ let mut reader = BufReader::with_capacity(2, io::Cursor::new(inner));
+
+ assert_eq!(reader.stream_position().ok(), Some(0));
+ assert_eq!(reader.seek(SeekFrom::Start(3)).ok(), Some(3));
+ assert_eq!(reader.stream_position().ok(), Some(3));
+ // relative seeking within the buffer and reading position should keep the buffer
+ assert_eq!(reader.fill_buf().ok(), Some(&[0, 1][..]));
+ assert!(reader.seek_relative(0).is_ok());
+ assert_eq!(reader.stream_position().ok(), Some(3));
+ assert_eq!(reader.buffer(), &[0, 1][..]);
+ assert!(reader.seek_relative(1).is_ok());
+ assert_eq!(reader.stream_position().ok(), Some(4));
+ assert_eq!(reader.buffer(), &[1][..]);
+ assert!(reader.seek_relative(-1).is_ok());
+ assert_eq!(reader.stream_position().ok(), Some(3));
+ assert_eq!(reader.buffer(), &[0, 1][..]);
+ // relative seeking outside the buffer will discard it
+ assert!(reader.seek_relative(2).is_ok());
+ assert_eq!(reader.stream_position().ok(), Some(5));
+ assert_eq!(reader.buffer(), &[][..]);
+}
+
+#[test]
+fn test_buffered_reader_stream_position_panic() {
+ let inner: &[u8] = &[5, 6, 7, 0, 1, 2, 3, 4];
+ let mut reader = BufReader::with_capacity(4, io::Cursor::new(inner));
+
+ // cause internal buffer to be filled but read only partially
+ let mut buffer = [0, 0];
+ assert!(reader.read_exact(&mut buffer).is_ok());
+ // rewinding the internal reader will cause buffer to loose sync
+ let inner = reader.get_mut();
+ assert!(inner.seek(SeekFrom::Start(0)).is_ok());
+ // overflow when subtracting the remaining buffer size from current position
+ let result = panic::catch_unwind(panic::AssertUnwindSafe(|| reader.stream_position().ok()));
+ assert!(result.is_err());
+}
+
+#[test]
+fn test_buffered_reader_invalidated_after_read() {
+ let inner: &[u8] = &[5, 6, 7, 0, 1, 2, 3, 4];
+ let mut reader = BufReader::with_capacity(3, io::Cursor::new(inner));
+
+ assert_eq!(reader.fill_buf().ok(), Some(&[5, 6, 7][..]));
+ reader.consume(3);
+
+ let mut buffer = [0, 0, 0, 0, 0];
+ assert_eq!(reader.read(&mut buffer).ok(), Some(5));
+ assert_eq!(buffer, [0, 1, 2, 3, 4]);
+
+ assert!(reader.seek_relative(-2).is_ok());
+ let mut buffer = [0, 0];
+ assert_eq!(reader.read(&mut buffer).ok(), Some(2));
+ assert_eq!(buffer, [3, 4]);
+}
+
+#[test]
+fn test_buffered_reader_invalidated_after_seek() {
+ let inner: &[u8] = &[5, 6, 7, 0, 1, 2, 3, 4];
+ let mut reader = BufReader::with_capacity(3, io::Cursor::new(inner));
+
+ assert_eq!(reader.fill_buf().ok(), Some(&[5, 6, 7][..]));
+ reader.consume(3);
+
+ assert!(reader.seek(SeekFrom::Current(5)).is_ok());
+
+ assert!(reader.seek_relative(-2).is_ok());
+ let mut buffer = [0, 0];
+ assert_eq!(reader.read(&mut buffer).ok(), Some(2));
+ assert_eq!(buffer, [3, 4]);
+}
+
+#[test]
+fn test_buffered_reader_seek_underflow() {
+ // gimmick reader that yields its position modulo 256 for each byte
+ struct PositionReader {
+ pos: u64,
+ }
+ impl Read for PositionReader {
+ fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+ let len = buf.len();
+ for x in buf {
+ *x = self.pos as u8;
+ self.pos = self.pos.wrapping_add(1);
+ }
+ Ok(len)
+ }
+ }
+ impl Seek for PositionReader {
+ fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
+ match pos {
+ SeekFrom::Start(n) => {
+ self.pos = n;
+ }
+ SeekFrom::Current(n) => {
+ self.pos = self.pos.wrapping_add(n as u64);
+ }
+ SeekFrom::End(n) => {
+ self.pos = u64::MAX.wrapping_add(n as u64);
+ }
+ }
+ Ok(self.pos)
+ }
+ }
+
+ let mut reader = BufReader::with_capacity(5, PositionReader { pos: 0 });
+ assert_eq!(reader.fill_buf().ok(), Some(&[0, 1, 2, 3, 4][..]));
+ assert_eq!(reader.seek(SeekFrom::End(-5)).ok(), Some(u64::MAX - 5));
+ assert_eq!(reader.fill_buf().ok().map(|s| s.len()), Some(5));
+ // the following seek will require two underlying seeks
+ let expected = 9223372036854775802;
+ assert_eq!(reader.seek(SeekFrom::Current(i64::MIN)).ok(), Some(expected));
+ assert_eq!(reader.fill_buf().ok().map(|s| s.len()), Some(5));
+ // seeking to 0 should empty the buffer.
+ assert_eq!(reader.seek(SeekFrom::Current(0)).ok(), Some(expected));
+ assert_eq!(reader.get_ref().pos, expected);
+}
+
+#[test]
+fn test_buffered_reader_seek_underflow_discard_buffer_between_seeks() {
+ // gimmick reader that returns Err after first seek
+ struct ErrAfterFirstSeekReader {
+ first_seek: bool,
+ }
+ impl Read for ErrAfterFirstSeekReader {
+ fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+ for x in &mut *buf {
+ *x = 0;
+ }
+ Ok(buf.len())
+ }
+ }
+ impl Seek for ErrAfterFirstSeekReader {
+ fn seek(&mut self, _: SeekFrom) -> io::Result<u64> {
+ if self.first_seek {
+ self.first_seek = false;
+ Ok(0)
+ } else {
+ Err(io::Error::new(io::ErrorKind::Other, "oh no!"))
+ }
+ }
+ }
+
+ let mut reader = BufReader::with_capacity(5, ErrAfterFirstSeekReader { first_seek: true });
+ assert_eq!(reader.fill_buf().ok(), Some(&[0, 0, 0, 0, 0][..]));
+
+ // The following seek will require two underlying seeks. The first will
+ // succeed but the second will fail. This should still invalidate the
+ // buffer.
+ assert!(reader.seek(SeekFrom::Current(i64::MIN)).is_err());
+ assert_eq!(reader.buffer().len(), 0);
+}
+
+#[test]
+fn test_buffered_reader_read_to_end_consumes_buffer() {
+ let data: &[u8] = &[0, 1, 2, 3, 4, 5, 6, 7];
+ let mut reader = BufReader::with_capacity(3, data);
+ let mut buf = Vec::new();
+ assert_eq!(reader.fill_buf().ok(), Some(&[0, 1, 2][..]));
+ assert_eq!(reader.read_to_end(&mut buf).ok(), Some(8));
+ assert_eq!(&buf, &[0, 1, 2, 3, 4, 5, 6, 7]);
+ assert!(reader.buffer().is_empty());
+}
+
+#[test]
+fn test_buffered_reader_read_to_string_consumes_buffer() {
+ let data: &[u8] = "deadbeef".as_bytes();
+ let mut reader = BufReader::with_capacity(3, data);
+ let mut buf = String::new();
+ assert_eq!(reader.fill_buf().ok(), Some("dea".as_bytes()));
+ assert_eq!(reader.read_to_string(&mut buf).ok(), Some(8));
+ assert_eq!(&buf, "deadbeef");
+ assert!(reader.buffer().is_empty());
+}
+
+#[test]
+fn test_buffered_writer() {
+ let inner = Vec::new();
+ let mut writer = BufWriter::with_capacity(2, inner);
+
+ writer.write(&[0, 1]).unwrap();
+ assert_eq!(writer.buffer(), []);
+ assert_eq!(*writer.get_ref(), [0, 1]);
+
+ writer.write(&[2]).unwrap();
+ assert_eq!(writer.buffer(), [2]);
+ assert_eq!(*writer.get_ref(), [0, 1]);
+
+ writer.write(&[3]).unwrap();
+ assert_eq!(writer.buffer(), [2, 3]);
+ assert_eq!(*writer.get_ref(), [0, 1]);
+
+ writer.flush().unwrap();
+ assert_eq!(writer.buffer(), []);
+ assert_eq!(*writer.get_ref(), [0, 1, 2, 3]);
+
+ writer.write(&[4]).unwrap();
+ writer.write(&[5]).unwrap();
+ assert_eq!(writer.buffer(), [4, 5]);
+ assert_eq!(*writer.get_ref(), [0, 1, 2, 3]);
+
+ writer.write(&[6]).unwrap();
+ assert_eq!(writer.buffer(), [6]);
+ assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5]);
+
+ writer.write(&[7, 8]).unwrap();
+ assert_eq!(writer.buffer(), []);
+ assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8]);
+
+ writer.write(&[9, 10, 11]).unwrap();
+ assert_eq!(writer.buffer(), []);
+ assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
+
+ writer.flush().unwrap();
+ assert_eq!(writer.buffer(), []);
+ assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
+}
+
+#[test]
+fn test_buffered_writer_inner_flushes() {
+ let mut w = BufWriter::with_capacity(3, Vec::new());
+ w.write(&[0, 1]).unwrap();
+ assert_eq!(*w.get_ref(), []);
+ let w = w.into_inner().unwrap();
+ assert_eq!(w, [0, 1]);
+}
+
+#[test]
+fn test_buffered_writer_seek() {
+ let mut w = BufWriter::with_capacity(3, io::Cursor::new(Vec::new()));
+ w.write_all(&[0, 1, 2, 3, 4, 5]).unwrap();
+ w.write_all(&[6, 7]).unwrap();
+ assert_eq!(w.seek(SeekFrom::Current(0)).ok(), Some(8));
+ assert_eq!(&w.get_ref().get_ref()[..], &[0, 1, 2, 3, 4, 5, 6, 7][..]);
+ assert_eq!(w.seek(SeekFrom::Start(2)).ok(), Some(2));
+ w.write_all(&[8, 9]).unwrap();
+ assert_eq!(&w.into_inner().unwrap().into_inner()[..], &[0, 1, 8, 9, 4, 5, 6, 7]);
+}
+
+#[test]
+fn test_read_until() {
+ let inner: &[u8] = &[0, 1, 2, 1, 0];
+ let mut reader = BufReader::with_capacity(2, inner);
+ let mut v = Vec::new();
+ reader.read_until(0, &mut v).unwrap();
+ assert_eq!(v, [0]);
+ v.truncate(0);
+ reader.read_until(2, &mut v).unwrap();
+ assert_eq!(v, [1, 2]);
+ v.truncate(0);
+ reader.read_until(1, &mut v).unwrap();
+ assert_eq!(v, [1]);
+ v.truncate(0);
+ reader.read_until(8, &mut v).unwrap();
+ assert_eq!(v, [0]);
+ v.truncate(0);
+ reader.read_until(9, &mut v).unwrap();
+ assert_eq!(v, []);
+}
+
+#[test]
+fn test_line_buffer() {
+ let mut writer = LineWriter::new(Vec::new());
+ writer.write(&[0]).unwrap();
+ assert_eq!(*writer.get_ref(), []);
+ writer.write(&[1]).unwrap();
+ assert_eq!(*writer.get_ref(), []);
+ writer.flush().unwrap();
+ assert_eq!(*writer.get_ref(), [0, 1]);
+ writer.write(&[0, b'\n', 1, b'\n', 2]).unwrap();
+ assert_eq!(*writer.get_ref(), [0, 1, 0, b'\n', 1, b'\n']);
+ writer.flush().unwrap();
+ assert_eq!(*writer.get_ref(), [0, 1, 0, b'\n', 1, b'\n', 2]);
+ writer.write(&[3, b'\n']).unwrap();
+ assert_eq!(*writer.get_ref(), [0, 1, 0, b'\n', 1, b'\n', 2, 3, b'\n']);
+}
+
+#[test]
+fn test_read_line() {
+ let in_buf: &[u8] = b"a\nb\nc";
+ let mut reader = BufReader::with_capacity(2, in_buf);
+ let mut s = String::new();
+ reader.read_line(&mut s).unwrap();
+ assert_eq!(s, "a\n");
+ s.truncate(0);
+ reader.read_line(&mut s).unwrap();
+ assert_eq!(s, "b\n");
+ s.truncate(0);
+ reader.read_line(&mut s).unwrap();
+ assert_eq!(s, "c");
+ s.truncate(0);
+ reader.read_line(&mut s).unwrap();
+ assert_eq!(s, "");
+}
+
+#[test]
+fn test_lines() {
+ let in_buf: &[u8] = b"a\nb\nc";
+ let reader = BufReader::with_capacity(2, in_buf);
+ let mut it = reader.lines();
+ assert_eq!(it.next().unwrap().unwrap(), "a".to_string());
+ assert_eq!(it.next().unwrap().unwrap(), "b".to_string());
+ assert_eq!(it.next().unwrap().unwrap(), "c".to_string());
+ assert!(it.next().is_none());
+}
+
+#[test]
+fn test_short_reads() {
+ let inner = ShortReader { lengths: vec![0, 1, 2, 0, 1, 0] };
+ let mut reader = BufReader::new(inner);
+ let mut buf = [0, 0];
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+ assert_eq!(reader.read(&mut buf).unwrap(), 1);
+ assert_eq!(reader.read(&mut buf).unwrap(), 2);
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+ assert_eq!(reader.read(&mut buf).unwrap(), 1);
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+}
+
+#[test]
+#[should_panic]
+fn dont_panic_in_drop_on_panicked_flush() {
+ struct FailFlushWriter;
+
+ impl Write for FailFlushWriter {
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ Ok(buf.len())
+ }
+ fn flush(&mut self) -> io::Result<()> {
+ Err(io::Error::last_os_error())
+ }
+ }
+
+ let writer = FailFlushWriter;
+ let _writer = BufWriter::new(writer);
+
+ // If writer panics *again* due to the flush error then the process will
+ // abort.
+ panic!();
+}
+
+#[test]
+#[cfg_attr(target_os = "emscripten", ignore)]
+fn panic_in_write_doesnt_flush_in_drop() {
+ static WRITES: AtomicUsize = AtomicUsize::new(0);
+
+ struct PanicWriter;
+
+ impl Write for PanicWriter {
+ fn write(&mut self, _: &[u8]) -> io::Result<usize> {
+ WRITES.fetch_add(1, Ordering::SeqCst);
+ panic!();
+ }
+ fn flush(&mut self) -> io::Result<()> {
+ Ok(())
+ }
+ }
+
+ thread::spawn(|| {
+ let mut writer = BufWriter::new(PanicWriter);
+ let _ = writer.write(b"hello world");
+ let _ = writer.flush();
+ })
+ .join()
+ .unwrap_err();
+
+ assert_eq!(WRITES.load(Ordering::SeqCst), 1);
+}
+
+#[bench]
+fn bench_buffered_reader(b: &mut test::Bencher) {
+ b.iter(|| BufReader::new(io::empty()));
+}
+
+#[bench]
+fn bench_buffered_reader_small_reads(b: &mut test::Bencher) {
+ let data = (0..u8::MAX).cycle().take(1024 * 4).collect::<Vec<_>>();
+ b.iter(|| {
+ let mut reader = BufReader::new(&data[..]);
+ let mut buf = [0u8; 4];
+ for _ in 0..1024 {
+ reader.read_exact(&mut buf).unwrap();
+ core::hint::black_box(&buf);
+ }
+ });
+}
+
+#[bench]
+fn bench_buffered_writer(b: &mut test::Bencher) {
+ b.iter(|| BufWriter::new(io::sink()));
+}
+
+/// A simple `Write` target, designed to be wrapped by `LineWriter` /
+/// `BufWriter` / etc, that can have its `write` & `flush` behavior
+/// configured
+#[derive(Default, Clone)]
+struct ProgrammableSink {
+ // Writes append to this slice
+ pub buffer: Vec<u8>,
+
+ // If true, writes will always be an error
+ pub always_write_error: bool,
+
+ // If true, flushes will always be an error
+ pub always_flush_error: bool,
+
+ // If set, only up to this number of bytes will be written in a single
+ // call to `write`
+ pub accept_prefix: Option<usize>,
+
+ // If set, counts down with each write, and writes return an error
+ // when it hits 0
+ pub max_writes: Option<usize>,
+
+ // If set, attempting to write when max_writes == Some(0) will be an
+ // error; otherwise, it will return Ok(0).
+ pub error_after_max_writes: bool,
+}
+
+impl Write for ProgrammableSink {
+ fn write(&mut self, data: &[u8]) -> io::Result<usize> {
+ if self.always_write_error {
+ return Err(io::Error::new(io::ErrorKind::Other, "test - always_write_error"));
+ }
+
+ match self.max_writes {
+ Some(0) if self.error_after_max_writes => {
+ return Err(io::Error::new(io::ErrorKind::Other, "test - max_writes"));
+ }
+ Some(0) => return Ok(0),
+ Some(ref mut count) => *count -= 1,
+ None => {}
+ }
+
+ let len = match self.accept_prefix {
+ None => data.len(),
+ Some(prefix) => data.len().min(prefix),
+ };
+
+ let data = &data[..len];
+ self.buffer.extend_from_slice(data);
+
+ Ok(len)
+ }
+
+ fn flush(&mut self) -> io::Result<()> {
+ if self.always_flush_error {
+ Err(io::Error::new(io::ErrorKind::Other, "test - always_flush_error"))
+ } else {
+ Ok(())
+ }
+ }
+}
+
+/// Previously the `LineWriter` could successfully write some bytes but
+/// then fail to report that it has done so. Additionally, an erroneous
+/// flush after a successful write was permanently ignored.
+///
+/// Test that a line writer correctly reports the number of written bytes,
+/// and that it attempts to flush buffered lines from previous writes
+/// before processing new data
+///
+/// Regression test for #37807
+#[test]
+fn erroneous_flush_retried() {
+ let writer = ProgrammableSink {
+ // Only write up to 4 bytes at a time
+ accept_prefix: Some(4),
+
+ // Accept the first two writes, then error the others
+ max_writes: Some(2),
+ error_after_max_writes: true,
+
+ ..Default::default()
+ };
+
+ // This should write the first 4 bytes. The rest will be buffered, out
+ // to the last newline.
+ let mut writer = LineWriter::new(writer);
+ assert_eq!(writer.write(b"a\nb\nc\nd\ne").unwrap(), 8);
+
+ // This write should attempt to flush "c\nd\n", then buffer "e". No
+ // errors should happen here because no further writes should be
+ // attempted against `writer`.
+ assert_eq!(writer.write(b"e").unwrap(), 1);
+ assert_eq!(&writer.get_ref().buffer, b"a\nb\nc\nd\n");
+}
+
+#[test]
+fn line_vectored() {
+ let mut a = LineWriter::new(Vec::new());
+ assert_eq!(
+ a.write_vectored(&[
+ IoSlice::new(&[]),
+ IoSlice::new(b"\n"),
+ IoSlice::new(&[]),
+ IoSlice::new(b"a"),
+ ])
+ .unwrap(),
+ 2,
+ );
+ assert_eq!(a.get_ref(), b"\n");
+
+ assert_eq!(
+ a.write_vectored(&[
+ IoSlice::new(&[]),
+ IoSlice::new(b"b"),
+ IoSlice::new(&[]),
+ IoSlice::new(b"a"),
+ IoSlice::new(&[]),
+ IoSlice::new(b"c"),
+ ])
+ .unwrap(),
+ 3,
+ );
+ assert_eq!(a.get_ref(), b"\n");
+ a.flush().unwrap();
+ assert_eq!(a.get_ref(), b"\nabac");
+ assert_eq!(a.write_vectored(&[]).unwrap(), 0);
+ assert_eq!(
+ a.write_vectored(&[
+ IoSlice::new(&[]),
+ IoSlice::new(&[]),
+ IoSlice::new(&[]),
+ IoSlice::new(&[]),
+ ])
+ .unwrap(),
+ 0,
+ );
+ assert_eq!(a.write_vectored(&[IoSlice::new(b"a\nb"),]).unwrap(), 3);
+ assert_eq!(a.get_ref(), b"\nabaca\nb");
+}
+
+#[test]
+fn line_vectored_partial_and_errors() {
+ use crate::collections::VecDeque;
+
+ enum Call {
+ Write { inputs: Vec<&'static [u8]>, output: io::Result<usize> },
+ Flush { output: io::Result<()> },
+ }
+
+ #[derive(Default)]
+ struct Writer {
+ calls: VecDeque<Call>,
+ }
+
+ impl Write for Writer {
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ self.write_vectored(&[IoSlice::new(buf)])
+ }
+
+ fn write_vectored(&mut self, buf: &[IoSlice<'_>]) -> io::Result<usize> {
+ match self.calls.pop_front().expect("unexpected call to write") {
+ Call::Write { inputs, output } => {
+ assert_eq!(inputs, buf.iter().map(|b| &**b).collect::<Vec<_>>());
+ output
+ }
+ Call::Flush { .. } => panic!("unexpected call to write; expected a flush"),
+ }
+ }
+
+ fn is_write_vectored(&self) -> bool {
+ true
+ }
+
+ fn flush(&mut self) -> io::Result<()> {
+ match self.calls.pop_front().expect("Unexpected call to flush") {
+ Call::Flush { output } => output,
+ Call::Write { .. } => panic!("unexpected call to flush; expected a write"),
+ }
+ }
+ }
+
+ impl Drop for Writer {
+ fn drop(&mut self) {
+ if !thread::panicking() {
+ assert_eq!(self.calls.len(), 0);
+ }
+ }
+ }
+
+ // partial writes keep going
+ let mut a = LineWriter::new(Writer::default());
+ a.write_vectored(&[IoSlice::new(&[]), IoSlice::new(b"abc")]).unwrap();
+
+ a.get_mut().calls.push_back(Call::Write { inputs: vec![b"abc"], output: Ok(1) });
+ a.get_mut().calls.push_back(Call::Write { inputs: vec![b"bc"], output: Ok(2) });
+ a.get_mut().calls.push_back(Call::Write { inputs: vec![b"x", b"\n"], output: Ok(2) });
+
+ a.write_vectored(&[IoSlice::new(b"x"), IoSlice::new(b"\n")]).unwrap();
+
+ a.get_mut().calls.push_back(Call::Flush { output: Ok(()) });
+ a.flush().unwrap();
+
+ // erroneous writes stop and don't write more
+ a.get_mut().calls.push_back(Call::Write { inputs: vec![b"x", b"\na"], output: Err(err()) });
+ a.get_mut().calls.push_back(Call::Flush { output: Ok(()) });
+ assert!(a.write_vectored(&[IoSlice::new(b"x"), IoSlice::new(b"\na")]).is_err());
+ a.flush().unwrap();
+
+ fn err() -> io::Error {
+ io::Error::new(io::ErrorKind::Other, "x")
+ }
+}
+
+/// Test that, in cases where vectored writing is not enabled, the
+/// LineWriter uses the normal `write` call, which more-correctly handles
+/// partial lines
+#[test]
+fn line_vectored_ignored() {
+ let writer = ProgrammableSink::default();
+ let mut writer = LineWriter::new(writer);
+
+ let content = [
+ IoSlice::new(&[]),
+ IoSlice::new(b"Line 1\nLine"),
+ IoSlice::new(b" 2\nLine 3\nL"),
+ IoSlice::new(&[]),
+ IoSlice::new(&[]),
+ IoSlice::new(b"ine 4"),
+ IoSlice::new(b"\nLine 5\n"),
+ ];
+
+ let count = writer.write_vectored(&content).unwrap();
+ assert_eq!(count, 11);
+ assert_eq!(&writer.get_ref().buffer, b"Line 1\n");
+
+ let count = writer.write_vectored(&content[2..]).unwrap();
+ assert_eq!(count, 11);
+ assert_eq!(&writer.get_ref().buffer, b"Line 1\nLine 2\nLine 3\n");
+
+ let count = writer.write_vectored(&content[5..]).unwrap();
+ assert_eq!(count, 5);
+ assert_eq!(&writer.get_ref().buffer, b"Line 1\nLine 2\nLine 3\n");
+
+ let count = writer.write_vectored(&content[6..]).unwrap();
+ assert_eq!(count, 8);
+ assert_eq!(
+ writer.get_ref().buffer.as_slice(),
+ b"Line 1\nLine 2\nLine 3\nLine 4\nLine 5\n".as_ref()
+ );
+}
+
+/// Test that, given this input:
+///
+/// Line 1\n
+/// Line 2\n
+/// Line 3\n
+/// Line 4
+///
+/// And given a result that only writes to midway through Line 2
+///
+/// That only up to the end of Line 3 is buffered
+///
+/// This behavior is desirable because it prevents flushing partial lines
+#[test]
+fn partial_write_buffers_line() {
+ let writer = ProgrammableSink { accept_prefix: Some(13), ..Default::default() };
+ let mut writer = LineWriter::new(writer);
+
+ assert_eq!(writer.write(b"Line 1\nLine 2\nLine 3\nLine4").unwrap(), 21);
+ assert_eq!(&writer.get_ref().buffer, b"Line 1\nLine 2");
+
+ assert_eq!(writer.write(b"Line 4").unwrap(), 6);
+ assert_eq!(&writer.get_ref().buffer, b"Line 1\nLine 2\nLine 3\n");
+}
+
+/// Test that, given this input:
+///
+/// Line 1\n
+/// Line 2\n
+/// Line 3
+///
+/// And given that the full write of lines 1 and 2 was successful
+/// That data up to Line 3 is buffered
+#[test]
+fn partial_line_buffered_after_line_write() {
+ let writer = ProgrammableSink::default();
+ let mut writer = LineWriter::new(writer);
+
+ assert_eq!(writer.write(b"Line 1\nLine 2\nLine 3").unwrap(), 20);
+ assert_eq!(&writer.get_ref().buffer, b"Line 1\nLine 2\n");
+
+ assert!(writer.flush().is_ok());
+ assert_eq!(&writer.get_ref().buffer, b"Line 1\nLine 2\nLine 3");
+}
+
+/// Test that, given a partial line that exceeds the length of
+/// LineBuffer's buffer (that is, without a trailing newline), that that
+/// line is written to the inner writer
+#[test]
+fn long_line_flushed() {
+ let writer = ProgrammableSink::default();
+ let mut writer = LineWriter::with_capacity(5, writer);
+
+ assert_eq!(writer.write(b"0123456789").unwrap(), 10);
+ assert_eq!(&writer.get_ref().buffer, b"0123456789");
+}
+
+/// Test that, given a very long partial line *after* successfully
+/// flushing a complete line, that that line is buffered unconditionally,
+/// and no additional writes take place. This assures the property that
+/// `write` should make at-most-one attempt to write new data.
+#[test]
+fn line_long_tail_not_flushed() {
+ let writer = ProgrammableSink::default();
+ let mut writer = LineWriter::with_capacity(5, writer);
+
+ // Assert that Line 1\n is flushed, and 01234 is buffered
+ assert_eq!(writer.write(b"Line 1\n0123456789").unwrap(), 12);
+ assert_eq!(&writer.get_ref().buffer, b"Line 1\n");
+
+ // Because the buffer is full, this subsequent write will flush it
+ assert_eq!(writer.write(b"5").unwrap(), 1);
+ assert_eq!(&writer.get_ref().buffer, b"Line 1\n01234");
+}
+
+/// Test that, if an attempt to pre-flush buffered data returns Ok(0),
+/// this is propagated as an error.
+#[test]
+fn line_buffer_write0_error() {
+ let writer = ProgrammableSink {
+ // Accept one write, then return Ok(0) on subsequent ones
+ max_writes: Some(1),
+
+ ..Default::default()
+ };
+ let mut writer = LineWriter::new(writer);
+
+ // This should write "Line 1\n" and buffer "Partial"
+ assert_eq!(writer.write(b"Line 1\nPartial").unwrap(), 14);
+ assert_eq!(&writer.get_ref().buffer, b"Line 1\n");
+
+ // This will attempt to flush "partial", which will return Ok(0), which
+ // needs to be an error, because we've already informed the client
+ // that we accepted the write.
+ let err = writer.write(b" Line End\n").unwrap_err();
+ assert_eq!(err.kind(), ErrorKind::WriteZero);
+ assert_eq!(&writer.get_ref().buffer, b"Line 1\n");
+}
+
+/// Test that, if a write returns Ok(0) after a successful pre-flush, this
+/// is propagated as Ok(0)
+#[test]
+fn line_buffer_write0_normal() {
+ let writer = ProgrammableSink {
+ // Accept two writes, then return Ok(0) on subsequent ones
+ max_writes: Some(2),
+
+ ..Default::default()
+ };
+ let mut writer = LineWriter::new(writer);
+
+ // This should write "Line 1\n" and buffer "Partial"
+ assert_eq!(writer.write(b"Line 1\nPartial").unwrap(), 14);
+ assert_eq!(&writer.get_ref().buffer, b"Line 1\n");
+
+ // This will flush partial, which will succeed, but then return Ok(0)
+ // when flushing " Line End\n"
+ assert_eq!(writer.write(b" Line End\n").unwrap(), 0);
+ assert_eq!(&writer.get_ref().buffer, b"Line 1\nPartial");
+}
+
+/// LineWriter has a custom `write_all`; make sure it works correctly
+#[test]
+fn line_write_all() {
+ let writer = ProgrammableSink {
+ // Only write 5 bytes at a time
+ accept_prefix: Some(5),
+ ..Default::default()
+ };
+ let mut writer = LineWriter::new(writer);
+
+ writer.write_all(b"Line 1\nLine 2\nLine 3\nLine 4\nPartial").unwrap();
+ assert_eq!(&writer.get_ref().buffer, b"Line 1\nLine 2\nLine 3\nLine 4\n");
+ writer.write_all(b" Line 5\n").unwrap();
+ assert_eq!(
+ writer.get_ref().buffer.as_slice(),
+ b"Line 1\nLine 2\nLine 3\nLine 4\nPartial Line 5\n".as_ref(),
+ );
+}
+
+#[test]
+fn line_write_all_error() {
+ let writer = ProgrammableSink {
+ // Only accept up to 3 writes of up to 5 bytes each
+ accept_prefix: Some(5),
+ max_writes: Some(3),
+ ..Default::default()
+ };
+
+ let mut writer = LineWriter::new(writer);
+ let res = writer.write_all(b"Line 1\nLine 2\nLine 3\nLine 4\nPartial");
+ assert!(res.is_err());
+ // An error from write_all leaves everything in an indeterminate state,
+ // so there's nothing else to test here
+}
+
+/// Under certain circumstances, the old implementation of LineWriter
+/// would try to buffer "to the last newline" but be forced to buffer
+/// less than that, leading to inappropriate partial line writes.
+/// Regression test for that issue.
+#[test]
+fn partial_multiline_buffering() {
+ let writer = ProgrammableSink {
+ // Write only up to 5 bytes at a time
+ accept_prefix: Some(5),
+ ..Default::default()
+ };
+
+ let mut writer = LineWriter::with_capacity(10, writer);
+
+ let content = b"AAAAABBBBB\nCCCCDDDDDD\nEEE";
+
+ // When content is written, LineWriter will try to write blocks A, B,
+ // C, and D. Only block A will succeed. Under the old behavior, LineWriter
+ // would then try to buffer B, C and D, but because its capacity is 10,
+ // it will only be able to buffer B and C. We don't want to buffer
+ // partial lines concurrent with whole lines, so the correct behavior
+ // is to buffer only block B (out to the newline)
+ assert_eq!(writer.write(content).unwrap(), 11);
+ assert_eq!(writer.get_ref().buffer, *b"AAAAA");
+
+ writer.flush().unwrap();
+ assert_eq!(writer.get_ref().buffer, *b"AAAAABBBBB\n");
+}
+
+/// Same as test_partial_multiline_buffering, but in the event NO full lines
+/// fit in the buffer, just buffer as much as possible
+#[test]
+fn partial_multiline_buffering_without_full_line() {
+ let writer = ProgrammableSink {
+ // Write only up to 5 bytes at a time
+ accept_prefix: Some(5),
+ ..Default::default()
+ };
+
+ let mut writer = LineWriter::with_capacity(5, writer);
+
+ let content = b"AAAAABBBBBBBBBB\nCCCCC\nDDDDD";
+
+ // When content is written, LineWriter will try to write blocks A, B,
+ // and C. Only block A will succeed. Under the old behavior, LineWriter
+ // would then try to buffer B and C, but because its capacity is 5,
+ // it will only be able to buffer part of B. Because it's not possible
+ // for it to buffer any complete lines, it should buffer as much of B as
+ // possible
+ assert_eq!(writer.write(content).unwrap(), 10);
+ assert_eq!(writer.get_ref().buffer, *b"AAAAA");
+
+ writer.flush().unwrap();
+ assert_eq!(writer.get_ref().buffer, *b"AAAAABBBBB");
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+enum RecordedEvent {
+ Write(String),
+ Flush,
+}
+
+#[derive(Debug, Clone, Default)]
+struct WriteRecorder {
+ pub events: Vec<RecordedEvent>,
+}
+
+impl Write for WriteRecorder {
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ use crate::str::from_utf8;
+
+ self.events.push(RecordedEvent::Write(from_utf8(buf).unwrap().to_string()));
+ Ok(buf.len())
+ }
+
+ fn flush(&mut self) -> io::Result<()> {
+ self.events.push(RecordedEvent::Flush);
+ Ok(())
+ }
+}
+
+/// Test that a normal, formatted writeln only results in a single write
+/// call to the underlying writer. A naive implementation of
+/// LineWriter::write_all results in two writes: one of the buffered data,
+/// and another of the final substring in the formatted set
+#[test]
+fn single_formatted_write() {
+ let writer = WriteRecorder::default();
+ let mut writer = LineWriter::new(writer);
+
+ // Under a naive implementation of LineWriter, this will result in two
+ // writes: "hello, world" and "!\n", because write() has to flush the
+ // buffer before attempting to write the last "!\n". write_all shouldn't
+ // have this limitation.
+ writeln!(&mut writer, "{}, {}!", "hello", "world").unwrap();
+ assert_eq!(writer.get_ref().events, [RecordedEvent::Write("hello, world!\n".to_string())]);
+}
diff --git a/library/std/src/io/copy.rs b/library/std/src/io/copy.rs
new file mode 100644
index 000000000..1a10245e4
--- /dev/null
+++ b/library/std/src/io/copy.rs
@@ -0,0 +1,161 @@
+use super::{BufWriter, ErrorKind, Read, ReadBuf, Result, Write, DEFAULT_BUF_SIZE};
+use crate::mem::MaybeUninit;
+
+/// Copies the entire contents of a reader into a writer.
+///
+/// This function will continuously read data from `reader` and then
+/// write it into `writer` in a streaming fashion until `reader`
+/// returns EOF.
+///
+/// On success, the total number of bytes that were copied from
+/// `reader` to `writer` is returned.
+///
+/// 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.
+///
+/// [`fs::copy`]: crate::fs::copy
+///
+/// # Errors
+///
+/// This function will return an error immediately if any call to [`read`] or
+/// [`write`] returns an error. All instances of [`ErrorKind::Interrupted`] are
+/// handled by this function and the underlying operation is retried.
+///
+/// [`read`]: Read::read
+/// [`write`]: Write::write
+///
+/// # Examples
+///
+/// ```
+/// use std::io;
+///
+/// fn main() -> io::Result<()> {
+/// let mut reader: &[u8] = b"hello";
+/// let mut writer: Vec<u8> = vec![];
+///
+/// io::copy(&mut reader, &mut writer)?;
+///
+/// assert_eq!(&b"hello"[..], &writer[..]);
+/// Ok(())
+/// }
+/// ```
+///
+/// # Platform-specific behavior
+///
+/// On Linux (including Android), this function uses `copy_file_range(2)`,
+/// `sendfile(2)` or `splice(2)` syscalls to move data directly between file
+/// descriptors if possible.
+///
+/// Note that platform-specific behavior [may change in the future][changes].
+///
+/// [changes]: crate::io#platform-specific-behavior
+#[stable(feature = "rust1", since = "1.0.0")]
+pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> Result<u64>
+where
+ R: Read,
+ W: Write,
+{
+ cfg_if::cfg_if! {
+ if #[cfg(any(target_os = "linux", target_os = "android"))] {
+ crate::sys::kernel_copy::copy_spec(reader, writer)
+ } else {
+ generic_copy(reader, writer)
+ }
+ }
+}
+
+/// The userspace read-write-loop implementation of `io::copy` that is used when
+/// OS-specific specializations for copy offloading are not available or not applicable.
+pub(crate) fn generic_copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> Result<u64>
+where
+ R: Read,
+ W: Write,
+{
+ BufferedCopySpec::copy_to(reader, writer)
+}
+
+/// Specialization of the read-write loop that either uses a stack buffer
+/// or reuses the internal buffer of a BufWriter
+trait BufferedCopySpec: Write {
+ fn copy_to<R: Read + ?Sized>(reader: &mut R, writer: &mut Self) -> Result<u64>;
+}
+
+impl<W: Write + ?Sized> BufferedCopySpec for W {
+ default fn copy_to<R: Read + ?Sized>(reader: &mut R, writer: &mut Self) -> Result<u64> {
+ stack_buffer_copy(reader, writer)
+ }
+}
+
+impl<I: Write> BufferedCopySpec for BufWriter<I> {
+ fn copy_to<R: Read + ?Sized>(reader: &mut R, writer: &mut Self) -> Result<u64> {
+ if writer.capacity() < DEFAULT_BUF_SIZE {
+ return stack_buffer_copy(reader, writer);
+ }
+
+ let mut len = 0;
+ let mut init = 0;
+
+ loop {
+ let buf = writer.buffer_mut();
+ let mut read_buf = ReadBuf::uninit(buf.spare_capacity_mut());
+
+ // SAFETY: init is either 0 or the initialized_len of the previous iteration
+ unsafe {
+ read_buf.assume_init(init);
+ }
+
+ if read_buf.capacity() >= DEFAULT_BUF_SIZE {
+ match reader.read_buf(&mut read_buf) {
+ Ok(()) => {
+ let bytes_read = read_buf.filled_len();
+
+ if bytes_read == 0 {
+ return Ok(len);
+ }
+
+ init = read_buf.initialized_len() - bytes_read;
+
+ // SAFETY: ReadBuf guarantees all of its filled bytes are init
+ unsafe { buf.set_len(buf.len() + bytes_read) };
+ len += bytes_read as u64;
+ // Read again if the buffer still has enough capacity, as BufWriter itself would do
+ // This will occur if the reader returns short reads
+ continue;
+ }
+ Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
+ Err(e) => return Err(e),
+ }
+ }
+
+ writer.flush_buf()?;
+ }
+ }
+}
+
+fn stack_buffer_copy<R: Read + ?Sized, W: Write + ?Sized>(
+ reader: &mut R,
+ writer: &mut W,
+) -> Result<u64> {
+ let mut buf = [MaybeUninit::uninit(); DEFAULT_BUF_SIZE];
+ let mut buf = ReadBuf::uninit(&mut buf);
+
+ let mut len = 0;
+
+ loop {
+ match reader.read_buf(&mut buf) {
+ Ok(()) => {}
+ Err(e) if e.kind() == ErrorKind::Interrupted => continue,
+ Err(e) => return Err(e),
+ };
+
+ if buf.filled().is_empty() {
+ break;
+ }
+
+ len += buf.filled().len() as u64;
+ writer.write_all(buf.filled())?;
+ buf.clear();
+ }
+
+ Ok(len)
+}
diff --git a/library/std/src/io/cursor.rs b/library/std/src/io/cursor.rs
new file mode 100644
index 000000000..f3fbfc447
--- /dev/null
+++ b/library/std/src/io/cursor.rs
@@ -0,0 +1,640 @@
+#[cfg(test)]
+mod tests;
+
+use crate::io::prelude::*;
+
+use crate::alloc::Allocator;
+use crate::cmp;
+use crate::io::{self, ErrorKind, IoSlice, IoSliceMut, ReadBuf, SeekFrom};
+
+/// A `Cursor` wraps an in-memory buffer and provides it with a
+/// [`Seek`] implementation.
+///
+/// `Cursor`s are used with in-memory buffers, anything implementing
+/// <code>[AsRef]<\[u8]></code>, to allow them to implement [`Read`] and/or [`Write`],
+/// allowing these buffers to be used anywhere you might use a reader or writer
+/// that does actual I/O.
+///
+/// The standard library implements some I/O traits on various types which
+/// are commonly used as a buffer, like <code>Cursor<[Vec]\<u8>></code> and
+/// <code>Cursor<[&\[u8\]][bytes]></code>.
+///
+/// # Examples
+///
+/// We may want to write bytes to a [`File`] in our production
+/// code, but use an in-memory buffer in our tests. We can do this with
+/// `Cursor`:
+///
+/// [bytes]: crate::slice "slice"
+/// [`File`]: crate::fs::File
+///
+/// ```no_run
+/// use std::io::prelude::*;
+/// use std::io::{self, SeekFrom};
+/// use std::fs::File;
+///
+/// // a library function we've written
+/// fn write_ten_bytes_at_end<W: Write + Seek>(writer: &mut W) -> io::Result<()> {
+/// writer.seek(SeekFrom::End(-10))?;
+///
+/// for i in 0..10 {
+/// writer.write(&[i])?;
+/// }
+///
+/// // all went well
+/// Ok(())
+/// }
+///
+/// # fn foo() -> io::Result<()> {
+/// // Here's some code that uses this library function.
+/// //
+/// // We might want to use a BufReader here for efficiency, but let's
+/// // keep this example focused.
+/// let mut file = File::create("foo.txt")?;
+///
+/// write_ten_bytes_at_end(&mut file)?;
+/// # Ok(())
+/// # }
+///
+/// // now let's write a test
+/// #[test]
+/// fn test_writes_bytes() {
+/// // setting up a real File is much slower than an in-memory buffer,
+/// // let's use a cursor instead
+/// use std::io::Cursor;
+/// let mut buff = Cursor::new(vec![0; 15]);
+///
+/// write_ten_bytes_at_end(&mut buff).unwrap();
+///
+/// assert_eq!(&buff.get_ref()[5..15], &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
+/// }
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+#[derive(Debug, Default, Eq, PartialEq)]
+pub struct Cursor<T> {
+ inner: T,
+ pos: u64,
+}
+
+impl<T> Cursor<T> {
+ /// Creates a new cursor wrapping the provided underlying in-memory buffer.
+ ///
+ /// Cursor initial position is `0` even if underlying buffer (e.g., [`Vec`])
+ /// is not empty. So writing to cursor starts with overwriting [`Vec`]
+ /// content, not with appending to it.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::Cursor;
+ ///
+ /// let buff = Cursor::new(Vec::new());
+ /// # fn force_inference(_: &Cursor<Vec<u8>>) {}
+ /// # force_inference(&buff);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
+ pub const fn new(inner: T) -> Cursor<T> {
+ Cursor { pos: 0, inner }
+ }
+
+ /// Consumes this cursor, returning the underlying value.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::Cursor;
+ ///
+ /// let buff = Cursor::new(Vec::new());
+ /// # fn force_inference(_: &Cursor<Vec<u8>>) {}
+ /// # force_inference(&buff);
+ ///
+ /// let vec = buff.into_inner();
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn into_inner(self) -> T {
+ self.inner
+ }
+
+ /// Gets a reference to the underlying value in this cursor.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::Cursor;
+ ///
+ /// let buff = Cursor::new(Vec::new());
+ /// # fn force_inference(_: &Cursor<Vec<u8>>) {}
+ /// # force_inference(&buff);
+ ///
+ /// let reference = buff.get_ref();
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
+ pub const fn get_ref(&self) -> &T {
+ &self.inner
+ }
+
+ /// Gets a mutable reference to the underlying value in this cursor.
+ ///
+ /// Care should be taken to avoid modifying the internal I/O state of the
+ /// underlying value as it may corrupt this cursor's position.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::Cursor;
+ ///
+ /// let mut buff = Cursor::new(Vec::new());
+ /// # fn force_inference(_: &Cursor<Vec<u8>>) {}
+ /// # force_inference(&buff);
+ ///
+ /// let reference = buff.get_mut();
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn get_mut(&mut self) -> &mut T {
+ &mut self.inner
+ }
+
+ /// Returns the current position of this cursor.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::Cursor;
+ /// use std::io::prelude::*;
+ /// use std::io::SeekFrom;
+ ///
+ /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
+ ///
+ /// assert_eq!(buff.position(), 0);
+ ///
+ /// buff.seek(SeekFrom::Current(2)).unwrap();
+ /// assert_eq!(buff.position(), 2);
+ ///
+ /// buff.seek(SeekFrom::Current(-1)).unwrap();
+ /// assert_eq!(buff.position(), 1);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
+ pub const fn position(&self) -> u64 {
+ self.pos
+ }
+
+ /// Sets the position of this cursor.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::Cursor;
+ ///
+ /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
+ ///
+ /// assert_eq!(buff.position(), 0);
+ ///
+ /// buff.set_position(2);
+ /// assert_eq!(buff.position(), 2);
+ ///
+ /// buff.set_position(4);
+ /// assert_eq!(buff.position(), 4);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn set_position(&mut self, pos: u64) {
+ self.pos = pos;
+ }
+}
+
+impl<T> Cursor<T>
+where
+ T: AsRef<[u8]>,
+{
+ /// Returns the remaining slice.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// #![feature(cursor_remaining)]
+ /// use std::io::Cursor;
+ ///
+ /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
+ ///
+ /// assert_eq!(buff.remaining_slice(), &[1, 2, 3, 4, 5]);
+ ///
+ /// buff.set_position(2);
+ /// assert_eq!(buff.remaining_slice(), &[3, 4, 5]);
+ ///
+ /// buff.set_position(4);
+ /// assert_eq!(buff.remaining_slice(), &[5]);
+ ///
+ /// buff.set_position(6);
+ /// assert_eq!(buff.remaining_slice(), &[]);
+ /// ```
+ #[unstable(feature = "cursor_remaining", issue = "86369")]
+ pub fn remaining_slice(&self) -> &[u8] {
+ let len = self.pos.min(self.inner.as_ref().len() as u64);
+ &self.inner.as_ref()[(len as usize)..]
+ }
+
+ /// Returns `true` if the remaining slice is empty.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// #![feature(cursor_remaining)]
+ /// use std::io::Cursor;
+ ///
+ /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
+ ///
+ /// buff.set_position(2);
+ /// assert!(!buff.is_empty());
+ ///
+ /// buff.set_position(5);
+ /// assert!(buff.is_empty());
+ ///
+ /// buff.set_position(10);
+ /// assert!(buff.is_empty());
+ /// ```
+ #[unstable(feature = "cursor_remaining", issue = "86369")]
+ pub fn is_empty(&self) -> bool {
+ self.pos >= self.inner.as_ref().len() as u64
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<T> Clone for Cursor<T>
+where
+ T: Clone,
+{
+ #[inline]
+ fn clone(&self) -> Self {
+ Cursor { inner: self.inner.clone(), pos: self.pos }
+ }
+
+ #[inline]
+ fn clone_from(&mut self, other: &Self) {
+ self.inner.clone_from(&other.inner);
+ self.pos = other.pos;
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<T> io::Seek for Cursor<T>
+where
+ T: AsRef<[u8]>,
+{
+ fn seek(&mut self, style: SeekFrom) -> io::Result<u64> {
+ let (base_pos, offset) = match style {
+ SeekFrom::Start(n) => {
+ self.pos = n;
+ return Ok(n);
+ }
+ SeekFrom::End(n) => (self.inner.as_ref().len() as u64, n),
+ SeekFrom::Current(n) => (self.pos, n),
+ };
+ match base_pos.checked_add_signed(offset) {
+ Some(n) => {
+ self.pos = n;
+ Ok(self.pos)
+ }
+ None => Err(io::const_io_error!(
+ ErrorKind::InvalidInput,
+ "invalid seek to a negative or overflowing position",
+ )),
+ }
+ }
+
+ fn stream_len(&mut self) -> io::Result<u64> {
+ Ok(self.inner.as_ref().len() as u64)
+ }
+
+ fn stream_position(&mut self) -> io::Result<u64> {
+ Ok(self.pos)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<T> Read for Cursor<T>
+where
+ T: AsRef<[u8]>,
+{
+ fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+ let n = Read::read(&mut self.remaining_slice(), buf)?;
+ self.pos += n as u64;
+ Ok(n)
+ }
+
+ fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> {
+ let prev_filled = buf.filled_len();
+
+ Read::read_buf(&mut self.fill_buf()?, buf)?;
+
+ self.pos += (buf.filled_len() - prev_filled) as u64;
+
+ Ok(())
+ }
+
+ fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
+ let mut nread = 0;
+ for buf in bufs {
+ let n = self.read(buf)?;
+ nread += n;
+ if n < buf.len() {
+ break;
+ }
+ }
+ Ok(nread)
+ }
+
+ fn is_read_vectored(&self) -> bool {
+ true
+ }
+
+ fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
+ let n = buf.len();
+ Read::read_exact(&mut self.remaining_slice(), buf)?;
+ self.pos += n as u64;
+ Ok(())
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<T> BufRead for Cursor<T>
+where
+ T: AsRef<[u8]>,
+{
+ fn fill_buf(&mut self) -> io::Result<&[u8]> {
+ Ok(self.remaining_slice())
+ }
+ fn consume(&mut self, amt: usize) {
+ self.pos += amt as u64;
+ }
+}
+
+// Non-resizing write implementation
+#[inline]
+fn slice_write(pos_mut: &mut u64, slice: &mut [u8], buf: &[u8]) -> io::Result<usize> {
+ let pos = cmp::min(*pos_mut, slice.len() as u64);
+ let amt = (&mut slice[(pos as usize)..]).write(buf)?;
+ *pos_mut += amt as u64;
+ Ok(amt)
+}
+
+#[inline]
+fn slice_write_vectored(
+ pos_mut: &mut u64,
+ slice: &mut [u8],
+ bufs: &[IoSlice<'_>],
+) -> io::Result<usize> {
+ let mut nwritten = 0;
+ for buf in bufs {
+ let n = slice_write(pos_mut, slice, buf)?;
+ nwritten += n;
+ if n < buf.len() {
+ break;
+ }
+ }
+ Ok(nwritten)
+}
+
+/// Reserves the required space, and pads the vec with 0s if necessary.
+fn reserve_and_pad<A: Allocator>(
+ pos_mut: &mut u64,
+ vec: &mut Vec<u8, A>,
+ buf_len: usize,
+) -> io::Result<usize> {
+ let pos: usize = (*pos_mut).try_into().map_err(|_| {
+ io::const_io_error!(
+ ErrorKind::InvalidInput,
+ "cursor position exceeds maximum possible vector length",
+ )
+ })?;
+
+ // For safety reasons, we don't want these numbers to overflow
+ // otherwise our allocation won't be enough
+ let desired_cap = pos.saturating_add(buf_len);
+ if desired_cap > vec.capacity() {
+ // We want our vec's total capacity
+ // to have room for (pos+buf_len) bytes. Reserve allocates
+ // based on additional elements from the length, so we need to
+ // reserve the difference
+ vec.reserve(desired_cap - vec.len());
+ }
+ // Pad if pos is above the current len.
+ if pos > vec.len() {
+ let diff = pos - vec.len();
+ // Unfortunately, `resize()` would suffice but the optimiser does not
+ // realise the `reserve` it does can be eliminated. So we do it manually
+ // to eliminate that extra branch
+ let spare = vec.spare_capacity_mut();
+ debug_assert!(spare.len() >= diff);
+ // Safety: we have allocated enough capacity for this.
+ // And we are only writing, not reading
+ unsafe {
+ spare.get_unchecked_mut(..diff).fill(core::mem::MaybeUninit::new(0));
+ vec.set_len(pos);
+ }
+ }
+
+ Ok(pos)
+}
+
+/// Writes the slice to the vec without allocating
+/// # Safety: vec must have buf.len() spare capacity
+unsafe fn vec_write_unchecked<A>(pos: usize, vec: &mut Vec<u8, A>, buf: &[u8]) -> usize
+where
+ A: Allocator,
+{
+ debug_assert!(vec.capacity() >= pos + buf.len());
+ vec.as_mut_ptr().add(pos).copy_from(buf.as_ptr(), buf.len());
+ pos + buf.len()
+}
+
+/// Resizing write implementation for [`Cursor`]
+///
+/// Cursor is allowed to have a pre-allocated and initialised
+/// vector body, but with a position of 0. This means the [`Write`]
+/// will overwrite the contents of the vec.
+///
+/// This also allows for the vec body to be empty, but with a position of N.
+/// This means that [`Write`] will pad the vec with 0 initially,
+/// before writing anything from that point
+fn vec_write<A>(pos_mut: &mut u64, vec: &mut Vec<u8, A>, buf: &[u8]) -> io::Result<usize>
+where
+ A: Allocator,
+{
+ let buf_len = buf.len();
+ let mut pos = reserve_and_pad(pos_mut, vec, buf_len)?;
+
+ // Write the buf then progress the vec forward if necessary
+ // Safety: we have ensured that the capacity is available
+ // and that all bytes get written up to pos
+ unsafe {
+ pos = vec_write_unchecked(pos, vec, buf);
+ if pos > vec.len() {
+ vec.set_len(pos);
+ }
+ };
+
+ // Bump us forward
+ *pos_mut += buf_len as u64;
+ Ok(buf_len)
+}
+
+/// Resizing write_vectored implementation for [`Cursor`]
+///
+/// Cursor is allowed to have a pre-allocated and initialised
+/// vector body, but with a position of 0. This means the [`Write`]
+/// will overwrite the contents of the vec.
+///
+/// This also allows for the vec body to be empty, but with a position of N.
+/// This means that [`Write`] will pad the vec with 0 initially,
+/// before writing anything from that point
+fn vec_write_vectored<A>(
+ pos_mut: &mut u64,
+ vec: &mut Vec<u8, A>,
+ bufs: &[IoSlice<'_>],
+) -> io::Result<usize>
+where
+ A: Allocator,
+{
+ // For safety reasons, we don't want this sum to overflow ever.
+ // If this saturates, the reserve should panic to avoid any unsound writing.
+ let buf_len = bufs.iter().fold(0usize, |a, b| a.saturating_add(b.len()));
+ let mut pos = reserve_and_pad(pos_mut, vec, buf_len)?;
+
+ // Write the buf then progress the vec forward if necessary
+ // Safety: we have ensured that the capacity is available
+ // and that all bytes get written up to the last pos
+ unsafe {
+ for buf in bufs {
+ pos = vec_write_unchecked(pos, vec, buf);
+ }
+ if pos > vec.len() {
+ vec.set_len(pos);
+ }
+ }
+
+ // Bump us forward
+ *pos_mut += buf_len as u64;
+ Ok(buf_len)
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Write for Cursor<&mut [u8]> {
+ #[inline]
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ slice_write(&mut self.pos, self.inner, buf)
+ }
+
+ #[inline]
+ fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+ slice_write_vectored(&mut self.pos, self.inner, bufs)
+ }
+
+ #[inline]
+ fn is_write_vectored(&self) -> bool {
+ true
+ }
+
+ #[inline]
+ fn flush(&mut self) -> io::Result<()> {
+ Ok(())
+ }
+}
+
+#[stable(feature = "cursor_mut_vec", since = "1.25.0")]
+impl<A> Write for Cursor<&mut Vec<u8, A>>
+where
+ A: Allocator,
+{
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ vec_write(&mut self.pos, self.inner, buf)
+ }
+
+ fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+ vec_write_vectored(&mut self.pos, self.inner, bufs)
+ }
+
+ #[inline]
+ fn is_write_vectored(&self) -> bool {
+ true
+ }
+
+ #[inline]
+ fn flush(&mut self) -> io::Result<()> {
+ Ok(())
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<A> Write for Cursor<Vec<u8, A>>
+where
+ A: Allocator,
+{
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ vec_write(&mut self.pos, &mut self.inner, buf)
+ }
+
+ fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+ vec_write_vectored(&mut self.pos, &mut self.inner, bufs)
+ }
+
+ #[inline]
+ fn is_write_vectored(&self) -> bool {
+ true
+ }
+
+ #[inline]
+ fn flush(&mut self) -> io::Result<()> {
+ Ok(())
+ }
+}
+
+#[stable(feature = "cursor_box_slice", since = "1.5.0")]
+impl<A> Write for Cursor<Box<[u8], A>>
+where
+ A: Allocator,
+{
+ #[inline]
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ slice_write(&mut self.pos, &mut self.inner, buf)
+ }
+
+ #[inline]
+ fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+ slice_write_vectored(&mut self.pos, &mut self.inner, bufs)
+ }
+
+ #[inline]
+ fn is_write_vectored(&self) -> bool {
+ true
+ }
+
+ #[inline]
+ fn flush(&mut self) -> io::Result<()> {
+ Ok(())
+ }
+}
+
+#[stable(feature = "cursor_array", since = "1.61.0")]
+impl<const N: usize> Write for Cursor<[u8; N]> {
+ #[inline]
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ slice_write(&mut self.pos, &mut self.inner, buf)
+ }
+
+ #[inline]
+ fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+ slice_write_vectored(&mut self.pos, &mut self.inner, bufs)
+ }
+
+ #[inline]
+ fn is_write_vectored(&self) -> bool {
+ true
+ }
+
+ #[inline]
+ fn flush(&mut self) -> io::Result<()> {
+ Ok(())
+ }
+}
diff --git a/library/std/src/io/cursor/tests.rs b/library/std/src/io/cursor/tests.rs
new file mode 100644
index 000000000..d7c203c29
--- /dev/null
+++ b/library/std/src/io/cursor/tests.rs
@@ -0,0 +1,567 @@
+use crate::io::prelude::*;
+use crate::io::{Cursor, IoSlice, IoSliceMut, SeekFrom};
+
+#[test]
+fn test_vec_writer() {
+ let mut writer = Vec::new();
+ assert_eq!(writer.write(&[0]).unwrap(), 1);
+ assert_eq!(writer.write(&[1, 2, 3]).unwrap(), 3);
+ assert_eq!(writer.write(&[4, 5, 6, 7]).unwrap(), 4);
+ assert_eq!(
+ writer
+ .write_vectored(&[IoSlice::new(&[]), IoSlice::new(&[8, 9]), IoSlice::new(&[10])],)
+ .unwrap(),
+ 3
+ );
+ let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
+ assert_eq!(writer, b);
+}
+
+#[test]
+fn test_mem_writer() {
+ let mut writer = Cursor::new(Vec::new());
+ writer.set_position(10);
+ assert_eq!(writer.write(&[0]).unwrap(), 1);
+ assert_eq!(writer.write(&[1, 2, 3]).unwrap(), 3);
+ assert_eq!(writer.write(&[4, 5, 6, 7]).unwrap(), 4);
+ assert_eq!(
+ writer
+ .write_vectored(&[IoSlice::new(&[]), IoSlice::new(&[8, 9]), IoSlice::new(&[10])],)
+ .unwrap(),
+ 3
+ );
+ let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
+ assert_eq!(&writer.get_ref()[..10], &[0; 10]);
+ assert_eq!(&writer.get_ref()[10..], b);
+}
+
+#[test]
+fn test_mem_writer_preallocated() {
+ let mut writer = Cursor::new(vec![0, 0, 0, 0, 0, 0, 0, 0, 8, 9, 10]);
+ assert_eq!(writer.write(&[0]).unwrap(), 1);
+ assert_eq!(writer.write(&[1, 2, 3]).unwrap(), 3);
+ assert_eq!(writer.write(&[4, 5, 6, 7]).unwrap(), 4);
+ let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
+ assert_eq!(&writer.get_ref()[..], b);
+}
+
+#[test]
+fn test_mem_mut_writer() {
+ let mut vec = Vec::new();
+ let mut writer = Cursor::new(&mut vec);
+ assert_eq!(writer.write(&[0]).unwrap(), 1);
+ assert_eq!(writer.write(&[1, 2, 3]).unwrap(), 3);
+ assert_eq!(writer.write(&[4, 5, 6, 7]).unwrap(), 4);
+ assert_eq!(
+ writer
+ .write_vectored(&[IoSlice::new(&[]), IoSlice::new(&[8, 9]), IoSlice::new(&[10])],)
+ .unwrap(),
+ 3
+ );
+ let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
+ assert_eq!(&writer.get_ref()[..], b);
+}
+
+fn test_slice_writer<T>(writer: &mut Cursor<T>)
+where
+ T: AsRef<[u8]>,
+ Cursor<T>: Write,
+{
+ assert_eq!(writer.position(), 0);
+ assert_eq!(writer.write(&[0]).unwrap(), 1);
+ assert_eq!(writer.position(), 1);
+ assert_eq!(writer.write(&[1, 2, 3]).unwrap(), 3);
+ assert_eq!(writer.write(&[4, 5, 6, 7]).unwrap(), 4);
+ assert_eq!(writer.position(), 8);
+ assert_eq!(writer.write(&[]).unwrap(), 0);
+ assert_eq!(writer.position(), 8);
+
+ assert_eq!(writer.write(&[8, 9]).unwrap(), 1);
+ assert_eq!(writer.write(&[10]).unwrap(), 0);
+ let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8];
+ assert_eq!(writer.get_ref().as_ref(), b);
+}
+
+fn test_slice_writer_vectored<T>(writer: &mut Cursor<T>)
+where
+ T: AsRef<[u8]>,
+ Cursor<T>: Write,
+{
+ assert_eq!(writer.position(), 0);
+ assert_eq!(writer.write_vectored(&[IoSlice::new(&[0])]).unwrap(), 1);
+ assert_eq!(writer.position(), 1);
+ assert_eq!(
+ writer.write_vectored(&[IoSlice::new(&[1, 2, 3]), IoSlice::new(&[4, 5, 6, 7]),]).unwrap(),
+ 7,
+ );
+ assert_eq!(writer.position(), 8);
+ assert_eq!(writer.write_vectored(&[]).unwrap(), 0);
+ assert_eq!(writer.position(), 8);
+
+ assert_eq!(writer.write_vectored(&[IoSlice::new(&[8, 9])]).unwrap(), 1);
+ assert_eq!(writer.write_vectored(&[IoSlice::new(&[10])]).unwrap(), 0);
+ let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8];
+ assert_eq!(writer.get_ref().as_ref(), b);
+}
+
+#[test]
+fn test_box_slice_writer() {
+ let mut writer = Cursor::new(vec![0u8; 9].into_boxed_slice());
+ test_slice_writer(&mut writer);
+}
+
+#[test]
+fn test_box_slice_writer_vectored() {
+ let mut writer = Cursor::new(vec![0u8; 9].into_boxed_slice());
+ test_slice_writer_vectored(&mut writer);
+}
+
+#[test]
+fn test_array_writer() {
+ let mut writer = Cursor::new([0u8; 9]);
+ test_slice_writer(&mut writer);
+}
+
+#[test]
+fn test_array_writer_vectored() {
+ let mut writer = Cursor::new([0u8; 9]);
+ test_slice_writer_vectored(&mut writer);
+}
+
+#[test]
+fn test_buf_writer() {
+ let mut buf = [0 as u8; 9];
+ let mut writer = Cursor::new(&mut buf[..]);
+ test_slice_writer(&mut writer);
+}
+
+#[test]
+fn test_buf_writer_vectored() {
+ let mut buf = [0 as u8; 9];
+ let mut writer = Cursor::new(&mut buf[..]);
+ test_slice_writer_vectored(&mut writer);
+}
+
+#[test]
+fn test_buf_writer_seek() {
+ let mut buf = [0 as u8; 8];
+ {
+ let mut writer = Cursor::new(&mut buf[..]);
+ assert_eq!(writer.position(), 0);
+ assert_eq!(writer.write(&[1]).unwrap(), 1);
+ assert_eq!(writer.position(), 1);
+
+ assert_eq!(writer.seek(SeekFrom::Start(2)).unwrap(), 2);
+ assert_eq!(writer.position(), 2);
+ assert_eq!(writer.write(&[2]).unwrap(), 1);
+ assert_eq!(writer.position(), 3);
+
+ assert_eq!(writer.seek(SeekFrom::Current(-2)).unwrap(), 1);
+ assert_eq!(writer.position(), 1);
+ assert_eq!(writer.write(&[3]).unwrap(), 1);
+ assert_eq!(writer.position(), 2);
+
+ assert_eq!(writer.seek(SeekFrom::End(-1)).unwrap(), 7);
+ assert_eq!(writer.position(), 7);
+ assert_eq!(writer.write(&[4]).unwrap(), 1);
+ assert_eq!(writer.position(), 8);
+ }
+ let b: &[_] = &[1, 3, 2, 0, 0, 0, 0, 4];
+ assert_eq!(buf, b);
+}
+
+#[test]
+fn test_buf_writer_error() {
+ let mut buf = [0 as u8; 2];
+ let mut writer = Cursor::new(&mut buf[..]);
+ assert_eq!(writer.write(&[0]).unwrap(), 1);
+ assert_eq!(writer.write(&[0, 0]).unwrap(), 1);
+ assert_eq!(writer.write(&[0, 0]).unwrap(), 0);
+}
+
+#[test]
+fn test_mem_reader() {
+ let mut reader = Cursor::new(vec![0, 1, 2, 3, 4, 5, 6, 7]);
+ let mut buf = [];
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+ assert_eq!(reader.position(), 0);
+ let mut buf = [0];
+ assert_eq!(reader.read(&mut buf).unwrap(), 1);
+ assert_eq!(reader.position(), 1);
+ let b: &[_] = &[0];
+ assert_eq!(buf, b);
+ let mut buf = [0; 4];
+ assert_eq!(reader.read(&mut buf).unwrap(), 4);
+ assert_eq!(reader.position(), 5);
+ let b: &[_] = &[1, 2, 3, 4];
+ assert_eq!(buf, b);
+ assert_eq!(reader.read(&mut buf).unwrap(), 3);
+ let b: &[_] = &[5, 6, 7];
+ assert_eq!(&buf[..3], b);
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+}
+
+#[test]
+fn test_mem_reader_vectored() {
+ let mut reader = Cursor::new(vec![0, 1, 2, 3, 4, 5, 6, 7]);
+ let mut buf = [];
+ assert_eq!(reader.read_vectored(&mut [IoSliceMut::new(&mut buf)]).unwrap(), 0);
+ assert_eq!(reader.position(), 0);
+ let mut buf = [0];
+ assert_eq!(
+ reader.read_vectored(&mut [IoSliceMut::new(&mut []), IoSliceMut::new(&mut buf),]).unwrap(),
+ 1,
+ );
+ assert_eq!(reader.position(), 1);
+ let b: &[_] = &[0];
+ assert_eq!(buf, b);
+ let mut buf1 = [0; 4];
+ let mut buf2 = [0; 4];
+ assert_eq!(
+ reader
+ .read_vectored(&mut [IoSliceMut::new(&mut buf1), IoSliceMut::new(&mut buf2),])
+ .unwrap(),
+ 7,
+ );
+ let b1: &[_] = &[1, 2, 3, 4];
+ let b2: &[_] = &[5, 6, 7];
+ assert_eq!(buf1, b1);
+ assert_eq!(&buf2[..3], b2);
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+}
+
+#[test]
+fn test_boxed_slice_reader() {
+ let mut reader = Cursor::new(vec![0, 1, 2, 3, 4, 5, 6, 7].into_boxed_slice());
+ let mut buf = [];
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+ assert_eq!(reader.position(), 0);
+ let mut buf = [0];
+ assert_eq!(reader.read(&mut buf).unwrap(), 1);
+ assert_eq!(reader.position(), 1);
+ let b: &[_] = &[0];
+ assert_eq!(buf, b);
+ let mut buf = [0; 4];
+ assert_eq!(reader.read(&mut buf).unwrap(), 4);
+ assert_eq!(reader.position(), 5);
+ let b: &[_] = &[1, 2, 3, 4];
+ assert_eq!(buf, b);
+ assert_eq!(reader.read(&mut buf).unwrap(), 3);
+ let b: &[_] = &[5, 6, 7];
+ assert_eq!(&buf[..3], b);
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+}
+
+#[test]
+fn test_boxed_slice_reader_vectored() {
+ let mut reader = Cursor::new(vec![0, 1, 2, 3, 4, 5, 6, 7].into_boxed_slice());
+ let mut buf = [];
+ assert_eq!(reader.read_vectored(&mut [IoSliceMut::new(&mut buf)]).unwrap(), 0);
+ assert_eq!(reader.position(), 0);
+ let mut buf = [0];
+ assert_eq!(
+ reader.read_vectored(&mut [IoSliceMut::new(&mut []), IoSliceMut::new(&mut buf),]).unwrap(),
+ 1,
+ );
+ assert_eq!(reader.position(), 1);
+ let b: &[_] = &[0];
+ assert_eq!(buf, b);
+ let mut buf1 = [0; 4];
+ let mut buf2 = [0; 4];
+ assert_eq!(
+ reader
+ .read_vectored(&mut [IoSliceMut::new(&mut buf1), IoSliceMut::new(&mut buf2)],)
+ .unwrap(),
+ 7,
+ );
+ let b1: &[_] = &[1, 2, 3, 4];
+ let b2: &[_] = &[5, 6, 7];
+ assert_eq!(buf1, b1);
+ assert_eq!(&buf2[..3], b2);
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+}
+
+#[test]
+fn read_to_end() {
+ let mut reader = Cursor::new(vec![0, 1, 2, 3, 4, 5, 6, 7]);
+ let mut v = Vec::new();
+ reader.read_to_end(&mut v).unwrap();
+ assert_eq!(v, [0, 1, 2, 3, 4, 5, 6, 7]);
+}
+
+#[test]
+fn test_slice_reader() {
+ let in_buf = vec![0, 1, 2, 3, 4, 5, 6, 7];
+ let reader = &mut &in_buf[..];
+ let mut buf = [];
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+ let mut buf = [0];
+ assert_eq!(reader.read(&mut buf).unwrap(), 1);
+ assert_eq!(reader.len(), 7);
+ let b: &[_] = &[0];
+ assert_eq!(&buf[..], b);
+ let mut buf = [0; 4];
+ assert_eq!(reader.read(&mut buf).unwrap(), 4);
+ assert_eq!(reader.len(), 3);
+ let b: &[_] = &[1, 2, 3, 4];
+ assert_eq!(&buf[..], b);
+ assert_eq!(reader.read(&mut buf).unwrap(), 3);
+ let b: &[_] = &[5, 6, 7];
+ assert_eq!(&buf[..3], b);
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+}
+
+#[test]
+fn test_slice_reader_vectored() {
+ let in_buf = vec![0, 1, 2, 3, 4, 5, 6, 7];
+ let reader = &mut &in_buf[..];
+ let mut buf = [];
+ assert_eq!(reader.read_vectored(&mut [IoSliceMut::new(&mut buf)]).unwrap(), 0);
+ let mut buf = [0];
+ assert_eq!(
+ reader.read_vectored(&mut [IoSliceMut::new(&mut []), IoSliceMut::new(&mut buf),]).unwrap(),
+ 1,
+ );
+ assert_eq!(reader.len(), 7);
+ let b: &[_] = &[0];
+ assert_eq!(buf, b);
+ let mut buf1 = [0; 4];
+ let mut buf2 = [0; 4];
+ assert_eq!(
+ reader
+ .read_vectored(&mut [IoSliceMut::new(&mut buf1), IoSliceMut::new(&mut buf2)],)
+ .unwrap(),
+ 7,
+ );
+ let b1: &[_] = &[1, 2, 3, 4];
+ let b2: &[_] = &[5, 6, 7];
+ assert_eq!(buf1, b1);
+ assert_eq!(&buf2[..3], b2);
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+}
+
+#[test]
+fn test_read_exact() {
+ let in_buf = vec![0, 1, 2, 3, 4, 5, 6, 7];
+ let reader = &mut &in_buf[..];
+ let mut buf = [];
+ assert!(reader.read_exact(&mut buf).is_ok());
+ let mut buf = [8];
+ assert!(reader.read_exact(&mut buf).is_ok());
+ assert_eq!(buf[0], 0);
+ assert_eq!(reader.len(), 7);
+ let mut buf = [0, 0, 0, 0, 0, 0, 0];
+ assert!(reader.read_exact(&mut buf).is_ok());
+ assert_eq!(buf, [1, 2, 3, 4, 5, 6, 7]);
+ assert_eq!(reader.len(), 0);
+ let mut buf = [0];
+ assert!(reader.read_exact(&mut buf).is_err());
+}
+
+#[test]
+fn test_buf_reader() {
+ let in_buf = vec![0, 1, 2, 3, 4, 5, 6, 7];
+ let mut reader = Cursor::new(&in_buf[..]);
+ let mut buf = [];
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+ assert_eq!(reader.position(), 0);
+ let mut buf = [0];
+ assert_eq!(reader.read(&mut buf).unwrap(), 1);
+ assert_eq!(reader.position(), 1);
+ let b: &[_] = &[0];
+ assert_eq!(buf, b);
+ let mut buf = [0; 4];
+ assert_eq!(reader.read(&mut buf).unwrap(), 4);
+ assert_eq!(reader.position(), 5);
+ let b: &[_] = &[1, 2, 3, 4];
+ assert_eq!(buf, b);
+ assert_eq!(reader.read(&mut buf).unwrap(), 3);
+ let b: &[_] = &[5, 6, 7];
+ assert_eq!(&buf[..3], b);
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+}
+
+#[test]
+fn seek_past_end() {
+ let buf = [0xff];
+ let mut r = Cursor::new(&buf[..]);
+ assert_eq!(r.seek(SeekFrom::Start(10)).unwrap(), 10);
+ assert_eq!(r.read(&mut [0]).unwrap(), 0);
+
+ let mut r = Cursor::new(vec![10]);
+ assert_eq!(r.seek(SeekFrom::Start(10)).unwrap(), 10);
+ assert_eq!(r.read(&mut [0]).unwrap(), 0);
+
+ let mut buf = [0];
+ let mut r = Cursor::new(&mut buf[..]);
+ assert_eq!(r.seek(SeekFrom::Start(10)).unwrap(), 10);
+ assert_eq!(r.write(&[3]).unwrap(), 0);
+
+ let mut r = Cursor::new(vec![10].into_boxed_slice());
+ assert_eq!(r.seek(SeekFrom::Start(10)).unwrap(), 10);
+ assert_eq!(r.write(&[3]).unwrap(), 0);
+}
+
+#[test]
+fn seek_past_i64() {
+ let buf = [0xff];
+ let mut r = Cursor::new(&buf[..]);
+ assert_eq!(r.seek(SeekFrom::Start(6)).unwrap(), 6);
+ assert_eq!(r.seek(SeekFrom::Current(0x7ffffffffffffff0)).unwrap(), 0x7ffffffffffffff6);
+ assert_eq!(r.seek(SeekFrom::Current(0x10)).unwrap(), 0x8000000000000006);
+ assert_eq!(r.seek(SeekFrom::Current(0)).unwrap(), 0x8000000000000006);
+ assert!(r.seek(SeekFrom::Current(0x7ffffffffffffffd)).is_err());
+ assert_eq!(r.seek(SeekFrom::Current(-0x8000000000000000)).unwrap(), 6);
+
+ let mut r = Cursor::new(vec![10]);
+ assert_eq!(r.seek(SeekFrom::Start(6)).unwrap(), 6);
+ assert_eq!(r.seek(SeekFrom::Current(0x7ffffffffffffff0)).unwrap(), 0x7ffffffffffffff6);
+ assert_eq!(r.seek(SeekFrom::Current(0x10)).unwrap(), 0x8000000000000006);
+ assert_eq!(r.seek(SeekFrom::Current(0)).unwrap(), 0x8000000000000006);
+ assert!(r.seek(SeekFrom::Current(0x7ffffffffffffffd)).is_err());
+ assert_eq!(r.seek(SeekFrom::Current(-0x8000000000000000)).unwrap(), 6);
+
+ let mut buf = [0];
+ let mut r = Cursor::new(&mut buf[..]);
+ assert_eq!(r.seek(SeekFrom::Start(6)).unwrap(), 6);
+ assert_eq!(r.seek(SeekFrom::Current(0x7ffffffffffffff0)).unwrap(), 0x7ffffffffffffff6);
+ assert_eq!(r.seek(SeekFrom::Current(0x10)).unwrap(), 0x8000000000000006);
+ assert_eq!(r.seek(SeekFrom::Current(0)).unwrap(), 0x8000000000000006);
+ assert!(r.seek(SeekFrom::Current(0x7ffffffffffffffd)).is_err());
+ assert_eq!(r.seek(SeekFrom::Current(-0x8000000000000000)).unwrap(), 6);
+
+ let mut r = Cursor::new(vec![10].into_boxed_slice());
+ assert_eq!(r.seek(SeekFrom::Start(6)).unwrap(), 6);
+ assert_eq!(r.seek(SeekFrom::Current(0x7ffffffffffffff0)).unwrap(), 0x7ffffffffffffff6);
+ assert_eq!(r.seek(SeekFrom::Current(0x10)).unwrap(), 0x8000000000000006);
+ assert_eq!(r.seek(SeekFrom::Current(0)).unwrap(), 0x8000000000000006);
+ assert!(r.seek(SeekFrom::Current(0x7ffffffffffffffd)).is_err());
+ assert_eq!(r.seek(SeekFrom::Current(-0x8000000000000000)).unwrap(), 6);
+}
+
+#[test]
+fn seek_before_0() {
+ let buf = [0xff];
+ let mut r = Cursor::new(&buf[..]);
+ assert!(r.seek(SeekFrom::End(-2)).is_err());
+
+ let mut r = Cursor::new(vec![10]);
+ assert!(r.seek(SeekFrom::End(-2)).is_err());
+
+ let mut buf = [0];
+ let mut r = Cursor::new(&mut buf[..]);
+ assert!(r.seek(SeekFrom::End(-2)).is_err());
+
+ let mut r = Cursor::new(vec![10].into_boxed_slice());
+ assert!(r.seek(SeekFrom::End(-2)).is_err());
+}
+
+#[test]
+fn test_seekable_mem_writer() {
+ let mut writer = Cursor::new(Vec::<u8>::new());
+ assert_eq!(writer.position(), 0);
+ assert_eq!(writer.write(&[0]).unwrap(), 1);
+ assert_eq!(writer.position(), 1);
+ assert_eq!(writer.write(&[1, 2, 3]).unwrap(), 3);
+ assert_eq!(writer.write(&[4, 5, 6, 7]).unwrap(), 4);
+ assert_eq!(writer.position(), 8);
+ let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7];
+ assert_eq!(&writer.get_ref()[..], b);
+
+ assert_eq!(writer.seek(SeekFrom::Start(0)).unwrap(), 0);
+ assert_eq!(writer.position(), 0);
+ assert_eq!(writer.write(&[3, 4]).unwrap(), 2);
+ let b: &[_] = &[3, 4, 2, 3, 4, 5, 6, 7];
+ assert_eq!(&writer.get_ref()[..], b);
+
+ assert_eq!(writer.seek(SeekFrom::Current(1)).unwrap(), 3);
+ assert_eq!(writer.write(&[0, 1]).unwrap(), 2);
+ let b: &[_] = &[3, 4, 2, 0, 1, 5, 6, 7];
+ assert_eq!(&writer.get_ref()[..], b);
+
+ assert_eq!(writer.seek(SeekFrom::End(-1)).unwrap(), 7);
+ assert_eq!(writer.write(&[1, 2]).unwrap(), 2);
+ let b: &[_] = &[3, 4, 2, 0, 1, 5, 6, 1, 2];
+ assert_eq!(&writer.get_ref()[..], b);
+
+ assert_eq!(writer.seek(SeekFrom::End(1)).unwrap(), 10);
+ assert_eq!(writer.write(&[1]).unwrap(), 1);
+ let b: &[_] = &[3, 4, 2, 0, 1, 5, 6, 1, 2, 0, 1];
+ assert_eq!(&writer.get_ref()[..], b);
+}
+
+#[test]
+fn vec_seek_past_end() {
+ let mut r = Cursor::new(Vec::new());
+ assert_eq!(r.seek(SeekFrom::Start(10)).unwrap(), 10);
+ assert_eq!(r.write(&[3]).unwrap(), 1);
+}
+
+#[test]
+fn vec_seek_before_0() {
+ let mut r = Cursor::new(Vec::new());
+ assert!(r.seek(SeekFrom::End(-2)).is_err());
+}
+
+#[test]
+#[cfg(target_pointer_width = "32")]
+fn vec_seek_and_write_past_usize_max() {
+ let mut c = Cursor::new(Vec::new());
+ c.set_position(usize::MAX as u64 + 1);
+ assert!(c.write_all(&[1, 2, 3]).is_err());
+}
+
+#[test]
+fn test_partial_eq() {
+ assert_eq!(Cursor::new(Vec::<u8>::new()), Cursor::new(Vec::<u8>::new()));
+}
+
+#[test]
+fn test_eq() {
+ struct AssertEq<T: Eq>(pub T);
+
+ let _: AssertEq<Cursor<Vec<u8>>> = AssertEq(Cursor::new(Vec::new()));
+}
+
+#[allow(dead_code)]
+fn const_cursor() {
+ const CURSOR: Cursor<&[u8]> = Cursor::new(&[0]);
+ const _: &&[u8] = CURSOR.get_ref();
+ const _: u64 = CURSOR.position();
+}
+
+#[bench]
+fn bench_write_vec(b: &mut test::Bencher) {
+ let slice = &[1; 128];
+
+ b.iter(|| {
+ let mut buf = b"some random data to overwrite".to_vec();
+ let mut cursor = Cursor::new(&mut buf);
+
+ let _ = cursor.write_all(slice);
+ test::black_box(&cursor);
+ })
+}
+
+#[bench]
+fn bench_write_vec_vectored(b: &mut test::Bencher) {
+ let slices = [
+ IoSlice::new(&[1; 128]),
+ IoSlice::new(&[2; 256]),
+ IoSlice::new(&[3; 512]),
+ IoSlice::new(&[4; 1024]),
+ IoSlice::new(&[5; 2048]),
+ IoSlice::new(&[6; 4096]),
+ IoSlice::new(&[7; 8192]),
+ IoSlice::new(&[8; 8192 * 2]),
+ ];
+
+ b.iter(|| {
+ let mut buf = b"some random data to overwrite".to_vec();
+ let mut cursor = Cursor::new(&mut buf);
+
+ let mut slices = slices;
+ let _ = cursor.write_all_vectored(&mut slices);
+ test::black_box(&cursor);
+ })
+}
diff --git a/library/std/src/io/error.rs b/library/std/src/io/error.rs
new file mode 100644
index 000000000..ff7fdcae1
--- /dev/null
+++ b/library/std/src/io/error.rs
@@ -0,0 +1,960 @@
+#[cfg(test)]
+mod tests;
+
+#[cfg(target_pointer_width = "64")]
+mod repr_bitpacked;
+#[cfg(target_pointer_width = "64")]
+use repr_bitpacked::Repr;
+
+#[cfg(not(target_pointer_width = "64"))]
+mod repr_unpacked;
+#[cfg(not(target_pointer_width = "64"))]
+use repr_unpacked::Repr;
+
+use crate::convert::From;
+use crate::error;
+use crate::fmt;
+use crate::result;
+use crate::sys;
+
+/// A specialized [`Result`] type for I/O operations.
+///
+/// This type is broadly used across [`std::io`] for any operation which may
+/// produce an error.
+///
+/// This typedef is generally used to avoid writing out [`io::Error`] directly and
+/// is otherwise a direct mapping to [`Result`].
+///
+/// While usual Rust style is to import types directly, aliases of [`Result`]
+/// often are not, to make it easier to distinguish between them. [`Result`] is
+/// generally assumed to be [`std::result::Result`][`Result`], and so users of this alias
+/// will generally use `io::Result` instead of shadowing the [prelude]'s import
+/// of [`std::result::Result`][`Result`].
+///
+/// [`std::io`]: crate::io
+/// [`io::Error`]: Error
+/// [`Result`]: crate::result::Result
+/// [prelude]: crate::prelude
+///
+/// # Examples
+///
+/// A convenience function that bubbles an `io::Result` to its caller:
+///
+/// ```
+/// use std::io;
+///
+/// fn get_string() -> io::Result<String> {
+/// let mut buffer = String::new();
+///
+/// io::stdin().read_line(&mut buffer)?;
+///
+/// Ok(buffer)
+/// }
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+pub type Result<T> = result::Result<T, Error>;
+
+/// The error type for I/O operations of the [`Read`], [`Write`], [`Seek`], and
+/// associated traits.
+///
+/// Errors mostly originate from the underlying OS, but custom instances of
+/// `Error` can be created with crafted error messages and a particular value of
+/// [`ErrorKind`].
+///
+/// [`Read`]: crate::io::Read
+/// [`Write`]: crate::io::Write
+/// [`Seek`]: crate::io::Seek
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct Error {
+ repr: Repr,
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl fmt::Debug for Error {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt::Debug::fmt(&self.repr, f)
+ }
+}
+
+// Only derive debug in tests, to make sure it
+// doesn't accidentally get printed.
+#[cfg_attr(test, derive(Debug))]
+enum ErrorData<C> {
+ Os(i32),
+ Simple(ErrorKind),
+ SimpleMessage(&'static SimpleMessage),
+ Custom(C),
+}
+
+// `#[repr(align(4))]` is probably redundant, it should have that value or
+// higher already. We include it just because repr_bitpacked.rs's encoding
+// requires an alignment >= 4 (note that `#[repr(align)]` will not reduce the
+// alignment required by the struct, only increase it).
+//
+// If we add more variants to ErrorData, this can be increased to 8, but it
+// should probably be behind `#[cfg_attr(target_pointer_width = "64", ...)]` or
+// whatever cfg we're using to enable the `repr_bitpacked` code, since only the
+// that version needs the alignment, and 8 is higher than the alignment we'll
+// have on 32 bit platforms.
+//
+// (For the sake of being explicit: the alignment requirement here only matters
+// if `error/repr_bitpacked.rs` is in use — for the unpacked repr it doesn't
+// matter at all)
+#[repr(align(4))]
+#[derive(Debug)]
+pub(crate) struct SimpleMessage {
+ kind: ErrorKind,
+ message: &'static str,
+}
+
+impl SimpleMessage {
+ pub(crate) const fn new(kind: ErrorKind, message: &'static str) -> Self {
+ Self { kind, message }
+ }
+}
+
+/// Create and return an `io::Error` for a given `ErrorKind` and constant
+/// message. This doesn't allocate.
+pub(crate) macro const_io_error($kind:expr, $message:expr $(,)?) {
+ $crate::io::error::Error::from_static_message({
+ const MESSAGE_DATA: $crate::io::error::SimpleMessage =
+ $crate::io::error::SimpleMessage::new($kind, $message);
+ &MESSAGE_DATA
+ })
+}
+
+// As with `SimpleMessage`: `#[repr(align(4))]` here is just because
+// repr_bitpacked's encoding requires it. In practice it almost certainly be
+// already be this high or higher.
+#[derive(Debug)]
+#[repr(align(4))]
+struct Custom {
+ kind: ErrorKind,
+ error: Box<dyn error::Error + Send + Sync>,
+}
+
+/// A list specifying general categories of I/O error.
+///
+/// This list is intended to grow over time and it is not recommended to
+/// exhaustively match against it.
+///
+/// It is used with the [`io::Error`] type.
+///
+/// [`io::Error`]: Error
+///
+/// # Handling errors and matching on `ErrorKind`
+///
+/// In application code, use `match` for the `ErrorKind` values you are
+/// expecting; use `_` to match "all other errors".
+///
+/// In comprehensive and thorough tests that want to verify that a test doesn't
+/// return any known incorrect error kind, you may want to cut-and-paste the
+/// current full list of errors from here into your test code, and then match
+/// `_` as the correct case. This seems counterintuitive, but it will make your
+/// tests more robust. In particular, if you want to verify that your code does
+/// produce an unrecognized error kind, the robust solution is to check for all
+/// the recognized error kinds and fail in those cases.
+#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
+#[stable(feature = "rust1", since = "1.0.0")]
+#[allow(deprecated)]
+#[non_exhaustive]
+pub enum ErrorKind {
+ /// An entity was not found, often a file.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ NotFound,
+ /// The operation lacked the necessary privileges to complete.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ PermissionDenied,
+ /// The connection was refused by the remote server.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ ConnectionRefused,
+ /// The connection was reset by the remote server.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ ConnectionReset,
+ /// The remote host is not reachable.
+ #[unstable(feature = "io_error_more", issue = "86442")]
+ HostUnreachable,
+ /// The network containing the remote host is not reachable.
+ #[unstable(feature = "io_error_more", issue = "86442")]
+ NetworkUnreachable,
+ /// The connection was aborted (terminated) by the remote server.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ ConnectionAborted,
+ /// The network operation failed because it was not connected yet.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ NotConnected,
+ /// A socket address could not be bound because the address is already in
+ /// use elsewhere.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ AddrInUse,
+ /// A nonexistent interface was requested or the requested address was not
+ /// local.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ AddrNotAvailable,
+ /// The system's networking is down.
+ #[unstable(feature = "io_error_more", issue = "86442")]
+ NetworkDown,
+ /// The operation failed because a pipe was closed.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ BrokenPipe,
+ /// An entity already exists, often a file.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ AlreadyExists,
+ /// The operation needs to block to complete, but the blocking operation was
+ /// requested to not occur.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ WouldBlock,
+ /// A filesystem object is, unexpectedly, not a directory.
+ ///
+ /// For example, a filesystem path was specified where one of the intermediate directory
+ /// components was, in fact, a plain file.
+ #[unstable(feature = "io_error_more", issue = "86442")]
+ NotADirectory,
+ /// The filesystem object is, unexpectedly, a directory.
+ ///
+ /// A directory was specified when a non-directory was expected.
+ #[unstable(feature = "io_error_more", issue = "86442")]
+ IsADirectory,
+ /// A non-empty directory was specified where an empty directory was expected.
+ #[unstable(feature = "io_error_more", issue = "86442")]
+ DirectoryNotEmpty,
+ /// The filesystem or storage medium is read-only, but a write operation was attempted.
+ #[unstable(feature = "io_error_more", issue = "86442")]
+ ReadOnlyFilesystem,
+ /// Loop in the filesystem or IO subsystem; often, too many levels of symbolic links.
+ ///
+ /// There was a loop (or excessively long chain) resolving a filesystem object
+ /// or file IO object.
+ ///
+ /// On Unix this is usually the result of a symbolic link loop; or, of exceeding the
+ /// system-specific limit on the depth of symlink traversal.
+ #[unstable(feature = "io_error_more", issue = "86442")]
+ FilesystemLoop,
+ /// Stale network file handle.
+ ///
+ /// With some network filesystems, notably NFS, an open file (or directory) can be invalidated
+ /// by problems with the network or server.
+ #[unstable(feature = "io_error_more", issue = "86442")]
+ StaleNetworkFileHandle,
+ /// A parameter was incorrect.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ InvalidInput,
+ /// Data not valid for the operation were encountered.
+ ///
+ /// Unlike [`InvalidInput`], this typically means that the operation
+ /// parameters were valid, however the error was caused by malformed
+ /// input data.
+ ///
+ /// For example, a function that reads a file into a string will error with
+ /// `InvalidData` if the file's contents are not valid UTF-8.
+ ///
+ /// [`InvalidInput`]: ErrorKind::InvalidInput
+ #[stable(feature = "io_invalid_data", since = "1.2.0")]
+ InvalidData,
+ /// The I/O operation's timeout expired, causing it to be canceled.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ TimedOut,
+ /// An error returned when an operation could not be completed because a
+ /// call to [`write`] returned [`Ok(0)`].
+ ///
+ /// This typically means that an operation could only succeed if it wrote a
+ /// particular number of bytes but only a smaller number of bytes could be
+ /// written.
+ ///
+ /// [`write`]: crate::io::Write::write
+ /// [`Ok(0)`]: Ok
+ #[stable(feature = "rust1", since = "1.0.0")]
+ WriteZero,
+ /// The underlying storage (typically, a filesystem) is full.
+ ///
+ /// This does not include out of quota errors.
+ #[unstable(feature = "io_error_more", issue = "86442")]
+ StorageFull,
+ /// Seek on unseekable file.
+ ///
+ /// Seeking was attempted on an open file handle which is not suitable for seeking - for
+ /// example, on Unix, a named pipe opened with `File::open`.
+ #[unstable(feature = "io_error_more", issue = "86442")]
+ NotSeekable,
+ /// Filesystem quota was exceeded.
+ #[unstable(feature = "io_error_more", issue = "86442")]
+ FilesystemQuotaExceeded,
+ /// File larger than allowed or supported.
+ ///
+ /// This might arise from a hard limit of the underlying filesystem or file access API, or from
+ /// an administratively imposed resource limitation. Simple disk full, and out of quota, have
+ /// their own errors.
+ #[unstable(feature = "io_error_more", issue = "86442")]
+ FileTooLarge,
+ /// Resource is busy.
+ #[unstable(feature = "io_error_more", issue = "86442")]
+ ResourceBusy,
+ /// Executable file is busy.
+ ///
+ /// An attempt was made to write to a file which is also in use as a running program. (Not all
+ /// operating systems detect this situation.)
+ #[unstable(feature = "io_error_more", issue = "86442")]
+ ExecutableFileBusy,
+ /// Deadlock (avoided).
+ ///
+ /// A file locking operation would result in deadlock. This situation is typically detected, if
+ /// at all, on a best-effort basis.
+ #[unstable(feature = "io_error_more", issue = "86442")]
+ Deadlock,
+ /// Cross-device or cross-filesystem (hard) link or rename.
+ #[unstable(feature = "io_error_more", issue = "86442")]
+ CrossesDevices,
+ /// Too many (hard) links to the same filesystem object.
+ ///
+ /// The filesystem does not support making so many hardlinks to the same file.
+ #[unstable(feature = "io_error_more", issue = "86442")]
+ TooManyLinks,
+ /// A filename was invalid.
+ ///
+ /// This error can also cause if it exceeded the filename length limit.
+ #[unstable(feature = "io_error_more", issue = "86442")]
+ InvalidFilename,
+ /// Program argument list too long.
+ ///
+ /// When trying to run an external program, a system or process limit on the size of the
+ /// arguments would have been exceeded.
+ #[unstable(feature = "io_error_more", issue = "86442")]
+ ArgumentListTooLong,
+ /// This operation was interrupted.
+ ///
+ /// Interrupted operations can typically be retried.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ Interrupted,
+
+ /// This operation is unsupported on this platform.
+ ///
+ /// This means that the operation can never succeed.
+ #[stable(feature = "unsupported_error", since = "1.53.0")]
+ Unsupported,
+
+ // ErrorKinds which are primarily categorisations for OS error
+ // codes should be added above.
+ //
+ /// An error returned when an operation could not be completed because an
+ /// "end of file" was reached prematurely.
+ ///
+ /// This typically means that an operation could only succeed if it read a
+ /// particular number of bytes but only a smaller number of bytes could be
+ /// read.
+ #[stable(feature = "read_exact", since = "1.6.0")]
+ UnexpectedEof,
+
+ /// An operation could not be completed, because it failed
+ /// to allocate enough memory.
+ #[stable(feature = "out_of_memory_error", since = "1.54.0")]
+ OutOfMemory,
+
+ // "Unusual" error kinds which do not correspond simply to (sets
+ // of) OS error codes, should be added just above this comment.
+ // `Other` and `Uncategorised` should remain at the end:
+ //
+ /// A custom error that does not fall under any other I/O error kind.
+ ///
+ /// This can be used to construct your own [`Error`]s that do not match any
+ /// [`ErrorKind`].
+ ///
+ /// This [`ErrorKind`] is not used by the standard library.
+ ///
+ /// Errors from the standard library that do not fall under any of the I/O
+ /// error kinds cannot be `match`ed on, and will only match a wildcard (`_`) pattern.
+ /// New [`ErrorKind`]s might be added in the future for some of those.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ Other,
+
+ /// Any I/O error from the standard library that's not part of this list.
+ ///
+ /// Errors that are `Uncategorized` now may move to a different or a new
+ /// [`ErrorKind`] variant in the future. It is not recommended to match
+ /// an error against `Uncategorized`; use a wildcard match (`_`) instead.
+ #[unstable(feature = "io_error_uncategorized", issue = "none")]
+ #[doc(hidden)]
+ Uncategorized,
+}
+
+impl ErrorKind {
+ pub(crate) fn as_str(&self) -> &'static str {
+ use ErrorKind::*;
+ // Strictly alphabetical, please. (Sadly rustfmt cannot do this yet.)
+ match *self {
+ AddrInUse => "address in use",
+ AddrNotAvailable => "address not available",
+ AlreadyExists => "entity already exists",
+ ArgumentListTooLong => "argument list too long",
+ BrokenPipe => "broken pipe",
+ ConnectionAborted => "connection aborted",
+ ConnectionRefused => "connection refused",
+ ConnectionReset => "connection reset",
+ CrossesDevices => "cross-device link or rename",
+ Deadlock => "deadlock",
+ DirectoryNotEmpty => "directory not empty",
+ ExecutableFileBusy => "executable file busy",
+ FileTooLarge => "file too large",
+ FilesystemLoop => "filesystem loop or indirection limit (e.g. symlink loop)",
+ FilesystemQuotaExceeded => "filesystem quota exceeded",
+ HostUnreachable => "host unreachable",
+ Interrupted => "operation interrupted",
+ InvalidData => "invalid data",
+ InvalidFilename => "invalid filename",
+ InvalidInput => "invalid input parameter",
+ IsADirectory => "is a directory",
+ NetworkDown => "network down",
+ NetworkUnreachable => "network unreachable",
+ NotADirectory => "not a directory",
+ NotConnected => "not connected",
+ NotFound => "entity not found",
+ NotSeekable => "seek on unseekable file",
+ Other => "other error",
+ OutOfMemory => "out of memory",
+ PermissionDenied => "permission denied",
+ ReadOnlyFilesystem => "read-only filesystem or storage medium",
+ ResourceBusy => "resource busy",
+ StaleNetworkFileHandle => "stale network file handle",
+ StorageFull => "no storage space",
+ TimedOut => "timed out",
+ TooManyLinks => "too many links",
+ Uncategorized => "uncategorized error",
+ UnexpectedEof => "unexpected end of file",
+ Unsupported => "unsupported",
+ WouldBlock => "operation would block",
+ WriteZero => "write zero",
+ }
+ }
+}
+
+#[stable(feature = "io_errorkind_display", since = "1.60.0")]
+impl fmt::Display for ErrorKind {
+ /// Shows a human-readable description of the `ErrorKind`.
+ ///
+ /// This is similar to `impl Display for Error`, but doesn't require first converting to Error.
+ ///
+ /// # Examples
+ /// ```
+ /// use std::io::ErrorKind;
+ /// assert_eq!("entity not found", ErrorKind::NotFound.to_string());
+ /// ```
+ fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt.write_str(self.as_str())
+ }
+}
+
+/// Intended for use for errors not exposed to the user, where allocating onto
+/// the heap (for normal construction via Error::new) is too costly.
+#[stable(feature = "io_error_from_errorkind", since = "1.14.0")]
+impl From<ErrorKind> for Error {
+ /// Converts an [`ErrorKind`] into an [`Error`].
+ ///
+ /// This conversion creates a new error with a simple representation of error kind.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::{Error, ErrorKind};
+ ///
+ /// let not_found = ErrorKind::NotFound;
+ /// let error = Error::from(not_found);
+ /// assert_eq!("entity not found", format!("{error}"));
+ /// ```
+ #[inline]
+ fn from(kind: ErrorKind) -> Error {
+ Error { repr: Repr::new_simple(kind) }
+ }
+}
+
+impl Error {
+ /// Creates a new I/O error from a known kind of error as well as an
+ /// arbitrary error payload.
+ ///
+ /// This function is used to generically create I/O errors which do not
+ /// originate from the OS itself. The `error` argument is an arbitrary
+ /// payload which will be contained in this [`Error`].
+ ///
+ /// If no extra payload is required, use the `From` conversion from
+ /// `ErrorKind`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::{Error, ErrorKind};
+ ///
+ /// // errors can be created from strings
+ /// let custom_error = Error::new(ErrorKind::Other, "oh no!");
+ ///
+ /// // errors can also be created from other errors
+ /// let custom_error2 = Error::new(ErrorKind::Interrupted, custom_error);
+ ///
+ /// // creating an error without payload
+ /// let eof_error = Error::from(ErrorKind::UnexpectedEof);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn new<E>(kind: ErrorKind, error: E) -> Error
+ where
+ E: Into<Box<dyn error::Error + Send + Sync>>,
+ {
+ Self::_new(kind, error.into())
+ }
+
+ /// Creates a new I/O error from an arbitrary error payload.
+ ///
+ /// This function is used to generically create I/O errors which do not
+ /// originate from the OS itself. It is a shortcut for [`Error::new`]
+ /// with [`ErrorKind::Other`].
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// #![feature(io_error_other)]
+ ///
+ /// use std::io::Error;
+ ///
+ /// // errors can be created from strings
+ /// let custom_error = Error::other("oh no!");
+ ///
+ /// // errors can also be created from other errors
+ /// let custom_error2 = Error::other(custom_error);
+ /// ```
+ #[unstable(feature = "io_error_other", issue = "91946")]
+ pub fn other<E>(error: E) -> Error
+ where
+ E: Into<Box<dyn error::Error + Send + Sync>>,
+ {
+ Self::_new(ErrorKind::Other, error.into())
+ }
+
+ fn _new(kind: ErrorKind, error: Box<dyn error::Error + Send + Sync>) -> Error {
+ Error { repr: Repr::new_custom(Box::new(Custom { kind, error })) }
+ }
+
+ /// Creates a new I/O error from a known kind of error as well as a constant
+ /// message.
+ ///
+ /// This function does not allocate.
+ ///
+ /// You should not use this directly, and instead use the `const_io_error!`
+ /// macro: `io::const_io_error!(ErrorKind::Something, "some_message")`.
+ ///
+ /// This function should maybe change to `from_static_message<const MSG: &'static
+ /// str>(kind: ErrorKind)` in the future, when const generics allow that.
+ #[inline]
+ pub(crate) const fn from_static_message(msg: &'static SimpleMessage) -> Error {
+ Self { repr: Repr::new_simple_message(msg) }
+ }
+
+ /// Returns an error representing the last OS error which occurred.
+ ///
+ /// This function reads the value of `errno` for the target platform (e.g.
+ /// `GetLastError` on Windows) and will return a corresponding instance of
+ /// [`Error`] for the error code.
+ ///
+ /// This should be called immediately after a call to a platform function,
+ /// otherwise the state of the error value is indeterminate. In particular,
+ /// other standard library functions may call platform functions that may
+ /// (or may not) reset the error value even if they succeed.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::Error;
+ ///
+ /// let os_error = Error::last_os_error();
+ /// println!("last OS error: {os_error:?}");
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[must_use]
+ #[inline]
+ pub fn last_os_error() -> Error {
+ Error::from_raw_os_error(sys::os::errno() as i32)
+ }
+
+ /// Creates a new instance of an [`Error`] from a particular OS error code.
+ ///
+ /// # Examples
+ ///
+ /// On Linux:
+ ///
+ /// ```
+ /// # if cfg!(target_os = "linux") {
+ /// use std::io;
+ ///
+ /// let error = io::Error::from_raw_os_error(22);
+ /// assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
+ /// # }
+ /// ```
+ ///
+ /// On Windows:
+ ///
+ /// ```
+ /// # if cfg!(windows) {
+ /// use std::io;
+ ///
+ /// let error = io::Error::from_raw_os_error(10022);
+ /// assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
+ /// # }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[must_use]
+ #[inline]
+ pub fn from_raw_os_error(code: i32) -> Error {
+ Error { repr: Repr::new_os(code) }
+ }
+
+ /// Returns the OS error that this error represents (if any).
+ ///
+ /// If this [`Error`] was constructed via [`last_os_error`] or
+ /// [`from_raw_os_error`], then this function will return [`Some`], otherwise
+ /// it will return [`None`].
+ ///
+ /// [`last_os_error`]: Error::last_os_error
+ /// [`from_raw_os_error`]: Error::from_raw_os_error
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::{Error, ErrorKind};
+ ///
+ /// fn print_os_error(err: &Error) {
+ /// if let Some(raw_os_err) = err.raw_os_error() {
+ /// println!("raw OS error: {raw_os_err:?}");
+ /// } else {
+ /// println!("Not an OS error");
+ /// }
+ /// }
+ ///
+ /// fn main() {
+ /// // Will print "raw OS error: ...".
+ /// print_os_error(&Error::last_os_error());
+ /// // Will print "Not an OS error".
+ /// print_os_error(&Error::new(ErrorKind::Other, "oh no!"));
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[must_use]
+ #[inline]
+ pub fn raw_os_error(&self) -> Option<i32> {
+ match self.repr.data() {
+ ErrorData::Os(i) => Some(i),
+ ErrorData::Custom(..) => None,
+ ErrorData::Simple(..) => None,
+ ErrorData::SimpleMessage(..) => None,
+ }
+ }
+
+ /// Returns a reference to the inner error wrapped by this error (if any).
+ ///
+ /// If this [`Error`] was constructed via [`new`] then this function will
+ /// return [`Some`], otherwise it will return [`None`].
+ ///
+ /// [`new`]: Error::new
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::{Error, ErrorKind};
+ ///
+ /// fn print_error(err: &Error) {
+ /// if let Some(inner_err) = err.get_ref() {
+ /// println!("Inner error: {inner_err:?}");
+ /// } else {
+ /// println!("No inner error");
+ /// }
+ /// }
+ ///
+ /// fn main() {
+ /// // Will print "No inner error".
+ /// print_error(&Error::last_os_error());
+ /// // Will print "Inner error: ...".
+ /// print_error(&Error::new(ErrorKind::Other, "oh no!"));
+ /// }
+ /// ```
+ #[stable(feature = "io_error_inner", since = "1.3.0")]
+ #[must_use]
+ #[inline]
+ pub fn get_ref(&self) -> Option<&(dyn error::Error + Send + Sync + 'static)> {
+ match self.repr.data() {
+ ErrorData::Os(..) => None,
+ ErrorData::Simple(..) => None,
+ ErrorData::SimpleMessage(..) => None,
+ ErrorData::Custom(c) => Some(&*c.error),
+ }
+ }
+
+ /// Returns a mutable reference to the inner error wrapped by this error
+ /// (if any).
+ ///
+ /// If this [`Error`] was constructed via [`new`] then this function will
+ /// return [`Some`], otherwise it will return [`None`].
+ ///
+ /// [`new`]: Error::new
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::{Error, ErrorKind};
+ /// use std::{error, fmt};
+ /// use std::fmt::Display;
+ ///
+ /// #[derive(Debug)]
+ /// struct MyError {
+ /// v: String,
+ /// }
+ ///
+ /// impl MyError {
+ /// fn new() -> MyError {
+ /// MyError {
+ /// v: "oh no!".to_string()
+ /// }
+ /// }
+ ///
+ /// fn change_message(&mut self, new_message: &str) {
+ /// self.v = new_message.to_string();
+ /// }
+ /// }
+ ///
+ /// impl error::Error for MyError {}
+ ///
+ /// impl Display for MyError {
+ /// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ /// write!(f, "MyError: {}", &self.v)
+ /// }
+ /// }
+ ///
+ /// fn change_error(mut err: Error) -> Error {
+ /// if let Some(inner_err) = err.get_mut() {
+ /// inner_err.downcast_mut::<MyError>().unwrap().change_message("I've been changed!");
+ /// }
+ /// err
+ /// }
+ ///
+ /// fn print_error(err: &Error) {
+ /// if let Some(inner_err) = err.get_ref() {
+ /// println!("Inner error: {inner_err}");
+ /// } else {
+ /// println!("No inner error");
+ /// }
+ /// }
+ ///
+ /// fn main() {
+ /// // Will print "No inner error".
+ /// print_error(&change_error(Error::last_os_error()));
+ /// // Will print "Inner error: ...".
+ /// print_error(&change_error(Error::new(ErrorKind::Other, MyError::new())));
+ /// }
+ /// ```
+ #[stable(feature = "io_error_inner", since = "1.3.0")]
+ #[must_use]
+ #[inline]
+ pub fn get_mut(&mut self) -> Option<&mut (dyn error::Error + Send + Sync + 'static)> {
+ match self.repr.data_mut() {
+ ErrorData::Os(..) => None,
+ ErrorData::Simple(..) => None,
+ ErrorData::SimpleMessage(..) => None,
+ ErrorData::Custom(c) => Some(&mut *c.error),
+ }
+ }
+
+ /// Consumes the `Error`, returning its inner error (if any).
+ ///
+ /// If this [`Error`] was constructed via [`new`] then this function will
+ /// return [`Some`], otherwise it will return [`None`].
+ ///
+ /// [`new`]: Error::new
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::{Error, ErrorKind};
+ ///
+ /// fn print_error(err: Error) {
+ /// if let Some(inner_err) = err.into_inner() {
+ /// println!("Inner error: {inner_err}");
+ /// } else {
+ /// println!("No inner error");
+ /// }
+ /// }
+ ///
+ /// fn main() {
+ /// // Will print "No inner error".
+ /// print_error(Error::last_os_error());
+ /// // Will print "Inner error: ...".
+ /// print_error(Error::new(ErrorKind::Other, "oh no!"));
+ /// }
+ /// ```
+ #[stable(feature = "io_error_inner", since = "1.3.0")]
+ #[must_use = "`self` will be dropped if the result is not used"]
+ #[inline]
+ pub fn into_inner(self) -> Option<Box<dyn error::Error + Send + Sync>> {
+ match self.repr.into_data() {
+ ErrorData::Os(..) => None,
+ ErrorData::Simple(..) => None,
+ ErrorData::SimpleMessage(..) => None,
+ ErrorData::Custom(c) => Some(c.error),
+ }
+ }
+
+ /// Attempt to downgrade the inner error to `E` if any.
+ ///
+ /// If this [`Error`] was constructed via [`new`] then this function will
+ /// attempt to perform downgrade on it, otherwise it will return [`Err`].
+ ///
+ /// If downgrade succeeds, it will return [`Ok`], otherwise it will also
+ /// return [`Err`].
+ ///
+ /// [`new`]: Error::new
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// #![feature(io_error_downcast)]
+ ///
+ /// use std::fmt;
+ /// use std::io;
+ /// use std::error::Error;
+ ///
+ /// #[derive(Debug)]
+ /// enum E {
+ /// Io(io::Error),
+ /// SomeOtherVariant,
+ /// }
+ ///
+ /// impl fmt::Display for E {
+ /// // ...
+ /// # fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ /// # todo!()
+ /// # }
+ /// }
+ /// impl Error for E {}
+ ///
+ /// impl From<io::Error> for E {
+ /// fn from(err: io::Error) -> E {
+ /// err.downcast::<E>()
+ /// .map(|b| *b)
+ /// .unwrap_or_else(E::Io)
+ /// }
+ /// }
+ /// ```
+ #[unstable(feature = "io_error_downcast", issue = "99262")]
+ pub fn downcast<E>(self) -> result::Result<Box<E>, Self>
+ where
+ E: error::Error + Send + Sync + 'static,
+ {
+ match self.repr.into_data() {
+ ErrorData::Custom(b) if b.error.is::<E>() => {
+ let res = (*b).error.downcast::<E>();
+
+ // downcast is a really trivial and is marked as inline, so
+ // it's likely be inlined here.
+ //
+ // And the compiler should be able to eliminate the branch
+ // that produces `Err` here since b.error.is::<E>()
+ // returns true.
+ Ok(res.unwrap())
+ }
+ repr_data => Err(Self { repr: Repr::new(repr_data) }),
+ }
+ }
+
+ /// Returns the corresponding [`ErrorKind`] for this error.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::{Error, ErrorKind};
+ ///
+ /// fn print_error(err: Error) {
+ /// println!("{:?}", err.kind());
+ /// }
+ ///
+ /// fn main() {
+ /// // Will print "Uncategorized".
+ /// print_error(Error::last_os_error());
+ /// // Will print "AddrInUse".
+ /// print_error(Error::new(ErrorKind::AddrInUse, "oh no!"));
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[must_use]
+ #[inline]
+ pub fn kind(&self) -> ErrorKind {
+ match self.repr.data() {
+ ErrorData::Os(code) => sys::decode_error_kind(code),
+ ErrorData::Custom(c) => c.kind,
+ ErrorData::Simple(kind) => kind,
+ ErrorData::SimpleMessage(m) => m.kind,
+ }
+ }
+}
+
+impl fmt::Debug for Repr {
+ fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self.data() {
+ ErrorData::Os(code) => fmt
+ .debug_struct("Os")
+ .field("code", &code)
+ .field("kind", &sys::decode_error_kind(code))
+ .field("message", &sys::os::error_string(code))
+ .finish(),
+ ErrorData::Custom(c) => fmt::Debug::fmt(&c, fmt),
+ ErrorData::Simple(kind) => fmt.debug_tuple("Kind").field(&kind).finish(),
+ ErrorData::SimpleMessage(msg) => fmt
+ .debug_struct("Error")
+ .field("kind", &msg.kind)
+ .field("message", &msg.message)
+ .finish(),
+ }
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl fmt::Display for Error {
+ fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self.repr.data() {
+ ErrorData::Os(code) => {
+ let detail = sys::os::error_string(code);
+ write!(fmt, "{detail} (os error {code})")
+ }
+ ErrorData::Custom(ref c) => c.error.fmt(fmt),
+ ErrorData::Simple(kind) => write!(fmt, "{}", kind.as_str()),
+ ErrorData::SimpleMessage(msg) => msg.message.fmt(fmt),
+ }
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl error::Error for Error {
+ #[allow(deprecated, deprecated_in_future)]
+ fn description(&self) -> &str {
+ match self.repr.data() {
+ ErrorData::Os(..) | ErrorData::Simple(..) => self.kind().as_str(),
+ ErrorData::SimpleMessage(msg) => msg.message,
+ ErrorData::Custom(c) => c.error.description(),
+ }
+ }
+
+ #[allow(deprecated)]
+ fn cause(&self) -> Option<&dyn error::Error> {
+ match self.repr.data() {
+ ErrorData::Os(..) => None,
+ ErrorData::Simple(..) => None,
+ ErrorData::SimpleMessage(..) => None,
+ ErrorData::Custom(c) => c.error.cause(),
+ }
+ }
+
+ fn source(&self) -> Option<&(dyn error::Error + 'static)> {
+ match self.repr.data() {
+ ErrorData::Os(..) => None,
+ ErrorData::Simple(..) => None,
+ ErrorData::SimpleMessage(..) => None,
+ ErrorData::Custom(c) => c.error.source(),
+ }
+ }
+}
+
+fn _assert_error_is_sync_send() {
+ fn _is_sync_send<T: Sync + Send>() {}
+ _is_sync_send::<Error>();
+}
diff --git a/library/std/src/io/error/repr_bitpacked.rs b/library/std/src/io/error/repr_bitpacked.rs
new file mode 100644
index 000000000..292bf4826
--- /dev/null
+++ b/library/std/src/io/error/repr_bitpacked.rs
@@ -0,0 +1,409 @@
+//! This is a densely packed error representation which is used on targets with
+//! 64-bit pointers.
+//!
+//! (Note that `bitpacked` vs `unpacked` here has no relationship to
+//! `#[repr(packed)]`, it just refers to attempting to use any available bits in
+//! a more clever manner than `rustc`'s default layout algorithm would).
+//!
+//! Conceptually, it stores the same data as the "unpacked" equivalent we use on
+//! other targets. Specifically, you can imagine it as an optimized version of
+//! the following enum (which is roughly equivalent to what's stored by
+//! `repr_unpacked::Repr`, e.g. `super::ErrorData<Box<Custom>>`):
+//!
+//! ```ignore (exposition-only)
+//! enum ErrorData {
+//! Os(i32),
+//! Simple(ErrorKind),
+//! SimpleMessage(&'static SimpleMessage),
+//! Custom(Box<Custom>),
+//! }
+//! ```
+//!
+//! However, it packs this data into a 64bit non-zero value.
+//!
+//! This optimization not only allows `io::Error` to occupy a single pointer,
+//! but improves `io::Result` as well, especially for situations like
+//! `io::Result<()>` (which is now 64 bits) or `io::Result<u64>` (which is now
+//! 128 bits), which are quite common.
+//!
+//! # Layout
+//! Tagged values are 64 bits, with the 2 least significant bits used for the
+//! tag. This means there are there are 4 "variants":
+//!
+//! - **Tag 0b00**: The first variant is equivalent to
+//! `ErrorData::SimpleMessage`, and holds a `&'static SimpleMessage` directly.
+//!
+//! `SimpleMessage` has an alignment >= 4 (which is requested with
+//! `#[repr(align)]` and checked statically at the bottom of this file), which
+//! means every `&'static SimpleMessage` should have the both tag bits as 0,
+//! meaning its tagged and untagged representation are equivalent.
+//!
+//! This means we can skip tagging it, which is necessary as this variant can
+//! be constructed from a `const fn`, which probably cannot tag pointers (or
+//! at least it would be difficult).
+//!
+//! - **Tag 0b01**: The other pointer variant holds the data for
+//! `ErrorData::Custom` and the remaining 62 bits are used to store a
+//! `Box<Custom>`. `Custom` also has alignment >= 4, so the bottom two bits
+//! are free to use for the tag.
+//!
+//! The only important thing to note is that `ptr::wrapping_add` and
+//! `ptr::wrapping_sub` are used to tag the pointer, rather than bitwise
+//! operations. This should preserve the pointer's provenance, which would
+//! otherwise be lost.
+//!
+//! - **Tag 0b10**: Holds the data for `ErrorData::Os(i32)`. We store the `i32`
+//! in the pointer's most significant 32 bits, and don't use the bits `2..32`
+//! for anything. Using the top 32 bits is just to let us easily recover the
+//! `i32` code with the correct sign.
+//!
+//! - **Tag 0b11**: Holds the data for `ErrorData::Simple(ErrorKind)`. This
+//! stores the `ErrorKind` in the top 32 bits as well, although it doesn't
+//! occupy nearly that many. Most of the bits are unused here, but it's not
+//! like we need them for anything else yet.
+//!
+//! # Use of `NonNull<()>`
+//!
+//! Everything is stored in a `NonNull<()>`, which is odd, but actually serves a
+//! purpose.
+//!
+//! Conceptually you might think of this more like:
+//!
+//! ```ignore (exposition-only)
+//! union Repr {
+//! // holds integer (Simple/Os) variants, and
+//! // provides access to the tag bits.
+//! bits: NonZeroU64,
+//! // Tag is 0, so this is stored untagged.
+//! msg: &'static SimpleMessage,
+//! // Tagged (offset) `Box<Custom>` pointer.
+//! tagged_custom: NonNull<()>,
+//! }
+//! ```
+//!
+//! But there are a few problems with this:
+//!
+//! 1. Union access is equivalent to a transmute, so this representation would
+//! require we transmute between integers and pointers in at least one
+//! direction, which may be UB (and even if not, it is likely harder for a
+//! compiler to reason about than explicit ptr->int operations).
+//!
+//! 2. Even if all fields of a union have a niche, the union itself doesn't,
+//! although this may change in the future. This would make things like
+//! `io::Result<()>` and `io::Result<usize>` larger, which defeats part of
+//! the motivation of this bitpacking.
+//!
+//! Storing everything in a `NonZeroUsize` (or some other integer) would be a
+//! bit more traditional for pointer tagging, but it would lose provenance
+//! information, couldn't be constructed from a `const fn`, and would probably
+//! run into other issues as well.
+//!
+//! The `NonNull<()>` seems like the only alternative, even if it's fairly odd
+//! to use a pointer type to store something that may hold an integer, some of
+//! the time.
+
+use super::{Custom, ErrorData, ErrorKind, SimpleMessage};
+use alloc::boxed::Box;
+use core::marker::PhantomData;
+use core::mem::{align_of, size_of};
+use core::ptr::{self, NonNull};
+
+// The 2 least-significant bits are used as tag.
+const TAG_MASK: usize = 0b11;
+const TAG_SIMPLE_MESSAGE: usize = 0b00;
+const TAG_CUSTOM: usize = 0b01;
+const TAG_OS: usize = 0b10;
+const TAG_SIMPLE: usize = 0b11;
+
+/// The internal representation.
+///
+/// See the module docs for more, this is just a way to hack in a check that we
+/// indeed are not unwind-safe.
+///
+/// ```compile_fail,E0277
+/// fn is_unwind_safe<T: core::panic::UnwindSafe>() {}
+/// is_unwind_safe::<std::io::Error>();
+/// ```
+#[repr(transparent)]
+pub(super) struct Repr(NonNull<()>, PhantomData<ErrorData<Box<Custom>>>);
+
+// All the types `Repr` stores internally are Send + Sync, and so is it.
+unsafe impl Send for Repr {}
+unsafe impl Sync for Repr {}
+
+impl Repr {
+ pub(super) fn new(dat: ErrorData<Box<Custom>>) -> Self {
+ match dat {
+ ErrorData::Os(code) => Self::new_os(code),
+ ErrorData::Simple(kind) => Self::new_simple(kind),
+ ErrorData::SimpleMessage(simple_message) => Self::new_simple_message(simple_message),
+ ErrorData::Custom(b) => Self::new_custom(b),
+ }
+ }
+
+ pub(super) fn new_custom(b: Box<Custom>) -> Self {
+ let p = Box::into_raw(b).cast::<u8>();
+ // Should only be possible if an allocator handed out a pointer with
+ // wrong alignment.
+ debug_assert_eq!(p.addr() & TAG_MASK, 0);
+ // Note: We know `TAG_CUSTOM <= size_of::<Custom>()` (static_assert at
+ // end of file), and both the start and end of the expression must be
+ // valid without address space wraparound due to `Box`'s semantics.
+ //
+ // This means it would be correct to implement this using `ptr::add`
+ // (rather than `ptr::wrapping_add`), but it's unclear this would give
+ // any benefit, so we just use `wrapping_add` instead.
+ let tagged = p.wrapping_add(TAG_CUSTOM).cast::<()>();
+ // Safety: `TAG_CUSTOM + p` is the same as `TAG_CUSTOM | p`,
+ // because `p`'s alignment means it isn't allowed to have any of the
+ // `TAG_BITS` set (you can verify that addition and bitwise-or are the
+ // same when the operands have no bits in common using a truth table).
+ //
+ // Then, `TAG_CUSTOM | p` is not zero, as that would require
+ // `TAG_CUSTOM` and `p` both be zero, and neither is (as `p` came from a
+ // box, and `TAG_CUSTOM` just... isn't zero -- it's `0b01`). Therefore,
+ // `TAG_CUSTOM + p` isn't zero and so `tagged` can't be, and the
+ // `new_unchecked` is safe.
+ let res = Self(unsafe { NonNull::new_unchecked(tagged) }, PhantomData);
+ // quickly smoke-check we encoded the right thing (This generally will
+ // only run in libstd's tests, unless the user uses -Zbuild-std)
+ debug_assert!(matches!(res.data(), ErrorData::Custom(_)), "repr(custom) encoding failed");
+ res
+ }
+
+ #[inline]
+ pub(super) fn new_os(code: i32) -> Self {
+ let utagged = ((code as usize) << 32) | TAG_OS;
+ // Safety: `TAG_OS` is not zero, so the result of the `|` is not 0.
+ let res = Self(unsafe { NonNull::new_unchecked(ptr::invalid_mut(utagged)) }, PhantomData);
+ // quickly smoke-check we encoded the right thing (This generally will
+ // only run in libstd's tests, unless the user uses -Zbuild-std)
+ debug_assert!(
+ matches!(res.data(), ErrorData::Os(c) if c == code),
+ "repr(os) encoding failed for {code}"
+ );
+ res
+ }
+
+ #[inline]
+ pub(super) fn new_simple(kind: ErrorKind) -> Self {
+ let utagged = ((kind as usize) << 32) | TAG_SIMPLE;
+ // Safety: `TAG_SIMPLE` is not zero, so the result of the `|` is not 0.
+ let res = Self(unsafe { NonNull::new_unchecked(ptr::invalid_mut(utagged)) }, PhantomData);
+ // quickly smoke-check we encoded the right thing (This generally will
+ // only run in libstd's tests, unless the user uses -Zbuild-std)
+ debug_assert!(
+ matches!(res.data(), ErrorData::Simple(k) if k == kind),
+ "repr(simple) encoding failed {:?}",
+ kind,
+ );
+ res
+ }
+
+ #[inline]
+ pub(super) const fn new_simple_message(m: &'static SimpleMessage) -> Self {
+ // Safety: References are never null.
+ Self(unsafe { NonNull::new_unchecked(m as *const _ as *mut ()) }, PhantomData)
+ }
+
+ #[inline]
+ pub(super) fn data(&self) -> ErrorData<&Custom> {
+ // Safety: We're a Repr, decode_repr is fine.
+ unsafe { decode_repr(self.0, |c| &*c) }
+ }
+
+ #[inline]
+ pub(super) fn data_mut(&mut self) -> ErrorData<&mut Custom> {
+ // Safety: We're a Repr, decode_repr is fine.
+ unsafe { decode_repr(self.0, |c| &mut *c) }
+ }
+
+ #[inline]
+ pub(super) fn into_data(self) -> ErrorData<Box<Custom>> {
+ let this = core::mem::ManuallyDrop::new(self);
+ // Safety: We're a Repr, decode_repr is fine. The `Box::from_raw` is
+ // safe because we prevent double-drop using `ManuallyDrop`.
+ unsafe { decode_repr(this.0, |p| Box::from_raw(p)) }
+ }
+}
+
+impl Drop for Repr {
+ #[inline]
+ fn drop(&mut self) {
+ // Safety: We're a Repr, decode_repr is fine. The `Box::from_raw` is
+ // safe because we're being dropped.
+ unsafe {
+ let _ = decode_repr(self.0, |p| Box::<Custom>::from_raw(p));
+ }
+ }
+}
+
+// Shared helper to decode a `Repr`'s internal pointer into an ErrorData.
+//
+// Safety: `ptr`'s bits should be encoded as described in the document at the
+// top (it should `some_repr.0`)
+#[inline]
+unsafe fn decode_repr<C, F>(ptr: NonNull<()>, make_custom: F) -> ErrorData<C>
+where
+ F: FnOnce(*mut Custom) -> C,
+{
+ let bits = ptr.as_ptr().addr();
+ match bits & TAG_MASK {
+ TAG_OS => {
+ let code = ((bits as i64) >> 32) as i32;
+ ErrorData::Os(code)
+ }
+ TAG_SIMPLE => {
+ let kind_bits = (bits >> 32) as u32;
+ let kind = kind_from_prim(kind_bits).unwrap_or_else(|| {
+ debug_assert!(false, "Invalid io::error::Repr bits: `Repr({:#018x})`", bits);
+ // This means the `ptr` passed in was not valid, which violates
+ // the unsafe contract of `decode_repr`.
+ //
+ // Using this rather than unwrap meaningfully improves the code
+ // for callers which only care about one variant (usually
+ // `Custom`)
+ core::hint::unreachable_unchecked();
+ });
+ ErrorData::Simple(kind)
+ }
+ TAG_SIMPLE_MESSAGE => ErrorData::SimpleMessage(&*ptr.cast::<SimpleMessage>().as_ptr()),
+ TAG_CUSTOM => {
+ // It would be correct for us to use `ptr::sub` here (see the
+ // comment above the `wrapping_add` call in `new_custom` for why),
+ // but it isn't clear that it makes a difference, so we don't.
+ let custom = ptr.as_ptr().cast::<u8>().wrapping_sub(TAG_CUSTOM).cast::<Custom>();
+ ErrorData::Custom(make_custom(custom))
+ }
+ _ => {
+ // Can't happen, and compiler can tell
+ unreachable!();
+ }
+ }
+}
+
+// This compiles to the same code as the check+transmute, but doesn't require
+// unsafe, or to hard-code max ErrorKind or its size in a way the compiler
+// couldn't verify.
+#[inline]
+fn kind_from_prim(ek: u32) -> Option<ErrorKind> {
+ macro_rules! from_prim {
+ ($prim:expr => $Enum:ident { $($Variant:ident),* $(,)? }) => {{
+ // Force a compile error if the list gets out of date.
+ const _: fn(e: $Enum) = |e: $Enum| match e {
+ $($Enum::$Variant => ()),*
+ };
+ match $prim {
+ $(v if v == ($Enum::$Variant as _) => Some($Enum::$Variant),)*
+ _ => None,
+ }
+ }}
+ }
+ from_prim!(ek => ErrorKind {
+ NotFound,
+ PermissionDenied,
+ ConnectionRefused,
+ ConnectionReset,
+ HostUnreachable,
+ NetworkUnreachable,
+ ConnectionAborted,
+ NotConnected,
+ AddrInUse,
+ AddrNotAvailable,
+ NetworkDown,
+ BrokenPipe,
+ AlreadyExists,
+ WouldBlock,
+ NotADirectory,
+ IsADirectory,
+ DirectoryNotEmpty,
+ ReadOnlyFilesystem,
+ FilesystemLoop,
+ StaleNetworkFileHandle,
+ InvalidInput,
+ InvalidData,
+ TimedOut,
+ WriteZero,
+ StorageFull,
+ NotSeekable,
+ FilesystemQuotaExceeded,
+ FileTooLarge,
+ ResourceBusy,
+ ExecutableFileBusy,
+ Deadlock,
+ CrossesDevices,
+ TooManyLinks,
+ InvalidFilename,
+ ArgumentListTooLong,
+ Interrupted,
+ Other,
+ UnexpectedEof,
+ Unsupported,
+ OutOfMemory,
+ Uncategorized,
+ })
+}
+
+// Some static checking to alert us if a change breaks any of the assumptions
+// that our encoding relies on for correctness and soundness. (Some of these are
+// a bit overly thorough/cautious, admittedly)
+//
+// If any of these are hit on a platform that libstd supports, we should likely
+// just use `repr_unpacked.rs` there instead (unless the fix is easy).
+macro_rules! static_assert {
+ ($condition:expr) => {
+ const _: () = assert!($condition);
+ };
+ (@usize_eq: $lhs:expr, $rhs:expr) => {
+ const _: [(); $lhs] = [(); $rhs];
+ };
+}
+
+// The bitpacking we use requires pointers be exactly 64 bits.
+static_assert!(@usize_eq: size_of::<NonNull<()>>(), 8);
+
+// We also require pointers and usize be the same size.
+static_assert!(@usize_eq: size_of::<NonNull<()>>(), size_of::<usize>());
+
+// `Custom` and `SimpleMessage` need to be thin pointers.
+static_assert!(@usize_eq: size_of::<&'static SimpleMessage>(), 8);
+static_assert!(@usize_eq: size_of::<Box<Custom>>(), 8);
+
+static_assert!((TAG_MASK + 1).is_power_of_two());
+// And they must have sufficient alignment.
+static_assert!(align_of::<SimpleMessage>() >= TAG_MASK + 1);
+static_assert!(align_of::<Custom>() >= TAG_MASK + 1);
+
+static_assert!(@usize_eq: (TAG_MASK & TAG_SIMPLE_MESSAGE), TAG_SIMPLE_MESSAGE);
+static_assert!(@usize_eq: (TAG_MASK & TAG_CUSTOM), TAG_CUSTOM);
+static_assert!(@usize_eq: (TAG_MASK & TAG_OS), TAG_OS);
+static_assert!(@usize_eq: (TAG_MASK & TAG_SIMPLE), TAG_SIMPLE);
+
+// This is obviously true (`TAG_CUSTOM` is `0b01`), but in `Repr::new_custom` we
+// offset a pointer by this value, and expect it to both be within the same
+// object, and to not wrap around the address space. See the comment in that
+// function for further details.
+//
+// Actually, at the moment we use `ptr::wrapping_add`, not `ptr::add`, so this
+// check isn't needed for that one, although the assertion that we don't
+// actually wrap around in that wrapping_add does simplify the safety reasoning
+// elsewhere considerably.
+static_assert!(size_of::<Custom>() >= TAG_CUSTOM);
+
+// These two store a payload which is allowed to be zero, so they must be
+// non-zero to preserve the `NonNull`'s range invariant.
+static_assert!(TAG_OS != 0);
+static_assert!(TAG_SIMPLE != 0);
+// We can't tag `SimpleMessage`s, the tag must be 0.
+static_assert!(@usize_eq: TAG_SIMPLE_MESSAGE, 0);
+
+// Check that the point of all of this still holds.
+//
+// We'd check against `io::Error`, but *technically* it's allowed to vary,
+// as it's not `#[repr(transparent)]`/`#[repr(C)]`. We could add that, but
+// the `#[repr()]` would show up in rustdoc, which might be seen as a stable
+// commitment.
+static_assert!(@usize_eq: size_of::<Repr>(), 8);
+static_assert!(@usize_eq: size_of::<Option<Repr>>(), 8);
+static_assert!(@usize_eq: size_of::<Result<(), Repr>>(), 8);
+static_assert!(@usize_eq: size_of::<Result<usize, Repr>>(), 16);
diff --git a/library/std/src/io/error/repr_unpacked.rs b/library/std/src/io/error/repr_unpacked.rs
new file mode 100644
index 000000000..d6ad55b99
--- /dev/null
+++ b/library/std/src/io/error/repr_unpacked.rs
@@ -0,0 +1,54 @@
+//! This is a fairly simple unpacked error representation that's used on
+//! non-64bit targets, where the packed 64 bit representation wouldn't work, and
+//! would have no benefit.
+
+use super::{Custom, ErrorData, ErrorKind, SimpleMessage};
+use alloc::boxed::Box;
+
+type Inner = ErrorData<Box<Custom>>;
+
+pub(super) struct Repr(Inner);
+
+impl Repr {
+ #[inline]
+ pub(super) fn new(dat: ErrorData<Box<Custom>>) -> Self {
+ Self(dat)
+ }
+ pub(super) fn new_custom(b: Box<Custom>) -> Self {
+ Self(Inner::Custom(b))
+ }
+ #[inline]
+ pub(super) fn new_os(code: i32) -> Self {
+ Self(Inner::Os(code))
+ }
+ #[inline]
+ pub(super) fn new_simple(kind: ErrorKind) -> Self {
+ Self(Inner::Simple(kind))
+ }
+ #[inline]
+ pub(super) const fn new_simple_message(m: &'static SimpleMessage) -> Self {
+ Self(Inner::SimpleMessage(m))
+ }
+ #[inline]
+ pub(super) fn into_data(self) -> ErrorData<Box<Custom>> {
+ self.0
+ }
+ #[inline]
+ pub(super) fn data(&self) -> ErrorData<&Custom> {
+ match &self.0 {
+ Inner::Os(c) => ErrorData::Os(*c),
+ Inner::Simple(k) => ErrorData::Simple(*k),
+ Inner::SimpleMessage(m) => ErrorData::SimpleMessage(*m),
+ Inner::Custom(m) => ErrorData::Custom(&*m),
+ }
+ }
+ #[inline]
+ pub(super) fn data_mut(&mut self) -> ErrorData<&mut Custom> {
+ match &mut self.0 {
+ Inner::Os(c) => ErrorData::Os(*c),
+ Inner::Simple(k) => ErrorData::Simple(*k),
+ Inner::SimpleMessage(m) => ErrorData::SimpleMessage(*m),
+ Inner::Custom(m) => ErrorData::Custom(&mut *m),
+ }
+ }
+}
diff --git a/library/std/src/io/error/tests.rs b/library/std/src/io/error/tests.rs
new file mode 100644
index 000000000..c897a5e87
--- /dev/null
+++ b/library/std/src/io/error/tests.rs
@@ -0,0 +1,194 @@
+use super::{const_io_error, Custom, Error, ErrorData, ErrorKind, Repr, SimpleMessage};
+use crate::assert_matches::assert_matches;
+use crate::error;
+use crate::fmt;
+use crate::mem::size_of;
+use crate::sys::decode_error_kind;
+use crate::sys::os::error_string;
+
+#[test]
+fn test_size() {
+ assert!(size_of::<Error>() <= size_of::<[usize; 2]>());
+}
+
+#[test]
+fn test_debug_error() {
+ let code = 6;
+ let msg = error_string(code);
+ let kind = decode_error_kind(code);
+ let err = Error {
+ repr: Repr::new_custom(Box::new(Custom {
+ kind: ErrorKind::InvalidInput,
+ error: Box::new(Error { repr: super::Repr::new_os(code) }),
+ })),
+ };
+ let expected = format!(
+ "Custom {{ \
+ kind: InvalidInput, \
+ error: Os {{ \
+ code: {:?}, \
+ kind: {:?}, \
+ message: {:?} \
+ }} \
+ }}",
+ code, kind, msg
+ );
+ assert_eq!(format!("{err:?}"), expected);
+}
+
+#[test]
+fn test_downcasting() {
+ #[derive(Debug)]
+ struct TestError;
+
+ impl fmt::Display for TestError {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.write_str("asdf")
+ }
+ }
+
+ impl error::Error for TestError {}
+
+ // we have to call all of these UFCS style right now since method
+ // resolution won't implicitly drop the Send+Sync bounds
+ let mut err = Error::new(ErrorKind::Other, TestError);
+ assert!(err.get_ref().unwrap().is::<TestError>());
+ assert_eq!("asdf", err.get_ref().unwrap().to_string());
+ assert!(err.get_mut().unwrap().is::<TestError>());
+ let extracted = err.into_inner().unwrap();
+ extracted.downcast::<TestError>().unwrap();
+}
+
+#[test]
+fn test_const() {
+ const E: Error = const_io_error!(ErrorKind::NotFound, "hello");
+
+ assert_eq!(E.kind(), ErrorKind::NotFound);
+ assert_eq!(E.to_string(), "hello");
+ assert!(format!("{E:?}").contains("\"hello\""));
+ assert!(format!("{E:?}").contains("NotFound"));
+}
+
+#[test]
+fn test_os_packing() {
+ for code in -20i32..20i32 {
+ let e = Error::from_raw_os_error(code);
+ assert_eq!(e.raw_os_error(), Some(code));
+ assert_matches!(
+ e.repr.data(),
+ ErrorData::Os(c) if c == code,
+ );
+ }
+}
+
+#[test]
+fn test_errorkind_packing() {
+ assert_eq!(Error::from(ErrorKind::NotFound).kind(), ErrorKind::NotFound);
+ assert_eq!(Error::from(ErrorKind::PermissionDenied).kind(), ErrorKind::PermissionDenied);
+ assert_eq!(Error::from(ErrorKind::Uncategorized).kind(), ErrorKind::Uncategorized);
+ // Check that the innards look like like what we want.
+ assert_matches!(
+ Error::from(ErrorKind::OutOfMemory).repr.data(),
+ ErrorData::Simple(ErrorKind::OutOfMemory),
+ );
+}
+
+#[test]
+fn test_simple_message_packing() {
+ use super::{ErrorKind::*, SimpleMessage};
+ macro_rules! check_simple_msg {
+ ($err:expr, $kind:ident, $msg:literal) => {{
+ let e = &$err;
+ // Check that the public api is right.
+ assert_eq!(e.kind(), $kind);
+ assert!(format!("{e:?}").contains($msg));
+ // and we got what we expected
+ assert_matches!(
+ e.repr.data(),
+ ErrorData::SimpleMessage(SimpleMessage { kind: $kind, message: $msg })
+ );
+ }};
+ }
+
+ let not_static = const_io_error!(Uncategorized, "not a constant!");
+ check_simple_msg!(not_static, Uncategorized, "not a constant!");
+
+ const CONST: Error = const_io_error!(NotFound, "definitely a constant!");
+ check_simple_msg!(CONST, NotFound, "definitely a constant!");
+
+ static STATIC: Error = const_io_error!(BrokenPipe, "a constant, sort of!");
+ check_simple_msg!(STATIC, BrokenPipe, "a constant, sort of!");
+}
+
+#[derive(Debug, PartialEq)]
+struct Bojji(bool);
+impl error::Error for Bojji {}
+impl fmt::Display for Bojji {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "ah! {:?}", self)
+ }
+}
+
+#[test]
+fn test_custom_error_packing() {
+ use super::Custom;
+ let test = Error::new(ErrorKind::Uncategorized, Bojji(true));
+ assert_matches!(
+ test.repr.data(),
+ ErrorData::Custom(Custom {
+ kind: ErrorKind::Uncategorized,
+ error,
+ }) if error.downcast_ref::<Bojji>().as_deref() == Some(&Bojji(true)),
+ );
+}
+
+#[derive(Debug)]
+struct E;
+
+impl fmt::Display for E {
+ fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ Ok(())
+ }
+}
+
+impl error::Error for E {}
+
+#[test]
+fn test_std_io_error_downcast() {
+ // Case 1: custom error, downcast succeeds
+ let io_error = Error::new(ErrorKind::Other, Bojji(true));
+ let e: Box<Bojji> = io_error.downcast().unwrap();
+ assert!(e.0);
+
+ // Case 2: custom error, downcast fails
+ let io_error = Error::new(ErrorKind::Other, Bojji(true));
+ let io_error = io_error.downcast::<E>().unwrap_err();
+
+ // ensures that the custom error is intact
+ assert_eq!(ErrorKind::Other, io_error.kind());
+ let e: Box<Bojji> = io_error.downcast().unwrap();
+ assert!(e.0);
+
+ // Case 3: os error
+ let errno = 20;
+ let io_error = Error::from_raw_os_error(errno);
+ let io_error = io_error.downcast::<E>().unwrap_err();
+
+ assert_eq!(errno, io_error.raw_os_error().unwrap());
+
+ // Case 4: simple
+ let kind = ErrorKind::OutOfMemory;
+ let io_error: Error = kind.into();
+ let io_error = io_error.downcast::<E>().unwrap_err();
+
+ assert_eq!(kind, io_error.kind());
+
+ // Case 5: simple message
+ const SIMPLE_MESSAGE: SimpleMessage =
+ SimpleMessage { kind: ErrorKind::Other, message: "simple message error test" };
+ let io_error = Error::from_static_message(&SIMPLE_MESSAGE);
+ let io_error = io_error.downcast::<E>().unwrap_err();
+
+ assert_eq!(SIMPLE_MESSAGE.kind, io_error.kind());
+ assert_eq!(SIMPLE_MESSAGE.message, &*format!("{io_error}"));
+}
diff --git a/library/std/src/io/impls.rs b/library/std/src/io/impls.rs
new file mode 100644
index 000000000..950725473
--- /dev/null
+++ b/library/std/src/io/impls.rs
@@ -0,0 +1,458 @@
+#[cfg(test)]
+mod tests;
+
+use crate::alloc::Allocator;
+use crate::cmp;
+use crate::collections::VecDeque;
+use crate::fmt;
+use crate::io::{
+ self, BufRead, ErrorKind, IoSlice, IoSliceMut, Read, ReadBuf, Seek, SeekFrom, Write,
+};
+use crate::mem;
+
+// =============================================================================
+// Forwarding implementations
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<R: Read + ?Sized> Read for &mut R {
+ #[inline]
+ fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+ (**self).read(buf)
+ }
+
+ #[inline]
+ fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> {
+ (**self).read_buf(buf)
+ }
+
+ #[inline]
+ fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
+ (**self).read_vectored(bufs)
+ }
+
+ #[inline]
+ fn is_read_vectored(&self) -> bool {
+ (**self).is_read_vectored()
+ }
+
+ #[inline]
+ fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
+ (**self).read_to_end(buf)
+ }
+
+ #[inline]
+ fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
+ (**self).read_to_string(buf)
+ }
+
+ #[inline]
+ fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
+ (**self).read_exact(buf)
+ }
+}
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<W: Write + ?Sized> Write for &mut W {
+ #[inline]
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ (**self).write(buf)
+ }
+
+ #[inline]
+ fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+ (**self).write_vectored(bufs)
+ }
+
+ #[inline]
+ fn is_write_vectored(&self) -> bool {
+ (**self).is_write_vectored()
+ }
+
+ #[inline]
+ fn flush(&mut self) -> io::Result<()> {
+ (**self).flush()
+ }
+
+ #[inline]
+ fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
+ (**self).write_all(buf)
+ }
+
+ #[inline]
+ fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
+ (**self).write_fmt(fmt)
+ }
+}
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<S: Seek + ?Sized> Seek for &mut S {
+ #[inline]
+ fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
+ (**self).seek(pos)
+ }
+
+ #[inline]
+ fn stream_position(&mut self) -> io::Result<u64> {
+ (**self).stream_position()
+ }
+}
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<B: BufRead + ?Sized> BufRead for &mut B {
+ #[inline]
+ fn fill_buf(&mut self) -> io::Result<&[u8]> {
+ (**self).fill_buf()
+ }
+
+ #[inline]
+ fn consume(&mut self, amt: usize) {
+ (**self).consume(amt)
+ }
+
+ #[inline]
+ fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> {
+ (**self).read_until(byte, buf)
+ }
+
+ #[inline]
+ fn read_line(&mut self, buf: &mut String) -> io::Result<usize> {
+ (**self).read_line(buf)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<R: Read + ?Sized> Read for Box<R> {
+ #[inline]
+ fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+ (**self).read(buf)
+ }
+
+ #[inline]
+ fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> {
+ (**self).read_buf(buf)
+ }
+
+ #[inline]
+ fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
+ (**self).read_vectored(bufs)
+ }
+
+ #[inline]
+ fn is_read_vectored(&self) -> bool {
+ (**self).is_read_vectored()
+ }
+
+ #[inline]
+ fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
+ (**self).read_to_end(buf)
+ }
+
+ #[inline]
+ fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
+ (**self).read_to_string(buf)
+ }
+
+ #[inline]
+ fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
+ (**self).read_exact(buf)
+ }
+}
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<W: Write + ?Sized> Write for Box<W> {
+ #[inline]
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ (**self).write(buf)
+ }
+
+ #[inline]
+ fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+ (**self).write_vectored(bufs)
+ }
+
+ #[inline]
+ fn is_write_vectored(&self) -> bool {
+ (**self).is_write_vectored()
+ }
+
+ #[inline]
+ fn flush(&mut self) -> io::Result<()> {
+ (**self).flush()
+ }
+
+ #[inline]
+ fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
+ (**self).write_all(buf)
+ }
+
+ #[inline]
+ fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
+ (**self).write_fmt(fmt)
+ }
+}
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<S: Seek + ?Sized> Seek for Box<S> {
+ #[inline]
+ fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
+ (**self).seek(pos)
+ }
+
+ #[inline]
+ fn stream_position(&mut self) -> io::Result<u64> {
+ (**self).stream_position()
+ }
+}
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<B: BufRead + ?Sized> BufRead for Box<B> {
+ #[inline]
+ fn fill_buf(&mut self) -> io::Result<&[u8]> {
+ (**self).fill_buf()
+ }
+
+ #[inline]
+ fn consume(&mut self, amt: usize) {
+ (**self).consume(amt)
+ }
+
+ #[inline]
+ fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> {
+ (**self).read_until(byte, buf)
+ }
+
+ #[inline]
+ fn read_line(&mut self, buf: &mut String) -> io::Result<usize> {
+ (**self).read_line(buf)
+ }
+}
+
+// =============================================================================
+// In-memory buffer implementations
+
+/// Read is implemented for `&[u8]` by copying from the slice.
+///
+/// Note that reading updates the slice to point to the yet unread part.
+/// The slice will be empty when EOF is reached.
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Read for &[u8] {
+ #[inline]
+ fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+ let amt = cmp::min(buf.len(), self.len());
+ let (a, b) = self.split_at(amt);
+
+ // First check if the amount of bytes we want to read is small:
+ // `copy_from_slice` will generally expand to a call to `memcpy`, and
+ // for a single byte the overhead is significant.
+ if amt == 1 {
+ buf[0] = a[0];
+ } else {
+ buf[..amt].copy_from_slice(a);
+ }
+
+ *self = b;
+ Ok(amt)
+ }
+
+ #[inline]
+ fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> {
+ let amt = cmp::min(buf.remaining(), self.len());
+ let (a, b) = self.split_at(amt);
+
+ buf.append(a);
+
+ *self = b;
+ Ok(())
+ }
+
+ #[inline]
+ fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
+ let mut nread = 0;
+ for buf in bufs {
+ nread += self.read(buf)?;
+ if self.is_empty() {
+ break;
+ }
+ }
+
+ Ok(nread)
+ }
+
+ #[inline]
+ fn is_read_vectored(&self) -> bool {
+ true
+ }
+
+ #[inline]
+ fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
+ if buf.len() > self.len() {
+ return Err(io::const_io_error!(
+ ErrorKind::UnexpectedEof,
+ "failed to fill whole buffer"
+ ));
+ }
+ let (a, b) = self.split_at(buf.len());
+
+ // First check if the amount of bytes we want to read is small:
+ // `copy_from_slice` will generally expand to a call to `memcpy`, and
+ // for a single byte the overhead is significant.
+ if buf.len() == 1 {
+ buf[0] = a[0];
+ } else {
+ buf.copy_from_slice(a);
+ }
+
+ *self = b;
+ Ok(())
+ }
+
+ #[inline]
+ fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
+ buf.extend_from_slice(*self);
+ let len = self.len();
+ *self = &self[len..];
+ Ok(len)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl BufRead for &[u8] {
+ #[inline]
+ fn fill_buf(&mut self) -> io::Result<&[u8]> {
+ Ok(*self)
+ }
+
+ #[inline]
+ fn consume(&mut self, amt: usize) {
+ *self = &self[amt..];
+ }
+}
+
+/// Write is implemented for `&mut [u8]` by copying into the slice, overwriting
+/// its data.
+///
+/// Note that writing updates the slice to point to the yet unwritten part.
+/// The slice will be empty when it has been completely overwritten.
+///
+/// If the number of bytes to be written exceeds the size of the slice, write operations will
+/// return short writes: ultimately, `Ok(0)`; in this situation, `write_all` returns an error of
+/// kind `ErrorKind::WriteZero`.
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Write for &mut [u8] {
+ #[inline]
+ fn write(&mut self, data: &[u8]) -> io::Result<usize> {
+ let amt = cmp::min(data.len(), self.len());
+ let (a, b) = mem::replace(self, &mut []).split_at_mut(amt);
+ a.copy_from_slice(&data[..amt]);
+ *self = b;
+ Ok(amt)
+ }
+
+ #[inline]
+ fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+ let mut nwritten = 0;
+ for buf in bufs {
+ nwritten += self.write(buf)?;
+ if self.is_empty() {
+ break;
+ }
+ }
+
+ Ok(nwritten)
+ }
+
+ #[inline]
+ fn is_write_vectored(&self) -> bool {
+ true
+ }
+
+ #[inline]
+ fn write_all(&mut self, data: &[u8]) -> io::Result<()> {
+ if self.write(data)? == data.len() {
+ Ok(())
+ } else {
+ Err(io::const_io_error!(ErrorKind::WriteZero, "failed to write whole buffer"))
+ }
+ }
+
+ #[inline]
+ fn flush(&mut self) -> io::Result<()> {
+ Ok(())
+ }
+}
+
+/// Write is implemented for `Vec<u8>` by appending to the vector.
+/// The vector will grow as needed.
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<A: Allocator> Write for Vec<u8, A> {
+ #[inline]
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ self.extend_from_slice(buf);
+ Ok(buf.len())
+ }
+
+ #[inline]
+ fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+ let len = bufs.iter().map(|b| b.len()).sum();
+ self.reserve(len);
+ for buf in bufs {
+ self.extend_from_slice(buf);
+ }
+ Ok(len)
+ }
+
+ #[inline]
+ fn is_write_vectored(&self) -> bool {
+ true
+ }
+
+ #[inline]
+ fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
+ self.extend_from_slice(buf);
+ Ok(())
+ }
+
+ #[inline]
+ fn flush(&mut self) -> io::Result<()> {
+ Ok(())
+ }
+}
+
+/// Read is implemented for `VecDeque<u8>` by consuming bytes from the front of the `VecDeque`.
+#[stable(feature = "vecdeque_read_write", since = "1.63.0")]
+impl<A: Allocator> Read for VecDeque<u8, A> {
+ /// Fill `buf` with the contents of the "front" slice as returned by
+ /// [`as_slices`][`VecDeque::as_slices`]. If the contained byte slices of the `VecDeque` are
+ /// discontiguous, multiple calls to `read` will be needed to read the entire content.
+ #[inline]
+ fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+ let (ref mut front, _) = self.as_slices();
+ let n = Read::read(front, buf)?;
+ self.drain(..n);
+ Ok(n)
+ }
+
+ #[inline]
+ fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> {
+ let (ref mut front, _) = self.as_slices();
+ let n = cmp::min(buf.remaining(), front.len());
+ Read::read_buf(front, buf)?;
+ self.drain(..n);
+ Ok(())
+ }
+}
+
+/// Write is implemented for `VecDeque<u8>` by appending to the `VecDeque`, growing it as needed.
+#[stable(feature = "vecdeque_read_write", since = "1.63.0")]
+impl<A: Allocator> Write for VecDeque<u8, A> {
+ #[inline]
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ self.extend(buf);
+ Ok(buf.len())
+ }
+
+ #[inline]
+ fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
+ self.extend(buf);
+ Ok(())
+ }
+
+ #[inline]
+ fn flush(&mut self) -> io::Result<()> {
+ Ok(())
+ }
+}
diff --git a/library/std/src/io/impls/tests.rs b/library/std/src/io/impls/tests.rs
new file mode 100644
index 000000000..d1cd84a67
--- /dev/null
+++ b/library/std/src/io/impls/tests.rs
@@ -0,0 +1,57 @@
+use crate::io::prelude::*;
+
+#[bench]
+fn bench_read_slice(b: &mut test::Bencher) {
+ let buf = [5; 1024];
+ let mut dst = [0; 128];
+
+ b.iter(|| {
+ let mut rd = &buf[..];
+ for _ in 0..8 {
+ let _ = rd.read(&mut dst);
+ test::black_box(&dst);
+ }
+ })
+}
+
+#[bench]
+fn bench_write_slice(b: &mut test::Bencher) {
+ let mut buf = [0; 1024];
+ let src = [5; 128];
+
+ b.iter(|| {
+ let mut wr = &mut buf[..];
+ for _ in 0..8 {
+ let _ = wr.write_all(&src);
+ test::black_box(&wr);
+ }
+ })
+}
+
+#[bench]
+fn bench_read_vec(b: &mut test::Bencher) {
+ let buf = vec![5; 1024];
+ let mut dst = [0; 128];
+
+ b.iter(|| {
+ let mut rd = &buf[..];
+ for _ in 0..8 {
+ let _ = rd.read(&mut dst);
+ test::black_box(&dst);
+ }
+ })
+}
+
+#[bench]
+fn bench_write_vec(b: &mut test::Bencher) {
+ let mut buf = Vec::with_capacity(1024);
+ let src = [5; 128];
+
+ b.iter(|| {
+ let mut wr = &mut buf[..];
+ for _ in 0..8 {
+ let _ = wr.write_all(&src);
+ test::black_box(&wr);
+ }
+ })
+}
diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs
new file mode 100644
index 000000000..96addbd1a
--- /dev/null
+++ b/library/std/src/io/mod.rs
@@ -0,0 +1,2827 @@
+//! Traits, helpers, and type definitions for core I/O functionality.
+//!
+//! The `std::io` module contains a number of common things you'll need
+//! when doing input and output. The most core part of this module is
+//! the [`Read`] and [`Write`] traits, which provide the
+//! most general interface for reading and writing input and output.
+//!
+//! # Read and Write
+//!
+//! Because they are traits, [`Read`] and [`Write`] are implemented by a number
+//! of other types, and you can implement them for your types too. As such,
+//! you'll see a few different types of I/O throughout the documentation in
+//! this module: [`File`]s, [`TcpStream`]s, and sometimes even [`Vec<T>`]s. For
+//! example, [`Read`] adds a [`read`][`Read::read`] method, which we can use on
+//! [`File`]s:
+//!
+//! ```no_run
+//! use std::io;
+//! use std::io::prelude::*;
+//! use std::fs::File;
+//!
+//! fn main() -> io::Result<()> {
+//! let mut f = File::open("foo.txt")?;
+//! let mut buffer = [0; 10];
+//!
+//! // read up to 10 bytes
+//! let n = f.read(&mut buffer)?;
+//!
+//! println!("The bytes: {:?}", &buffer[..n]);
+//! Ok(())
+//! }
+//! ```
+//!
+//! [`Read`] and [`Write`] are so important, implementors of the two traits have a
+//! nickname: readers and writers. So you'll sometimes see 'a reader' instead
+//! of 'a type that implements the [`Read`] trait'. Much easier!
+//!
+//! ## Seek and BufRead
+//!
+//! Beyond that, there are two important traits that are provided: [`Seek`]
+//! and [`BufRead`]. Both of these build on top of a reader to control
+//! how the reading happens. [`Seek`] lets you control where the next byte is
+//! coming from:
+//!
+//! ```no_run
+//! use std::io;
+//! use std::io::prelude::*;
+//! use std::io::SeekFrom;
+//! use std::fs::File;
+//!
+//! fn main() -> io::Result<()> {
+//! let mut f = File::open("foo.txt")?;
+//! let mut buffer = [0; 10];
+//!
+//! // skip to the last 10 bytes of the file
+//! f.seek(SeekFrom::End(-10))?;
+//!
+//! // read up to 10 bytes
+//! let n = f.read(&mut buffer)?;
+//!
+//! println!("The bytes: {:?}", &buffer[..n]);
+//! Ok(())
+//! }
+//! ```
+//!
+//! [`BufRead`] uses an internal buffer to provide a number of other ways to read, but
+//! to show it off, we'll need to talk about buffers in general. Keep reading!
+//!
+//! ## BufReader and BufWriter
+//!
+//! Byte-based interfaces are unwieldy and can be inefficient, as we'd need to be
+//! making near-constant calls to the operating system. To help with this,
+//! `std::io` comes with two structs, [`BufReader`] and [`BufWriter`], which wrap
+//! readers and writers. The wrapper uses a buffer, reducing the number of
+//! calls and providing nicer methods for accessing exactly what you want.
+//!
+//! For example, [`BufReader`] works with the [`BufRead`] trait to add extra
+//! methods to any reader:
+//!
+//! ```no_run
+//! use std::io;
+//! use std::io::prelude::*;
+//! use std::io::BufReader;
+//! use std::fs::File;
+//!
+//! fn main() -> io::Result<()> {
+//! let f = File::open("foo.txt")?;
+//! let mut reader = BufReader::new(f);
+//! let mut buffer = String::new();
+//!
+//! // read a line into buffer
+//! reader.read_line(&mut buffer)?;
+//!
+//! println!("{buffer}");
+//! Ok(())
+//! }
+//! ```
+//!
+//! [`BufWriter`] doesn't add any new ways of writing; it just buffers every call
+//! to [`write`][`Write::write`]:
+//!
+//! ```no_run
+//! use std::io;
+//! use std::io::prelude::*;
+//! use std::io::BufWriter;
+//! use std::fs::File;
+//!
+//! fn main() -> io::Result<()> {
+//! let f = File::create("foo.txt")?;
+//! {
+//! let mut writer = BufWriter::new(f);
+//!
+//! // write a byte to the buffer
+//! writer.write(&[42])?;
+//!
+//! } // the buffer is flushed once writer goes out of scope
+//!
+//! Ok(())
+//! }
+//! ```
+//!
+//! ## Standard input and output
+//!
+//! A very common source of input is standard input:
+//!
+//! ```no_run
+//! use std::io;
+//!
+//! fn main() -> io::Result<()> {
+//! let mut input = String::new();
+//!
+//! io::stdin().read_line(&mut input)?;
+//!
+//! println!("You typed: {}", input.trim());
+//! Ok(())
+//! }
+//! ```
+//!
+//! Note that you cannot use the [`?` operator] in functions that do not return
+//! a [`Result<T, E>`][`Result`]. Instead, you can call [`.unwrap()`]
+//! or `match` on the return value to catch any possible errors:
+//!
+//! ```no_run
+//! use std::io;
+//!
+//! let mut input = String::new();
+//!
+//! io::stdin().read_line(&mut input).unwrap();
+//! ```
+//!
+//! And a very common source of output is standard output:
+//!
+//! ```no_run
+//! use std::io;
+//! use std::io::prelude::*;
+//!
+//! fn main() -> io::Result<()> {
+//! io::stdout().write(&[42])?;
+//! Ok(())
+//! }
+//! ```
+//!
+//! Of course, using [`io::stdout`] directly is less common than something like
+//! [`println!`].
+//!
+//! ## Iterator types
+//!
+//! A large number of the structures provided by `std::io` are for various
+//! ways of iterating over I/O. For example, [`Lines`] is used to split over
+//! lines:
+//!
+//! ```no_run
+//! use std::io;
+//! use std::io::prelude::*;
+//! use std::io::BufReader;
+//! use std::fs::File;
+//!
+//! fn main() -> io::Result<()> {
+//! let f = File::open("foo.txt")?;
+//! let reader = BufReader::new(f);
+//!
+//! for line in reader.lines() {
+//! println!("{}", line?);
+//! }
+//! Ok(())
+//! }
+//! ```
+//!
+//! ## Functions
+//!
+//! There are a number of [functions][functions-list] that offer access to various
+//! features. For example, we can use three of these functions to copy everything
+//! from standard input to standard output:
+//!
+//! ```no_run
+//! use std::io;
+//!
+//! fn main() -> io::Result<()> {
+//! io::copy(&mut io::stdin(), &mut io::stdout())?;
+//! Ok(())
+//! }
+//! ```
+//!
+//! [functions-list]: #functions-1
+//!
+//! ## io::Result
+//!
+//! Last, but certainly not least, is [`io::Result`]. This type is used
+//! as the return type of many `std::io` functions that can cause an error, and
+//! can be returned from your own functions as well. Many of the examples in this
+//! module use the [`?` operator]:
+//!
+//! ```
+//! use std::io;
+//!
+//! fn read_input() -> io::Result<()> {
+//! let mut input = String::new();
+//!
+//! io::stdin().read_line(&mut input)?;
+//!
+//! println!("You typed: {}", input.trim());
+//!
+//! Ok(())
+//! }
+//! ```
+//!
+//! The return type of `read_input()`, [`io::Result<()>`][`io::Result`], is a very
+//! common type for functions which don't have a 'real' return value, but do want to
+//! return errors if they happen. In this case, the only purpose of this function is
+//! to read the line and print it, so we use `()`.
+//!
+//! ## Platform-specific behavior
+//!
+//! Many I/O functions throughout the standard library are documented to indicate
+//! what various library or syscalls they are delegated to. This is done to help
+//! applications both understand what's happening under the hood as well as investigate
+//! any possibly unclear semantics. Note, however, that this is informative, not a binding
+//! contract. The implementation of many of these functions are subject to change over
+//! time and may call fewer or more syscalls/library functions.
+//!
+//! [`File`]: crate::fs::File
+//! [`TcpStream`]: crate::net::TcpStream
+//! [`io::stdout`]: stdout
+//! [`io::Result`]: self::Result
+//! [`?` operator]: ../../book/appendix-02-operators.html
+//! [`Result`]: crate::result::Result
+//! [`.unwrap()`]: crate::result::Result::unwrap
+
+#![stable(feature = "rust1", since = "1.0.0")]
+
+#[cfg(test)]
+mod tests;
+
+use crate::cmp;
+use crate::fmt;
+use crate::mem::replace;
+use crate::ops::{Deref, DerefMut};
+use crate::slice;
+use crate::str;
+use crate::sys;
+use crate::sys_common::memchr;
+
+#[stable(feature = "bufwriter_into_parts", since = "1.56.0")]
+pub use self::buffered::WriterPanicked;
+#[unstable(feature = "internal_output_capture", issue = "none")]
+#[doc(no_inline, hidden)]
+pub use self::stdio::set_output_capture;
+#[unstable(feature = "print_internals", issue = "none")]
+pub use self::stdio::{_eprint, _print};
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use self::{
+ buffered::{BufReader, BufWriter, IntoInnerError, LineWriter},
+ copy::copy,
+ cursor::Cursor,
+ error::{Error, ErrorKind, Result},
+ stdio::{stderr, stdin, stdout, Stderr, StderrLock, Stdin, StdinLock, Stdout, StdoutLock},
+ util::{empty, repeat, sink, Empty, Repeat, Sink},
+};
+
+#[unstable(feature = "read_buf", issue = "78485")]
+pub use self::readbuf::ReadBuf;
+pub(crate) use error::const_io_error;
+
+mod buffered;
+pub(crate) mod copy;
+mod cursor;
+mod error;
+mod impls;
+pub mod prelude;
+mod readbuf;
+mod stdio;
+mod util;
+
+const DEFAULT_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE;
+
+pub(crate) use stdio::cleanup;
+
+struct Guard<'a> {
+ buf: &'a mut Vec<u8>,
+ len: usize,
+}
+
+impl Drop for Guard<'_> {
+ fn drop(&mut self) {
+ unsafe {
+ self.buf.set_len(self.len);
+ }
+ }
+}
+
+// Several `read_to_string` and `read_line` methods in the standard library will
+// append data into a `String` buffer, but we need to be pretty careful when
+// doing this. The implementation will just call `.as_mut_vec()` and then
+// delegate to a byte-oriented reading method, but we must ensure that when
+// returning we never leave `buf` in a state such that it contains invalid UTF-8
+// in its bounds.
+//
+// To this end, we use an RAII guard (to protect against panics) which updates
+// the length of the string when it is dropped. This guard initially truncates
+// the string to the prior length and only after we've validated that the
+// new contents are valid UTF-8 do we allow it to set a longer length.
+//
+// The unsafety in this function is twofold:
+//
+// 1. We're looking at the raw bytes of `buf`, so we take on the burden of UTF-8
+// checks.
+// 2. We're passing a raw buffer to the function `f`, and it is expected that
+// the function only *appends* bytes to the buffer. We'll get undefined
+// behavior if existing bytes are overwritten to have non-UTF-8 data.
+pub(crate) unsafe fn append_to_string<F>(buf: &mut String, f: F) -> Result<usize>
+where
+ F: FnOnce(&mut Vec<u8>) -> Result<usize>,
+{
+ let mut g = Guard { len: buf.len(), buf: buf.as_mut_vec() };
+ let ret = f(g.buf);
+ if str::from_utf8(&g.buf[g.len..]).is_err() {
+ ret.and_then(|_| {
+ Err(error::const_io_error!(
+ ErrorKind::InvalidData,
+ "stream did not contain valid UTF-8"
+ ))
+ })
+ } else {
+ g.len = g.buf.len();
+ ret
+ }
+}
+
+// This uses an adaptive system to extend the vector when it fills. We want to
+// avoid paying to allocate and zero a huge chunk of memory if the reader only
+// has 4 bytes while still making large reads if the reader does have a ton
+// of data to return. Simply tacking on an extra DEFAULT_BUF_SIZE space every
+// time is 4,500 times (!) slower than a default reservation size of 32 if the
+// reader has a very small amount of data to return.
+pub(crate) fn default_read_to_end<R: Read + ?Sized>(r: &mut R, buf: &mut Vec<u8>) -> Result<usize> {
+ let start_len = buf.len();
+ let start_cap = buf.capacity();
+
+ let mut initialized = 0; // Extra initialized bytes from previous loop iteration
+ loop {
+ if buf.len() == buf.capacity() {
+ buf.reserve(32); // buf is full, need more space
+ }
+
+ let mut read_buf = ReadBuf::uninit(buf.spare_capacity_mut());
+
+ // SAFETY: These bytes were initialized but not filled in the previous loop
+ unsafe {
+ read_buf.assume_init(initialized);
+ }
+
+ match r.read_buf(&mut read_buf) {
+ Ok(()) => {}
+ Err(e) if e.kind() == ErrorKind::Interrupted => continue,
+ Err(e) => return Err(e),
+ }
+
+ if read_buf.filled_len() == 0 {
+ return Ok(buf.len() - start_len);
+ }
+
+ // store how much was initialized but not filled
+ initialized = read_buf.initialized_len() - read_buf.filled_len();
+ let new_len = read_buf.filled_len() + buf.len();
+
+ // SAFETY: ReadBuf's invariants mean this much memory is init
+ unsafe {
+ buf.set_len(new_len);
+ }
+
+ if buf.len() == buf.capacity() && buf.capacity() == start_cap {
+ // The buffer might be an exact fit. Let's read into a probe buffer
+ // and see if it returns `Ok(0)`. If so, we've avoided an
+ // unnecessary doubling of the capacity. But if not, append the
+ // probe buffer to the primary buffer and let its capacity grow.
+ let mut probe = [0u8; 32];
+
+ loop {
+ match r.read(&mut probe) {
+ Ok(0) => return Ok(buf.len() - start_len),
+ Ok(n) => {
+ buf.extend_from_slice(&probe[..n]);
+ break;
+ }
+ Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
+ Err(e) => return Err(e),
+ }
+ }
+ }
+ }
+}
+
+pub(crate) fn default_read_to_string<R: Read + ?Sized>(
+ r: &mut R,
+ buf: &mut String,
+) -> Result<usize> {
+ // Note that we do *not* call `r.read_to_end()` here. We are passing
+ // `&mut Vec<u8>` (the raw contents of `buf`) into the `read_to_end`
+ // method to fill it up. An arbitrary implementation could overwrite the
+ // entire contents of the vector, not just append to it (which is what
+ // we are expecting).
+ //
+ // To prevent extraneously checking the UTF-8-ness of the entire buffer
+ // we pass it to our hardcoded `default_read_to_end` implementation which
+ // we know is guaranteed to only read data into the end of the buffer.
+ unsafe { append_to_string(buf, |b| default_read_to_end(r, b)) }
+}
+
+pub(crate) fn default_read_vectored<F>(read: F, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
+where
+ F: FnOnce(&mut [u8]) -> Result<usize>,
+{
+ let buf = bufs.iter_mut().find(|b| !b.is_empty()).map_or(&mut [][..], |b| &mut **b);
+ read(buf)
+}
+
+pub(crate) fn default_write_vectored<F>(write: F, bufs: &[IoSlice<'_>]) -> Result<usize>
+where
+ F: FnOnce(&[u8]) -> Result<usize>,
+{
+ let buf = bufs.iter().find(|b| !b.is_empty()).map_or(&[][..], |b| &**b);
+ write(buf)
+}
+
+pub(crate) fn default_read_exact<R: Read + ?Sized>(this: &mut R, mut buf: &mut [u8]) -> Result<()> {
+ while !buf.is_empty() {
+ match this.read(buf) {
+ Ok(0) => break,
+ Ok(n) => {
+ let tmp = buf;
+ buf = &mut tmp[n..];
+ }
+ Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
+ Err(e) => return Err(e),
+ }
+ }
+ if !buf.is_empty() {
+ Err(error::const_io_error!(ErrorKind::UnexpectedEof, "failed to fill whole buffer"))
+ } else {
+ Ok(())
+ }
+}
+
+pub(crate) fn default_read_buf<F>(read: F, buf: &mut ReadBuf<'_>) -> Result<()>
+where
+ F: FnOnce(&mut [u8]) -> Result<usize>,
+{
+ let n = read(buf.initialize_unfilled())?;
+ buf.add_filled(n);
+ Ok(())
+}
+
+/// The `Read` trait allows for reading bytes from a source.
+///
+/// Implementors of the `Read` trait are called 'readers'.
+///
+/// Readers are defined by one required method, [`read()`]. Each call to [`read()`]
+/// will attempt to pull bytes from this source into a provided buffer. A
+/// number of other methods are implemented in terms of [`read()`], giving
+/// implementors a number of ways to read bytes while only needing to implement
+/// a single method.
+///
+/// Readers are intended to be composable with one another. Many implementors
+/// throughout [`std::io`] take and provide types which implement the `Read`
+/// trait.
+///
+/// Please note that each call to [`read()`] may involve a system call, and
+/// therefore, using something that implements [`BufRead`], such as
+/// [`BufReader`], will be more efficient.
+///
+/// # Examples
+///
+/// [`File`]s implement `Read`:
+///
+/// ```no_run
+/// use std::io;
+/// use std::io::prelude::*;
+/// use std::fs::File;
+///
+/// fn main() -> io::Result<()> {
+/// let mut f = File::open("foo.txt")?;
+/// let mut buffer = [0; 10];
+///
+/// // read up to 10 bytes
+/// f.read(&mut buffer)?;
+///
+/// let mut buffer = Vec::new();
+/// // read the whole file
+/// f.read_to_end(&mut buffer)?;
+///
+/// // read into a String, so that you don't need to do the conversion.
+/// let mut buffer = String::new();
+/// f.read_to_string(&mut buffer)?;
+///
+/// // and more! See the other methods for more details.
+/// Ok(())
+/// }
+/// ```
+///
+/// Read from [`&str`] because [`&[u8]`][prim@slice] implements `Read`:
+///
+/// ```no_run
+/// # use std::io;
+/// use std::io::prelude::*;
+///
+/// fn main() -> io::Result<()> {
+/// let mut b = "This string will be read".as_bytes();
+/// let mut buffer = [0; 10];
+///
+/// // read up to 10 bytes
+/// b.read(&mut buffer)?;
+///
+/// // etc... it works exactly as a File does!
+/// Ok(())
+/// }
+/// ```
+///
+/// [`read()`]: Read::read
+/// [`&str`]: prim@str
+/// [`std::io`]: self
+/// [`File`]: crate::fs::File
+#[stable(feature = "rust1", since = "1.0.0")]
+#[doc(notable_trait)]
+#[cfg_attr(not(test), rustc_diagnostic_item = "IoRead")]
+pub trait Read {
+ /// Pull some bytes from this source into the specified buffer, returning
+ /// how many bytes were read.
+ ///
+ /// This function does not provide any guarantees about whether it blocks
+ /// waiting for data, but if an object needs to block for a read and cannot,
+ /// it will typically signal this via an [`Err`] return value.
+ ///
+ /// If the return value of this method is [`Ok(n)`], then implementations must
+ /// guarantee that `0 <= n <= buf.len()`. A nonzero `n` value indicates
+ /// that the buffer `buf` has been filled in with `n` bytes of data from this
+ /// source. If `n` is `0`, then it can indicate one of two scenarios:
+ ///
+ /// 1. This reader has reached its "end of file" and will likely no longer
+ /// be able to produce bytes. Note that this does not mean that the
+ /// reader will *always* no longer be able to produce bytes. As an example,
+ /// on Linux, this method will call the `recv` syscall for a [`TcpStream`],
+ /// where returning zero indicates the connection was shut down correctly. While
+ /// for [`File`], it is possible to reach the end of file and get zero as result,
+ /// but if more data is appended to the file, future calls to `read` will return
+ /// more data.
+ /// 2. The buffer specified was 0 bytes in length.
+ ///
+ /// It is not an error if the returned value `n` is smaller than the buffer size,
+ /// even when the reader is not at the end of the stream yet.
+ /// This may happen for example because fewer bytes are actually available right now
+ /// (e. g. being close to end-of-file) or because read() was interrupted by a signal.
+ ///
+ /// As this trait is safe to implement, callers cannot rely on `n <= buf.len()` for safety.
+ /// Extra care needs to be taken when `unsafe` functions are used to access the read bytes.
+ /// Callers have to ensure that no unchecked out-of-bounds accesses are possible even if
+ /// `n > buf.len()`.
+ ///
+ /// No guarantees are provided about the contents of `buf` when this
+ /// function is called, implementations cannot rely on any property of the
+ /// contents of `buf` being true. It is recommended that *implementations*
+ /// only write data to `buf` instead of reading its contents.
+ ///
+ /// Correspondingly, however, *callers* of this method must not assume any guarantees
+ /// about how the implementation uses `buf`. The trait is safe to implement,
+ /// so it is possible that the code that's supposed to write to the buffer might also read
+ /// from it. It is your responsibility to make sure that `buf` is initialized
+ /// before calling `read`. Calling `read` with an uninitialized `buf` (of the kind one
+ /// obtains via [`MaybeUninit<T>`]) is not safe, and can lead to undefined behavior.
+ ///
+ /// [`MaybeUninit<T>`]: crate::mem::MaybeUninit
+ ///
+ /// # Errors
+ ///
+ /// If this function encounters any form of I/O or other error, an error
+ /// variant will be returned. If an error is returned then it must be
+ /// guaranteed that no bytes were read.
+ ///
+ /// An error of the [`ErrorKind::Interrupted`] kind is non-fatal and the read
+ /// operation should be retried if there is nothing else to do.
+ ///
+ /// # Examples
+ ///
+ /// [`File`]s implement `Read`:
+ ///
+ /// [`Ok(n)`]: Ok
+ /// [`File`]: crate::fs::File
+ /// [`TcpStream`]: crate::net::TcpStream
+ ///
+ /// ```no_run
+ /// use std::io;
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// fn main() -> io::Result<()> {
+ /// let mut f = File::open("foo.txt")?;
+ /// let mut buffer = [0; 10];
+ ///
+ /// // read up to 10 bytes
+ /// let n = f.read(&mut buffer[..])?;
+ ///
+ /// println!("The bytes: {:?}", &buffer[..n]);
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
+
+ /// Like `read`, except that it reads into a slice of buffers.
+ ///
+ /// Data is copied to fill each buffer in order, with the final buffer
+ /// written to possibly being only partially filled. This method must
+ /// behave equivalently to a single call to `read` with concatenated
+ /// buffers.
+ ///
+ /// The default implementation calls `read` with either the first nonempty
+ /// buffer provided, or an empty one if none exists.
+ #[stable(feature = "iovec", since = "1.36.0")]
+ fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize> {
+ default_read_vectored(|b| self.read(b), bufs)
+ }
+
+ /// Determines if this `Read`er has an efficient `read_vectored`
+ /// implementation.
+ ///
+ /// If a `Read`er does not override the default `read_vectored`
+ /// implementation, code using it may want to avoid the method all together
+ /// and coalesce writes into a single buffer for higher performance.
+ ///
+ /// The default implementation returns `false`.
+ #[unstable(feature = "can_vector", issue = "69941")]
+ fn is_read_vectored(&self) -> bool {
+ false
+ }
+
+ /// Read all bytes until EOF in this source, placing them into `buf`.
+ ///
+ /// All bytes read from this source will be appended to the specified buffer
+ /// `buf`. This function will continuously call [`read()`] to append more data to
+ /// `buf` until [`read()`] returns either [`Ok(0)`] or an error of
+ /// non-[`ErrorKind::Interrupted`] kind.
+ ///
+ /// If successful, this function will return the total number of bytes read.
+ ///
+ /// # Errors
+ ///
+ /// If this function encounters an error of the kind
+ /// [`ErrorKind::Interrupted`] then the error is ignored and the operation
+ /// will continue.
+ ///
+ /// If any other read error is encountered then this function immediately
+ /// returns. Any bytes which have already been read will be appended to
+ /// `buf`.
+ ///
+ /// # Examples
+ ///
+ /// [`File`]s implement `Read`:
+ ///
+ /// [`read()`]: Read::read
+ /// [`Ok(0)`]: Ok
+ /// [`File`]: crate::fs::File
+ ///
+ /// ```no_run
+ /// use std::io;
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// fn main() -> io::Result<()> {
+ /// let mut f = File::open("foo.txt")?;
+ /// let mut buffer = Vec::new();
+ ///
+ /// // read the whole file
+ /// f.read_to_end(&mut buffer)?;
+ /// Ok(())
+ /// }
+ /// ```
+ ///
+ /// (See also the [`std::fs::read`] convenience function for reading from a
+ /// file.)
+ ///
+ /// [`std::fs::read`]: crate::fs::read
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
+ default_read_to_end(self, buf)
+ }
+
+ /// Read all bytes until EOF in this source, appending them to `buf`.
+ ///
+ /// If successful, this function returns the number of bytes which were read
+ /// and appended to `buf`.
+ ///
+ /// # Errors
+ ///
+ /// If the data in this stream is *not* valid UTF-8 then an error is
+ /// returned and `buf` is unchanged.
+ ///
+ /// See [`read_to_end`] for other error semantics.
+ ///
+ /// [`read_to_end`]: Read::read_to_end
+ ///
+ /// # Examples
+ ///
+ /// [`File`]s implement `Read`:
+ ///
+ /// [`File`]: crate::fs::File
+ ///
+ /// ```no_run
+ /// use std::io;
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// fn main() -> io::Result<()> {
+ /// let mut f = File::open("foo.txt")?;
+ /// let mut buffer = String::new();
+ ///
+ /// f.read_to_string(&mut buffer)?;
+ /// Ok(())
+ /// }
+ /// ```
+ ///
+ /// (See also the [`std::fs::read_to_string`] convenience function for
+ /// reading from a file.)
+ ///
+ /// [`std::fs::read_to_string`]: crate::fs::read_to_string
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn read_to_string(&mut self, buf: &mut String) -> Result<usize> {
+ default_read_to_string(self, buf)
+ }
+
+ /// Read the exact number of bytes required to fill `buf`.
+ ///
+ /// This function reads as many bytes as necessary to completely fill the
+ /// specified buffer `buf`.
+ ///
+ /// No guarantees are provided about the contents of `buf` when this
+ /// function is called, implementations cannot rely on any property of the
+ /// contents of `buf` being true. It is recommended that implementations
+ /// only write data to `buf` instead of reading its contents. The
+ /// documentation on [`read`] has a more detailed explanation on this
+ /// subject.
+ ///
+ /// # Errors
+ ///
+ /// If this function encounters an error of the kind
+ /// [`ErrorKind::Interrupted`] then the error is ignored and the operation
+ /// will continue.
+ ///
+ /// If this function encounters an "end of file" before completely filling
+ /// the buffer, it returns an error of the kind [`ErrorKind::UnexpectedEof`].
+ /// The contents of `buf` are unspecified in this case.
+ ///
+ /// If any other read error is encountered then this function immediately
+ /// returns. The contents of `buf` are unspecified in this case.
+ ///
+ /// If this function returns an error, it is unspecified how many bytes it
+ /// has read, but it will never read more than would be necessary to
+ /// completely fill the buffer.
+ ///
+ /// # Examples
+ ///
+ /// [`File`]s implement `Read`:
+ ///
+ /// [`read`]: Read::read
+ /// [`File`]: crate::fs::File
+ ///
+ /// ```no_run
+ /// use std::io;
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// fn main() -> io::Result<()> {
+ /// let mut f = File::open("foo.txt")?;
+ /// let mut buffer = [0; 10];
+ ///
+ /// // read exactly 10 bytes
+ /// f.read_exact(&mut buffer)?;
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "read_exact", since = "1.6.0")]
+ fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
+ default_read_exact(self, buf)
+ }
+
+ /// Pull some bytes from this source into the specified buffer.
+ ///
+ /// This is equivalent to the [`read`](Read::read) method, except that it is passed a [`ReadBuf`] rather than `[u8]` to allow use
+ /// with uninitialized buffers. The new data will be appended to any existing contents of `buf`.
+ ///
+ /// The default implementation delegates to `read`.
+ #[unstable(feature = "read_buf", issue = "78485")]
+ fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> Result<()> {
+ default_read_buf(|b| self.read(b), buf)
+ }
+
+ /// Read the exact number of bytes required to fill `buf`.
+ ///
+ /// This is equivalent to the [`read_exact`](Read::read_exact) method, except that it is passed a [`ReadBuf`] rather than `[u8]` to
+ /// allow use with uninitialized buffers.
+ #[unstable(feature = "read_buf", issue = "78485")]
+ fn read_buf_exact(&mut self, buf: &mut ReadBuf<'_>) -> Result<()> {
+ while buf.remaining() > 0 {
+ let prev_filled = buf.filled().len();
+ match self.read_buf(buf) {
+ Ok(()) => {}
+ Err(e) if e.kind() == ErrorKind::Interrupted => continue,
+ Err(e) => return Err(e),
+ }
+
+ if buf.filled().len() == prev_filled {
+ return Err(Error::new(ErrorKind::UnexpectedEof, "failed to fill buffer"));
+ }
+ }
+
+ Ok(())
+ }
+
+ /// Creates a "by reference" adaptor for this instance of `Read`.
+ ///
+ /// The returned adapter also implements `Read` and will simply borrow this
+ /// current reader.
+ ///
+ /// # Examples
+ ///
+ /// [`File`]s implement `Read`:
+ ///
+ /// [`File`]: crate::fs::File
+ ///
+ /// ```no_run
+ /// use std::io;
+ /// use std::io::Read;
+ /// use std::fs::File;
+ ///
+ /// fn main() -> io::Result<()> {
+ /// let mut f = File::open("foo.txt")?;
+ /// let mut buffer = Vec::new();
+ /// let mut other_buffer = Vec::new();
+ ///
+ /// {
+ /// let reference = f.by_ref();
+ ///
+ /// // read at most 5 bytes
+ /// reference.take(5).read_to_end(&mut buffer)?;
+ ///
+ /// } // drop our &mut reference so we can use f again
+ ///
+ /// // original file still usable, read the rest
+ /// f.read_to_end(&mut other_buffer)?;
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn by_ref(&mut self) -> &mut Self
+ where
+ Self: Sized,
+ {
+ self
+ }
+
+ /// Transforms this `Read` instance to an [`Iterator`] over its bytes.
+ ///
+ /// The returned type implements [`Iterator`] where the [`Item`] is
+ /// <code>[Result]<[u8], [io::Error]></code>.
+ /// The yielded item is [`Ok`] if a byte was successfully read and [`Err`]
+ /// otherwise. EOF is mapped to returning [`None`] from this iterator.
+ ///
+ /// # Examples
+ ///
+ /// [`File`]s implement `Read`:
+ ///
+ /// [`Item`]: Iterator::Item
+ /// [`File`]: crate::fs::File "fs::File"
+ /// [Result]: crate::result::Result "Result"
+ /// [io::Error]: self::Error "io::Error"
+ ///
+ /// ```no_run
+ /// use std::io;
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// fn main() -> io::Result<()> {
+ /// let f = File::open("foo.txt")?;
+ ///
+ /// for byte in f.bytes() {
+ /// println!("{}", byte.unwrap());
+ /// }
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn bytes(self) -> Bytes<Self>
+ where
+ Self: Sized,
+ {
+ Bytes { inner: self }
+ }
+
+ /// Creates an adapter which will chain this stream with another.
+ ///
+ /// The returned `Read` instance will first read all bytes from this object
+ /// until EOF is encountered. Afterwards the output is equivalent to the
+ /// output of `next`.
+ ///
+ /// # Examples
+ ///
+ /// [`File`]s implement `Read`:
+ ///
+ /// [`File`]: crate::fs::File
+ ///
+ /// ```no_run
+ /// use std::io;
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// fn main() -> io::Result<()> {
+ /// let f1 = File::open("foo.txt")?;
+ /// let f2 = File::open("bar.txt")?;
+ ///
+ /// let mut handle = f1.chain(f2);
+ /// let mut buffer = String::new();
+ ///
+ /// // read the value into a String. We could use any Read method here,
+ /// // this is just one example.
+ /// handle.read_to_string(&mut buffer)?;
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn chain<R: Read>(self, next: R) -> Chain<Self, R>
+ where
+ Self: Sized,
+ {
+ Chain { first: self, second: next, done_first: false }
+ }
+
+ /// Creates an adapter which will read at most `limit` bytes from it.
+ ///
+ /// This function returns a new instance of `Read` which will read at most
+ /// `limit` bytes, after which it will always return EOF ([`Ok(0)`]). Any
+ /// read errors will not count towards the number of bytes read and future
+ /// calls to [`read()`] may succeed.
+ ///
+ /// # Examples
+ ///
+ /// [`File`]s implement `Read`:
+ ///
+ /// [`File`]: crate::fs::File
+ /// [`Ok(0)`]: Ok
+ /// [`read()`]: Read::read
+ ///
+ /// ```no_run
+ /// use std::io;
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// fn main() -> io::Result<()> {
+ /// let f = File::open("foo.txt")?;
+ /// let mut buffer = [0; 5];
+ ///
+ /// // read at most five bytes
+ /// let mut handle = f.take(5);
+ ///
+ /// handle.read(&mut buffer)?;
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn take(self, limit: u64) -> Take<Self>
+ where
+ Self: Sized,
+ {
+ Take { inner: self, limit }
+ }
+}
+
+/// Read all bytes from a [reader][Read] into a new [`String`].
+///
+/// This is a convenience function for [`Read::read_to_string`]. Using this
+/// function avoids having to create a variable first and provides more type
+/// safety since you can only get the buffer out if there were no errors. (If you
+/// use [`Read::read_to_string`] you have to remember to check whether the read
+/// succeeded because otherwise your buffer will be empty or only partially full.)
+///
+/// # Performance
+///
+/// The downside of this function's increased ease of use and type safety is
+/// that it gives you less control over performance. For example, you can't
+/// pre-allocate memory like you can using [`String::with_capacity`] and
+/// [`Read::read_to_string`]. Also, you can't re-use the buffer if an error
+/// occurs while reading.
+///
+/// In many cases, this function's performance will be adequate and the ease of use
+/// and type safety tradeoffs will be worth it. However, there are cases where you
+/// need more control over performance, and in those cases you should definitely use
+/// [`Read::read_to_string`] directly.
+///
+/// Note that in some special cases, such as when reading files, this function will
+/// pre-allocate memory based on the size of the input it is reading. In those
+/// cases, the performance should be as good as if you had used
+/// [`Read::read_to_string`] with a manually pre-allocated buffer.
+///
+/// # Errors
+///
+/// This function forces you to handle errors because the output (the `String`)
+/// is wrapped in a [`Result`]. See [`Read::read_to_string`] for the errors
+/// that can occur. If any error occurs, you will get an [`Err`], so you
+/// don't have to worry about your buffer being empty or partially full.
+///
+/// # Examples
+///
+/// ```no_run
+/// #![feature(io_read_to_string)]
+///
+/// # use std::io;
+/// fn main() -> io::Result<()> {
+/// let stdin = io::read_to_string(io::stdin())?;
+/// println!("Stdin was:");
+/// println!("{stdin}");
+/// Ok(())
+/// }
+/// ```
+#[unstable(feature = "io_read_to_string", issue = "80218")]
+pub fn read_to_string<R: Read>(mut reader: R) -> Result<String> {
+ let mut buf = String::new();
+ reader.read_to_string(&mut buf)?;
+ Ok(buf)
+}
+
+/// A buffer type used with `Read::read_vectored`.
+///
+/// It is semantically a wrapper around an `&mut [u8]`, but is guaranteed to be
+/// ABI compatible with the `iovec` type on Unix platforms and `WSABUF` on
+/// Windows.
+#[stable(feature = "iovec", since = "1.36.0")]
+#[repr(transparent)]
+pub struct IoSliceMut<'a>(sys::io::IoSliceMut<'a>);
+
+#[stable(feature = "iovec-send-sync", since = "1.44.0")]
+unsafe impl<'a> Send for IoSliceMut<'a> {}
+
+#[stable(feature = "iovec-send-sync", since = "1.44.0")]
+unsafe impl<'a> Sync for IoSliceMut<'a> {}
+
+#[stable(feature = "iovec", since = "1.36.0")]
+impl<'a> fmt::Debug for IoSliceMut<'a> {
+ fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt::Debug::fmt(self.0.as_slice(), fmt)
+ }
+}
+
+impl<'a> IoSliceMut<'a> {
+ /// Creates a new `IoSliceMut` wrapping a byte slice.
+ ///
+ /// # Panics
+ ///
+ /// Panics on Windows if the slice is larger than 4GB.
+ #[stable(feature = "iovec", since = "1.36.0")]
+ #[inline]
+ pub fn new(buf: &'a mut [u8]) -> IoSliceMut<'a> {
+ IoSliceMut(sys::io::IoSliceMut::new(buf))
+ }
+
+ /// Advance the internal cursor of the slice.
+ ///
+ /// Also see [`IoSliceMut::advance_slices`] to advance the cursors of
+ /// multiple buffers.
+ ///
+ /// # Panics
+ ///
+ /// Panics when trying to advance beyond the end of the slice.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// #![feature(io_slice_advance)]
+ ///
+ /// use std::io::IoSliceMut;
+ /// use std::ops::Deref;
+ ///
+ /// let mut data = [1; 8];
+ /// let mut buf = IoSliceMut::new(&mut data);
+ ///
+ /// // Mark 3 bytes as read.
+ /// buf.advance(3);
+ /// assert_eq!(buf.deref(), [1; 5].as_ref());
+ /// ```
+ #[unstable(feature = "io_slice_advance", issue = "62726")]
+ #[inline]
+ pub fn advance(&mut self, n: usize) {
+ self.0.advance(n)
+ }
+
+ /// Advance a slice of slices.
+ ///
+ /// Shrinks the slice to remove any `IoSliceMut`s that are fully advanced over.
+ /// If the cursor ends up in the middle of an `IoSliceMut`, it is modified
+ /// to start at that cursor.
+ ///
+ /// For example, if we have a slice of two 8-byte `IoSliceMut`s, and we advance by 10 bytes,
+ /// the result will only include the second `IoSliceMut`, advanced by 2 bytes.
+ ///
+ /// # Panics
+ ///
+ /// Panics when trying to advance beyond the end of the slices.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// #![feature(io_slice_advance)]
+ ///
+ /// use std::io::IoSliceMut;
+ /// use std::ops::Deref;
+ ///
+ /// let mut buf1 = [1; 8];
+ /// let mut buf2 = [2; 16];
+ /// let mut buf3 = [3; 8];
+ /// let mut bufs = &mut [
+ /// IoSliceMut::new(&mut buf1),
+ /// IoSliceMut::new(&mut buf2),
+ /// IoSliceMut::new(&mut buf3),
+ /// ][..];
+ ///
+ /// // Mark 10 bytes as read.
+ /// IoSliceMut::advance_slices(&mut bufs, 10);
+ /// assert_eq!(bufs[0].deref(), [2; 14].as_ref());
+ /// assert_eq!(bufs[1].deref(), [3; 8].as_ref());
+ /// ```
+ #[unstable(feature = "io_slice_advance", issue = "62726")]
+ #[inline]
+ pub fn advance_slices(bufs: &mut &mut [IoSliceMut<'a>], n: usize) {
+ // Number of buffers to remove.
+ let mut remove = 0;
+ // Total length of all the to be removed buffers.
+ let mut accumulated_len = 0;
+ for buf in bufs.iter() {
+ if accumulated_len + buf.len() > n {
+ break;
+ } else {
+ accumulated_len += buf.len();
+ remove += 1;
+ }
+ }
+
+ *bufs = &mut replace(bufs, &mut [])[remove..];
+ if bufs.is_empty() {
+ assert!(n == accumulated_len, "advancing io slices beyond their length");
+ } else {
+ bufs[0].advance(n - accumulated_len)
+ }
+ }
+}
+
+#[stable(feature = "iovec", since = "1.36.0")]
+impl<'a> Deref for IoSliceMut<'a> {
+ type Target = [u8];
+
+ #[inline]
+ fn deref(&self) -> &[u8] {
+ self.0.as_slice()
+ }
+}
+
+#[stable(feature = "iovec", since = "1.36.0")]
+impl<'a> DerefMut for IoSliceMut<'a> {
+ #[inline]
+ fn deref_mut(&mut self) -> &mut [u8] {
+ self.0.as_mut_slice()
+ }
+}
+
+/// A buffer type used with `Write::write_vectored`.
+///
+/// It is semantically a wrapper around a `&[u8]`, but is guaranteed to be
+/// ABI compatible with the `iovec` type on Unix platforms and `WSABUF` on
+/// Windows.
+#[stable(feature = "iovec", since = "1.36.0")]
+#[derive(Copy, Clone)]
+#[repr(transparent)]
+pub struct IoSlice<'a>(sys::io::IoSlice<'a>);
+
+#[stable(feature = "iovec-send-sync", since = "1.44.0")]
+unsafe impl<'a> Send for IoSlice<'a> {}
+
+#[stable(feature = "iovec-send-sync", since = "1.44.0")]
+unsafe impl<'a> Sync for IoSlice<'a> {}
+
+#[stable(feature = "iovec", since = "1.36.0")]
+impl<'a> fmt::Debug for IoSlice<'a> {
+ fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt::Debug::fmt(self.0.as_slice(), fmt)
+ }
+}
+
+impl<'a> IoSlice<'a> {
+ /// Creates a new `IoSlice` wrapping a byte slice.
+ ///
+ /// # Panics
+ ///
+ /// Panics on Windows if the slice is larger than 4GB.
+ #[stable(feature = "iovec", since = "1.36.0")]
+ #[must_use]
+ #[inline]
+ pub fn new(buf: &'a [u8]) -> IoSlice<'a> {
+ IoSlice(sys::io::IoSlice::new(buf))
+ }
+
+ /// Advance the internal cursor of the slice.
+ ///
+ /// Also see [`IoSlice::advance_slices`] to advance the cursors of multiple
+ /// buffers.
+ ///
+ /// # Panics
+ ///
+ /// Panics when trying to advance beyond the end of the slice.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// #![feature(io_slice_advance)]
+ ///
+ /// use std::io::IoSlice;
+ /// use std::ops::Deref;
+ ///
+ /// let data = [1; 8];
+ /// let mut buf = IoSlice::new(&data);
+ ///
+ /// // Mark 3 bytes as read.
+ /// buf.advance(3);
+ /// assert_eq!(buf.deref(), [1; 5].as_ref());
+ /// ```
+ #[unstable(feature = "io_slice_advance", issue = "62726")]
+ #[inline]
+ pub fn advance(&mut self, n: usize) {
+ self.0.advance(n)
+ }
+
+ /// Advance a slice of slices.
+ ///
+ /// Shrinks the slice to remove any `IoSlice`s that are fully advanced over.
+ /// If the cursor ends up in the middle of an `IoSlice`, it is modified
+ /// to start at that cursor.
+ ///
+ /// For example, if we have a slice of two 8-byte `IoSlice`s, and we advance by 10 bytes,
+ /// the result will only include the second `IoSlice`, advanced by 2 bytes.
+ ///
+ /// # Panics
+ ///
+ /// Panics when trying to advance beyond the end of the slices.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// #![feature(io_slice_advance)]
+ ///
+ /// use std::io::IoSlice;
+ /// use std::ops::Deref;
+ ///
+ /// let buf1 = [1; 8];
+ /// let buf2 = [2; 16];
+ /// let buf3 = [3; 8];
+ /// let mut bufs = &mut [
+ /// IoSlice::new(&buf1),
+ /// IoSlice::new(&buf2),
+ /// IoSlice::new(&buf3),
+ /// ][..];
+ ///
+ /// // Mark 10 bytes as written.
+ /// IoSlice::advance_slices(&mut bufs, 10);
+ /// assert_eq!(bufs[0].deref(), [2; 14].as_ref());
+ /// assert_eq!(bufs[1].deref(), [3; 8].as_ref());
+ #[unstable(feature = "io_slice_advance", issue = "62726")]
+ #[inline]
+ pub fn advance_slices(bufs: &mut &mut [IoSlice<'a>], n: usize) {
+ // Number of buffers to remove.
+ let mut remove = 0;
+ // Total length of all the to be removed buffers.
+ let mut accumulated_len = 0;
+ for buf in bufs.iter() {
+ if accumulated_len + buf.len() > n {
+ break;
+ } else {
+ accumulated_len += buf.len();
+ remove += 1;
+ }
+ }
+
+ *bufs = &mut replace(bufs, &mut [])[remove..];
+ if bufs.is_empty() {
+ assert!(n == accumulated_len, "advancing io slices beyond their length");
+ } else {
+ bufs[0].advance(n - accumulated_len)
+ }
+ }
+}
+
+#[stable(feature = "iovec", since = "1.36.0")]
+impl<'a> Deref for IoSlice<'a> {
+ type Target = [u8];
+
+ #[inline]
+ fn deref(&self) -> &[u8] {
+ self.0.as_slice()
+ }
+}
+
+/// A trait for objects which are byte-oriented sinks.
+///
+/// Implementors of the `Write` trait are sometimes called 'writers'.
+///
+/// Writers are defined by two required methods, [`write`] and [`flush`]:
+///
+/// * The [`write`] method will attempt to write some data into the object,
+/// returning how many bytes were successfully written.
+///
+/// * The [`flush`] method is useful for adapters and explicit buffers
+/// themselves for ensuring that all buffered data has been pushed out to the
+/// 'true sink'.
+///
+/// Writers are intended to be composable with one another. Many implementors
+/// throughout [`std::io`] take and provide types which implement the `Write`
+/// trait.
+///
+/// [`write`]: Write::write
+/// [`flush`]: Write::flush
+/// [`std::io`]: self
+///
+/// # Examples
+///
+/// ```no_run
+/// use std::io::prelude::*;
+/// use std::fs::File;
+///
+/// fn main() -> std::io::Result<()> {
+/// let data = b"some bytes";
+///
+/// let mut pos = 0;
+/// let mut buffer = File::create("foo.txt")?;
+///
+/// while pos < data.len() {
+/// let bytes_written = buffer.write(&data[pos..])?;
+/// pos += bytes_written;
+/// }
+/// Ok(())
+/// }
+/// ```
+///
+/// The trait also provides convenience methods like [`write_all`], which calls
+/// `write` in a loop until its entire input has been written.
+///
+/// [`write_all`]: Write::write_all
+#[stable(feature = "rust1", since = "1.0.0")]
+#[doc(notable_trait)]
+#[cfg_attr(not(test), rustc_diagnostic_item = "IoWrite")]
+pub trait Write {
+ /// Write a buffer into this writer, returning how many bytes were written.
+ ///
+ /// This function will attempt to write the entire contents of `buf`, but
+ /// the entire write might not succeed, or the write may also generate an
+ /// error. A call to `write` represents *at most one* attempt to write to
+ /// any wrapped object.
+ ///
+ /// Calls to `write` are not guaranteed to block waiting for data to be
+ /// written, and a write which would otherwise block can be indicated through
+ /// an [`Err`] variant.
+ ///
+ /// If the return value is [`Ok(n)`] then it must be guaranteed that
+ /// `n <= buf.len()`. A return value of `0` typically means that the
+ /// underlying object is no longer able to accept bytes and will likely not
+ /// be able to in the future as well, or that the buffer provided is empty.
+ ///
+ /// # Errors
+ ///
+ /// Each call to `write` may generate an I/O error indicating that the
+ /// operation could not be completed. If an error is returned then no bytes
+ /// in the buffer were written to this writer.
+ ///
+ /// It is **not** considered an error if the entire buffer could not be
+ /// written to this writer.
+ ///
+ /// An error of the [`ErrorKind::Interrupted`] kind is non-fatal and the
+ /// write operation should be retried if there is nothing else to do.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// fn main() -> std::io::Result<()> {
+ /// let mut buffer = File::create("foo.txt")?;
+ ///
+ /// // Writes some prefix of the byte string, not necessarily all of it.
+ /// buffer.write(b"some bytes")?;
+ /// Ok(())
+ /// }
+ /// ```
+ ///
+ /// [`Ok(n)`]: Ok
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn write(&mut self, buf: &[u8]) -> Result<usize>;
+
+ /// Like [`write`], except that it writes from a slice of buffers.
+ ///
+ /// Data is copied from each buffer in order, with the final buffer
+ /// read from possibly being only partially consumed. This method must
+ /// behave as a call to [`write`] with the buffers concatenated would.
+ ///
+ /// The default implementation calls [`write`] with either the first nonempty
+ /// buffer provided, or an empty one if none exists.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::IoSlice;
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// fn main() -> std::io::Result<()> {
+ /// let data1 = [1; 8];
+ /// let data2 = [15; 8];
+ /// let io_slice1 = IoSlice::new(&data1);
+ /// let io_slice2 = IoSlice::new(&data2);
+ ///
+ /// let mut buffer = File::create("foo.txt")?;
+ ///
+ /// // Writes some prefix of the byte string, not necessarily all of it.
+ /// buffer.write_vectored(&[io_slice1, io_slice2])?;
+ /// Ok(())
+ /// }
+ /// ```
+ ///
+ /// [`write`]: Write::write
+ #[stable(feature = "iovec", since = "1.36.0")]
+ fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize> {
+ default_write_vectored(|b| self.write(b), bufs)
+ }
+
+ /// Determines if this `Write`r has an efficient [`write_vectored`]
+ /// implementation.
+ ///
+ /// If a `Write`r does not override the default [`write_vectored`]
+ /// implementation, code using it may want to avoid the method all together
+ /// and coalesce writes into a single buffer for higher performance.
+ ///
+ /// The default implementation returns `false`.
+ ///
+ /// [`write_vectored`]: Write::write_vectored
+ #[unstable(feature = "can_vector", issue = "69941")]
+ fn is_write_vectored(&self) -> bool {
+ false
+ }
+
+ /// Flush this output stream, ensuring that all intermediately buffered
+ /// contents reach their destination.
+ ///
+ /// # Errors
+ ///
+ /// It is considered an error if not all bytes could be written due to
+ /// I/O errors or EOF being reached.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::prelude::*;
+ /// use std::io::BufWriter;
+ /// use std::fs::File;
+ ///
+ /// fn main() -> std::io::Result<()> {
+ /// let mut buffer = BufWriter::new(File::create("foo.txt")?);
+ ///
+ /// buffer.write_all(b"some bytes")?;
+ /// buffer.flush()?;
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn flush(&mut self) -> Result<()>;
+
+ /// Attempts to write an entire buffer into this writer.
+ ///
+ /// This method will continuously call [`write`] until there is no more data
+ /// to be written or an error of non-[`ErrorKind::Interrupted`] kind is
+ /// returned. This method will not return until the entire buffer has been
+ /// successfully written or such an error occurs. The first error that is
+ /// not of [`ErrorKind::Interrupted`] kind generated from this method will be
+ /// returned.
+ ///
+ /// If the buffer contains no data, this will never call [`write`].
+ ///
+ /// # Errors
+ ///
+ /// This function will return the first error of
+ /// non-[`ErrorKind::Interrupted`] kind that [`write`] returns.
+ ///
+ /// [`write`]: Write::write
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// fn main() -> std::io::Result<()> {
+ /// let mut buffer = File::create("foo.txt")?;
+ ///
+ /// buffer.write_all(b"some bytes")?;
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn write_all(&mut self, mut buf: &[u8]) -> Result<()> {
+ while !buf.is_empty() {
+ match self.write(buf) {
+ Ok(0) => {
+ return Err(error::const_io_error!(
+ ErrorKind::WriteZero,
+ "failed to write whole buffer",
+ ));
+ }
+ Ok(n) => buf = &buf[n..],
+ Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
+ Err(e) => return Err(e),
+ }
+ }
+ Ok(())
+ }
+
+ /// Attempts to write multiple buffers into this writer.
+ ///
+ /// This method will continuously call [`write_vectored`] until there is no
+ /// more data to be written or an error of non-[`ErrorKind::Interrupted`]
+ /// kind is returned. This method will not return until all buffers have
+ /// been successfully written or such an error occurs. The first error that
+ /// is not of [`ErrorKind::Interrupted`] kind generated from this method
+ /// will be returned.
+ ///
+ /// If the buffer contains no data, this will never call [`write_vectored`].
+ ///
+ /// # Notes
+ ///
+ /// Unlike [`write_vectored`], this takes a *mutable* reference to
+ /// a slice of [`IoSlice`]s, not an immutable one. That's because we need to
+ /// modify the slice to keep track of the bytes already written.
+ ///
+ /// Once this function returns, the contents of `bufs` are unspecified, as
+ /// this depends on how many calls to [`write_vectored`] were necessary. It is
+ /// best to understand this function as taking ownership of `bufs` and to
+ /// not use `bufs` afterwards. The underlying buffers, to which the
+ /// [`IoSlice`]s point (but not the [`IoSlice`]s themselves), are unchanged and
+ /// can be reused.
+ ///
+ /// [`write_vectored`]: Write::write_vectored
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// #![feature(write_all_vectored)]
+ /// # fn main() -> std::io::Result<()> {
+ ///
+ /// use std::io::{Write, IoSlice};
+ ///
+ /// let mut writer = Vec::new();
+ /// let bufs = &mut [
+ /// IoSlice::new(&[1]),
+ /// IoSlice::new(&[2, 3]),
+ /// IoSlice::new(&[4, 5, 6]),
+ /// ];
+ ///
+ /// writer.write_all_vectored(bufs)?;
+ /// // Note: the contents of `bufs` is now undefined, see the Notes section.
+ ///
+ /// assert_eq!(writer, &[1, 2, 3, 4, 5, 6]);
+ /// # Ok(()) }
+ /// ```
+ #[unstable(feature = "write_all_vectored", issue = "70436")]
+ fn write_all_vectored(&mut self, mut bufs: &mut [IoSlice<'_>]) -> Result<()> {
+ // Guarantee that bufs is empty if it contains no data,
+ // to avoid calling write_vectored if there is no data to be written.
+ IoSlice::advance_slices(&mut bufs, 0);
+ while !bufs.is_empty() {
+ match self.write_vectored(bufs) {
+ Ok(0) => {
+ return Err(error::const_io_error!(
+ ErrorKind::WriteZero,
+ "failed to write whole buffer",
+ ));
+ }
+ Ok(n) => IoSlice::advance_slices(&mut bufs, n),
+ Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
+ Err(e) => return Err(e),
+ }
+ }
+ Ok(())
+ }
+
+ /// Writes a formatted string into this writer, returning any error
+ /// encountered.
+ ///
+ /// This method is primarily used to interface with the
+ /// [`format_args!()`] macro, and it is rare that this should
+ /// explicitly be called. The [`write!()`] macro should be favored to
+ /// invoke this method instead.
+ ///
+ /// This function internally uses the [`write_all`] method on
+ /// this trait and hence will continuously write data so long as no errors
+ /// are received. This also means that partial writes are not indicated in
+ /// this signature.
+ ///
+ /// [`write_all`]: Write::write_all
+ ///
+ /// # Errors
+ ///
+ /// This function will return any I/O error reported while formatting.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// fn main() -> std::io::Result<()> {
+ /// let mut buffer = File::create("foo.txt")?;
+ ///
+ /// // this call
+ /// write!(buffer, "{:.*}", 2, 1.234567)?;
+ /// // turns into this:
+ /// buffer.write_fmt(format_args!("{:.*}", 2, 1.234567))?;
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> Result<()> {
+ // Create a shim which translates a Write to a fmt::Write and saves
+ // off I/O errors. instead of discarding them
+ struct Adapter<'a, T: ?Sized + 'a> {
+ inner: &'a mut T,
+ error: Result<()>,
+ }
+
+ impl<T: Write + ?Sized> fmt::Write for Adapter<'_, T> {
+ fn write_str(&mut self, s: &str) -> fmt::Result {
+ match self.inner.write_all(s.as_bytes()) {
+ Ok(()) => Ok(()),
+ Err(e) => {
+ self.error = Err(e);
+ Err(fmt::Error)
+ }
+ }
+ }
+ }
+
+ let mut output = Adapter { inner: self, error: Ok(()) };
+ match fmt::write(&mut output, fmt) {
+ Ok(()) => Ok(()),
+ Err(..) => {
+ // check if the error came from the underlying `Write` or not
+ if output.error.is_err() {
+ output.error
+ } else {
+ Err(error::const_io_error!(ErrorKind::Uncategorized, "formatter error"))
+ }
+ }
+ }
+ }
+
+ /// Creates a "by reference" adapter for this instance of `Write`.
+ ///
+ /// The returned adapter also implements `Write` and will simply borrow this
+ /// current writer.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::Write;
+ /// use std::fs::File;
+ ///
+ /// fn main() -> std::io::Result<()> {
+ /// let mut buffer = File::create("foo.txt")?;
+ ///
+ /// let reference = buffer.by_ref();
+ ///
+ /// // we can use reference just like our original buffer
+ /// reference.write_all(b"some bytes")?;
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn by_ref(&mut self) -> &mut Self
+ where
+ Self: Sized,
+ {
+ self
+ }
+}
+
+/// The `Seek` trait provides a cursor which can be moved within a stream of
+/// bytes.
+///
+/// The stream typically has a fixed size, allowing seeking relative to either
+/// end or the current offset.
+///
+/// # Examples
+///
+/// [`File`]s implement `Seek`:
+///
+/// [`File`]: crate::fs::File
+///
+/// ```no_run
+/// use std::io;
+/// use std::io::prelude::*;
+/// use std::fs::File;
+/// use std::io::SeekFrom;
+///
+/// fn main() -> io::Result<()> {
+/// let mut f = File::open("foo.txt")?;
+///
+/// // move the cursor 42 bytes from the start of the file
+/// f.seek(SeekFrom::Start(42))?;
+/// Ok(())
+/// }
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+pub trait Seek {
+ /// Seek to an offset, in bytes, in a stream.
+ ///
+ /// A seek beyond the end of a stream is allowed, but behavior is defined
+ /// by the implementation.
+ ///
+ /// If the seek operation completed successfully,
+ /// this method returns the new position from the start of the stream.
+ /// That position can be used later with [`SeekFrom::Start`].
+ ///
+ /// # Errors
+ ///
+ /// Seeking can fail, for example because it might involve flushing a buffer.
+ ///
+ /// Seeking to a negative offset is considered an error.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn seek(&mut self, pos: SeekFrom) -> Result<u64>;
+
+ /// Rewind to the beginning of a stream.
+ ///
+ /// This is a convenience method, equivalent to `seek(SeekFrom::Start(0))`.
+ ///
+ /// # Errors
+ ///
+ /// Rewinding can fail, for example because it might involve flushing a buffer.
+ ///
+ /// # Example
+ ///
+ /// ```no_run
+ /// use std::io::{Read, Seek, Write};
+ /// use std::fs::OpenOptions;
+ ///
+ /// let mut f = OpenOptions::new()
+ /// .write(true)
+ /// .read(true)
+ /// .create(true)
+ /// .open("foo.txt").unwrap();
+ ///
+ /// let hello = "Hello!\n";
+ /// write!(f, "{hello}").unwrap();
+ /// f.rewind().unwrap();
+ ///
+ /// let mut buf = String::new();
+ /// f.read_to_string(&mut buf).unwrap();
+ /// assert_eq!(&buf, hello);
+ /// ```
+ #[stable(feature = "seek_rewind", since = "1.55.0")]
+ fn rewind(&mut self) -> Result<()> {
+ self.seek(SeekFrom::Start(0))?;
+ Ok(())
+ }
+
+ /// Returns the length of this stream (in bytes).
+ ///
+ /// This method is implemented using up to three seek operations. If this
+ /// method returns successfully, the seek position is unchanged (i.e. the
+ /// position before calling this method is the same as afterwards).
+ /// However, if this method returns an error, the seek position is
+ /// unspecified.
+ ///
+ /// If you need to obtain the length of *many* streams and you don't care
+ /// about the seek position afterwards, you can reduce the number of seek
+ /// operations by simply calling `seek(SeekFrom::End(0))` and using its
+ /// return value (it is also the stream length).
+ ///
+ /// Note that length of a stream can change over time (for example, when
+ /// data is appended to a file). So calling this method multiple times does
+ /// not necessarily return the same length each time.
+ ///
+ /// # Example
+ ///
+ /// ```no_run
+ /// #![feature(seek_stream_len)]
+ /// use std::{
+ /// io::{self, Seek},
+ /// fs::File,
+ /// };
+ ///
+ /// fn main() -> io::Result<()> {
+ /// let mut f = File::open("foo.txt")?;
+ ///
+ /// let len = f.stream_len()?;
+ /// println!("The file is currently {len} bytes long");
+ /// Ok(())
+ /// }
+ /// ```
+ #[unstable(feature = "seek_stream_len", issue = "59359")]
+ fn stream_len(&mut self) -> Result<u64> {
+ let old_pos = self.stream_position()?;
+ let len = self.seek(SeekFrom::End(0))?;
+
+ // Avoid seeking a third time when we were already at the end of the
+ // stream. The branch is usually way cheaper than a seek operation.
+ if old_pos != len {
+ self.seek(SeekFrom::Start(old_pos))?;
+ }
+
+ Ok(len)
+ }
+
+ /// Returns the current seek position from the start of the stream.
+ ///
+ /// This is equivalent to `self.seek(SeekFrom::Current(0))`.
+ ///
+ /// # Example
+ ///
+ /// ```no_run
+ /// use std::{
+ /// io::{self, BufRead, BufReader, Seek},
+ /// fs::File,
+ /// };
+ ///
+ /// fn main() -> io::Result<()> {
+ /// let mut f = BufReader::new(File::open("foo.txt")?);
+ ///
+ /// let before = f.stream_position()?;
+ /// f.read_line(&mut String::new())?;
+ /// let after = f.stream_position()?;
+ ///
+ /// println!("The first line was {} bytes long", after - before);
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "seek_convenience", since = "1.51.0")]
+ fn stream_position(&mut self) -> Result<u64> {
+ self.seek(SeekFrom::Current(0))
+ }
+}
+
+/// Enumeration of possible methods to seek within an I/O object.
+///
+/// It is used by the [`Seek`] trait.
+#[derive(Copy, PartialEq, Eq, Clone, Debug)]
+#[stable(feature = "rust1", since = "1.0.0")]
+pub enum SeekFrom {
+ /// Sets the offset to the provided number of bytes.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ Start(#[stable(feature = "rust1", since = "1.0.0")] u64),
+
+ /// Sets the offset to the size of this object plus the specified number of
+ /// bytes.
+ ///
+ /// It is possible to seek beyond the end of an object, but it's an error to
+ /// seek before byte 0.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ End(#[stable(feature = "rust1", since = "1.0.0")] i64),
+
+ /// Sets the offset to the current position plus the specified number of
+ /// bytes.
+ ///
+ /// It is possible to seek beyond the end of an object, but it's an error to
+ /// seek before byte 0.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ Current(#[stable(feature = "rust1", since = "1.0.0")] i64),
+}
+
+fn read_until<R: BufRead + ?Sized>(r: &mut R, delim: u8, buf: &mut Vec<u8>) -> Result<usize> {
+ let mut read = 0;
+ loop {
+ let (done, used) = {
+ let available = match r.fill_buf() {
+ Ok(n) => n,
+ Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
+ Err(e) => return Err(e),
+ };
+ match memchr::memchr(delim, available) {
+ Some(i) => {
+ buf.extend_from_slice(&available[..=i]);
+ (true, i + 1)
+ }
+ None => {
+ buf.extend_from_slice(available);
+ (false, available.len())
+ }
+ }
+ };
+ r.consume(used);
+ read += used;
+ if done || used == 0 {
+ return Ok(read);
+ }
+ }
+}
+
+/// A `BufRead` is a type of `Read`er which has an internal buffer, allowing it
+/// to perform extra ways of reading.
+///
+/// For example, reading line-by-line is inefficient without using a buffer, so
+/// if you want to read by line, you'll need `BufRead`, which includes a
+/// [`read_line`] method as well as a [`lines`] iterator.
+///
+/// # Examples
+///
+/// A locked standard input implements `BufRead`:
+///
+/// ```no_run
+/// use std::io;
+/// use std::io::prelude::*;
+///
+/// let stdin = io::stdin();
+/// for line in stdin.lock().lines() {
+/// println!("{}", line.unwrap());
+/// }
+/// ```
+///
+/// If you have something that implements [`Read`], you can use the [`BufReader`
+/// type][`BufReader`] to turn it into a `BufRead`.
+///
+/// For example, [`File`] implements [`Read`], but not `BufRead`.
+/// [`BufReader`] to the rescue!
+///
+/// [`File`]: crate::fs::File
+/// [`read_line`]: BufRead::read_line
+/// [`lines`]: BufRead::lines
+///
+/// ```no_run
+/// use std::io::{self, BufReader};
+/// use std::io::prelude::*;
+/// use std::fs::File;
+///
+/// fn main() -> io::Result<()> {
+/// let f = File::open("foo.txt")?;
+/// let f = BufReader::new(f);
+///
+/// for line in f.lines() {
+/// println!("{}", line.unwrap());
+/// }
+///
+/// Ok(())
+/// }
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+pub trait BufRead: Read {
+ /// Returns the contents of the internal buffer, filling it with more data
+ /// from the inner reader if it is empty.
+ ///
+ /// This function is a lower-level call. It needs to be paired with the
+ /// [`consume`] method to function properly. When calling this
+ /// method, none of the contents will be "read" in the sense that later
+ /// calling `read` may return the same contents. As such, [`consume`] must
+ /// be called with the number of bytes that are consumed from this buffer to
+ /// ensure that the bytes are never returned twice.
+ ///
+ /// [`consume`]: BufRead::consume
+ ///
+ /// An empty buffer returned indicates that the stream has reached EOF.
+ ///
+ /// # Errors
+ ///
+ /// This function will return an I/O error if the underlying reader was
+ /// read, but returned an error.
+ ///
+ /// # Examples
+ ///
+ /// A locked standard input implements `BufRead`:
+ ///
+ /// ```no_run
+ /// use std::io;
+ /// use std::io::prelude::*;
+ ///
+ /// let stdin = io::stdin();
+ /// let mut stdin = stdin.lock();
+ ///
+ /// let buffer = stdin.fill_buf().unwrap();
+ ///
+ /// // work with buffer
+ /// println!("{buffer:?}");
+ ///
+ /// // ensure the bytes we worked with aren't returned again later
+ /// let length = buffer.len();
+ /// stdin.consume(length);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn fill_buf(&mut self) -> Result<&[u8]>;
+
+ /// Tells this buffer that `amt` bytes have been consumed from the buffer,
+ /// so they should no longer be returned in calls to `read`.
+ ///
+ /// This function is a lower-level call. It needs to be paired with the
+ /// [`fill_buf`] method to function properly. This function does
+ /// not perform any I/O, it simply informs this object that some amount of
+ /// its buffer, returned from [`fill_buf`], has been consumed and should
+ /// no longer be returned. As such, this function may do odd things if
+ /// [`fill_buf`] isn't called before calling it.
+ ///
+ /// The `amt` must be `<=` the number of bytes in the buffer returned by
+ /// [`fill_buf`].
+ ///
+ /// # Examples
+ ///
+ /// Since `consume()` is meant to be used with [`fill_buf`],
+ /// that method's example includes an example of `consume()`.
+ ///
+ /// [`fill_buf`]: BufRead::fill_buf
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn consume(&mut self, amt: usize);
+
+ /// Check if the underlying `Read` has any data left to be read.
+ ///
+ /// This function may fill the buffer to check for data,
+ /// so this functions returns `Result<bool>`, not `bool`.
+ ///
+ /// Default implementation calls `fill_buf` and checks that
+ /// returned slice is empty (which means that there is no data left,
+ /// since EOF is reached).
+ ///
+ /// Examples
+ ///
+ /// ```
+ /// #![feature(buf_read_has_data_left)]
+ /// use std::io;
+ /// use std::io::prelude::*;
+ ///
+ /// let stdin = io::stdin();
+ /// let mut stdin = stdin.lock();
+ ///
+ /// while stdin.has_data_left().unwrap() {
+ /// let mut line = String::new();
+ /// stdin.read_line(&mut line).unwrap();
+ /// // work with line
+ /// println!("{line:?}");
+ /// }
+ /// ```
+ #[unstable(feature = "buf_read_has_data_left", reason = "recently added", issue = "86423")]
+ fn has_data_left(&mut self) -> Result<bool> {
+ self.fill_buf().map(|b| !b.is_empty())
+ }
+
+ /// Read all bytes into `buf` until the delimiter `byte` or EOF is reached.
+ ///
+ /// This function will read bytes from the underlying stream until the
+ /// delimiter or EOF is found. Once found, all bytes up to, and including,
+ /// the delimiter (if found) will be appended to `buf`.
+ ///
+ /// If successful, this function will return the total number of bytes read.
+ ///
+ /// This function is blocking and should be used carefully: it is possible for
+ /// an attacker to continuously send bytes without ever sending the delimiter
+ /// or EOF.
+ ///
+ /// # Errors
+ ///
+ /// This function will ignore all instances of [`ErrorKind::Interrupted`] and
+ /// will otherwise return any errors returned by [`fill_buf`].
+ ///
+ /// If an I/O error is encountered then all bytes read so far will be
+ /// present in `buf` and its length will have been adjusted appropriately.
+ ///
+ /// [`fill_buf`]: BufRead::fill_buf
+ ///
+ /// # Examples
+ ///
+ /// [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In
+ /// this example, we use [`Cursor`] to read all the bytes in a byte slice
+ /// in hyphen delimited segments:
+ ///
+ /// ```
+ /// use std::io::{self, BufRead};
+ ///
+ /// let mut cursor = io::Cursor::new(b"lorem-ipsum");
+ /// let mut buf = vec![];
+ ///
+ /// // cursor is at 'l'
+ /// let num_bytes = cursor.read_until(b'-', &mut buf)
+ /// .expect("reading from cursor won't fail");
+ /// assert_eq!(num_bytes, 6);
+ /// assert_eq!(buf, b"lorem-");
+ /// buf.clear();
+ ///
+ /// // cursor is at 'i'
+ /// let num_bytes = cursor.read_until(b'-', &mut buf)
+ /// .expect("reading from cursor won't fail");
+ /// assert_eq!(num_bytes, 5);
+ /// assert_eq!(buf, b"ipsum");
+ /// buf.clear();
+ ///
+ /// // cursor is at EOF
+ /// let num_bytes = cursor.read_until(b'-', &mut buf)
+ /// .expect("reading from cursor won't fail");
+ /// assert_eq!(num_bytes, 0);
+ /// assert_eq!(buf, b"");
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> Result<usize> {
+ read_until(self, byte, buf)
+ }
+
+ /// Read all bytes until a newline (the `0xA` byte) is reached, and append
+ /// them to the provided buffer. You do not need to clear the buffer before
+ /// appending.
+ ///
+ /// This function will read bytes from the underlying stream until the
+ /// newline delimiter (the `0xA` byte) or EOF is found. Once found, all bytes
+ /// up to, and including, the delimiter (if found) will be appended to
+ /// `buf`.
+ ///
+ /// If successful, this function will return the total number of bytes read.
+ ///
+ /// If this function returns [`Ok(0)`], the stream has reached EOF.
+ ///
+ /// This function is blocking and should be used carefully: it is possible for
+ /// an attacker to continuously send bytes without ever sending a newline
+ /// or EOF.
+ ///
+ /// [`Ok(0)`]: Ok
+ ///
+ /// # Errors
+ ///
+ /// This function has the same error semantics as [`read_until`] and will
+ /// also return an error if the read bytes are not valid UTF-8. If an I/O
+ /// error is encountered then `buf` may contain some bytes already read in
+ /// the event that all data read so far was valid UTF-8.
+ ///
+ /// [`read_until`]: BufRead::read_until
+ ///
+ /// # Examples
+ ///
+ /// [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In
+ /// this example, we use [`Cursor`] to read all the lines in a byte slice:
+ ///
+ /// ```
+ /// use std::io::{self, BufRead};
+ ///
+ /// let mut cursor = io::Cursor::new(b"foo\nbar");
+ /// let mut buf = String::new();
+ ///
+ /// // cursor is at 'f'
+ /// let num_bytes = cursor.read_line(&mut buf)
+ /// .expect("reading from cursor won't fail");
+ /// assert_eq!(num_bytes, 4);
+ /// assert_eq!(buf, "foo\n");
+ /// buf.clear();
+ ///
+ /// // cursor is at 'b'
+ /// let num_bytes = cursor.read_line(&mut buf)
+ /// .expect("reading from cursor won't fail");
+ /// assert_eq!(num_bytes, 3);
+ /// assert_eq!(buf, "bar");
+ /// buf.clear();
+ ///
+ /// // cursor is at EOF
+ /// let num_bytes = cursor.read_line(&mut buf)
+ /// .expect("reading from cursor won't fail");
+ /// assert_eq!(num_bytes, 0);
+ /// assert_eq!(buf, "");
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn read_line(&mut self, buf: &mut String) -> Result<usize> {
+ // Note that we are not calling the `.read_until` method here, but
+ // rather our hardcoded implementation. For more details as to why, see
+ // the comments in `read_to_end`.
+ unsafe { append_to_string(buf, |b| read_until(self, b'\n', b)) }
+ }
+
+ /// Returns an iterator over the contents of this reader split on the byte
+ /// `byte`.
+ ///
+ /// The iterator returned from this function will return instances of
+ /// <code>[io::Result]<[Vec]\<u8>></code>. Each vector returned will *not* have
+ /// the delimiter byte at the end.
+ ///
+ /// This function will yield errors whenever [`read_until`] would have
+ /// also yielded an error.
+ ///
+ /// [io::Result]: self::Result "io::Result"
+ /// [`read_until`]: BufRead::read_until
+ ///
+ /// # Examples
+ ///
+ /// [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In
+ /// this example, we use [`Cursor`] to iterate over all hyphen delimited
+ /// segments in a byte slice
+ ///
+ /// ```
+ /// use std::io::{self, BufRead};
+ ///
+ /// let cursor = io::Cursor::new(b"lorem-ipsum-dolor");
+ ///
+ /// let mut split_iter = cursor.split(b'-').map(|l| l.unwrap());
+ /// assert_eq!(split_iter.next(), Some(b"lorem".to_vec()));
+ /// assert_eq!(split_iter.next(), Some(b"ipsum".to_vec()));
+ /// assert_eq!(split_iter.next(), Some(b"dolor".to_vec()));
+ /// assert_eq!(split_iter.next(), None);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn split(self, byte: u8) -> Split<Self>
+ where
+ Self: Sized,
+ {
+ Split { buf: self, delim: byte }
+ }
+
+ /// Returns an iterator over the lines of this reader.
+ ///
+ /// The iterator returned from this function will yield instances of
+ /// <code>[io::Result]<[String]></code>. Each string returned will *not* have a newline
+ /// byte (the `0xA` byte) or `CRLF` (`0xD`, `0xA` bytes) at the end.
+ ///
+ /// [io::Result]: self::Result "io::Result"
+ ///
+ /// # Examples
+ ///
+ /// [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In
+ /// this example, we use [`Cursor`] to iterate over all the lines in a byte
+ /// slice.
+ ///
+ /// ```
+ /// use std::io::{self, BufRead};
+ ///
+ /// let cursor = io::Cursor::new(b"lorem\nipsum\r\ndolor");
+ ///
+ /// let mut lines_iter = cursor.lines().map(|l| l.unwrap());
+ /// assert_eq!(lines_iter.next(), Some(String::from("lorem")));
+ /// assert_eq!(lines_iter.next(), Some(String::from("ipsum")));
+ /// assert_eq!(lines_iter.next(), Some(String::from("dolor")));
+ /// assert_eq!(lines_iter.next(), None);
+ /// ```
+ ///
+ /// # Errors
+ ///
+ /// Each line of the iterator has the same error semantics as [`BufRead::read_line`].
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn lines(self) -> Lines<Self>
+ where
+ Self: Sized,
+ {
+ Lines { buf: self }
+ }
+}
+
+/// Adapter to chain together two readers.
+///
+/// This struct is generally created by calling [`chain`] on a reader.
+/// Please see the documentation of [`chain`] for more details.
+///
+/// [`chain`]: Read::chain
+#[stable(feature = "rust1", since = "1.0.0")]
+#[derive(Debug)]
+pub struct Chain<T, U> {
+ first: T,
+ second: U,
+ done_first: bool,
+}
+
+impl<T, U> Chain<T, U> {
+ /// Consumes the `Chain`, returning the wrapped readers.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io;
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// fn main() -> io::Result<()> {
+ /// let mut foo_file = File::open("foo.txt")?;
+ /// let mut bar_file = File::open("bar.txt")?;
+ ///
+ /// let chain = foo_file.chain(bar_file);
+ /// let (foo_file, bar_file) = chain.into_inner();
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "more_io_inner_methods", since = "1.20.0")]
+ pub fn into_inner(self) -> (T, U) {
+ (self.first, self.second)
+ }
+
+ /// Gets references to the underlying readers in this `Chain`.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io;
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// fn main() -> io::Result<()> {
+ /// let mut foo_file = File::open("foo.txt")?;
+ /// let mut bar_file = File::open("bar.txt")?;
+ ///
+ /// let chain = foo_file.chain(bar_file);
+ /// let (foo_file, bar_file) = chain.get_ref();
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "more_io_inner_methods", since = "1.20.0")]
+ pub fn get_ref(&self) -> (&T, &U) {
+ (&self.first, &self.second)
+ }
+
+ /// Gets mutable references to the underlying readers in this `Chain`.
+ ///
+ /// Care should be taken to avoid modifying the internal I/O state of the
+ /// underlying readers as doing so may corrupt the internal state of this
+ /// `Chain`.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io;
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// fn main() -> io::Result<()> {
+ /// let mut foo_file = File::open("foo.txt")?;
+ /// let mut bar_file = File::open("bar.txt")?;
+ ///
+ /// let mut chain = foo_file.chain(bar_file);
+ /// let (foo_file, bar_file) = chain.get_mut();
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "more_io_inner_methods", since = "1.20.0")]
+ pub fn get_mut(&mut self) -> (&mut T, &mut U) {
+ (&mut self.first, &mut self.second)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<T: Read, U: Read> Read for Chain<T, U> {
+ fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
+ if !self.done_first {
+ match self.first.read(buf)? {
+ 0 if !buf.is_empty() => self.done_first = true,
+ n => return Ok(n),
+ }
+ }
+ self.second.read(buf)
+ }
+
+ fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize> {
+ if !self.done_first {
+ match self.first.read_vectored(bufs)? {
+ 0 if bufs.iter().any(|b| !b.is_empty()) => self.done_first = true,
+ n => return Ok(n),
+ }
+ }
+ self.second.read_vectored(bufs)
+ }
+}
+
+#[stable(feature = "chain_bufread", since = "1.9.0")]
+impl<T: BufRead, U: BufRead> BufRead for Chain<T, U> {
+ fn fill_buf(&mut self) -> Result<&[u8]> {
+ if !self.done_first {
+ match self.first.fill_buf()? {
+ buf if buf.is_empty() => {
+ self.done_first = true;
+ }
+ buf => return Ok(buf),
+ }
+ }
+ self.second.fill_buf()
+ }
+
+ fn consume(&mut self, amt: usize) {
+ if !self.done_first { self.first.consume(amt) } else { self.second.consume(amt) }
+ }
+}
+
+impl<T, U> SizeHint for Chain<T, U> {
+ #[inline]
+ fn lower_bound(&self) -> usize {
+ SizeHint::lower_bound(&self.first) + SizeHint::lower_bound(&self.second)
+ }
+
+ #[inline]
+ fn upper_bound(&self) -> Option<usize> {
+ match (SizeHint::upper_bound(&self.first), SizeHint::upper_bound(&self.second)) {
+ (Some(first), Some(second)) => first.checked_add(second),
+ _ => None,
+ }
+ }
+}
+
+/// Reader adapter which limits the bytes read from an underlying reader.
+///
+/// This struct is generally created by calling [`take`] on a reader.
+/// Please see the documentation of [`take`] for more details.
+///
+/// [`take`]: Read::take
+#[stable(feature = "rust1", since = "1.0.0")]
+#[derive(Debug)]
+pub struct Take<T> {
+ inner: T,
+ limit: u64,
+}
+
+impl<T> Take<T> {
+ /// Returns the number of bytes that can be read before this instance will
+ /// return EOF.
+ ///
+ /// # Note
+ ///
+ /// This instance may reach `EOF` after reading fewer bytes than indicated by
+ /// this method if the underlying [`Read`] instance reaches EOF.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io;
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// fn main() -> io::Result<()> {
+ /// let f = File::open("foo.txt")?;
+ ///
+ /// // read at most five bytes
+ /// let handle = f.take(5);
+ ///
+ /// println!("limit: {}", handle.limit());
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn limit(&self) -> u64 {
+ self.limit
+ }
+
+ /// Sets the number of bytes that can be read before this instance will
+ /// return EOF. This is the same as constructing a new `Take` instance, so
+ /// the amount of bytes read and the previous limit value don't matter when
+ /// calling this method.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io;
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// fn main() -> io::Result<()> {
+ /// let f = File::open("foo.txt")?;
+ ///
+ /// // read at most five bytes
+ /// let mut handle = f.take(5);
+ /// handle.set_limit(10);
+ ///
+ /// assert_eq!(handle.limit(), 10);
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "take_set_limit", since = "1.27.0")]
+ pub fn set_limit(&mut self, limit: u64) {
+ self.limit = limit;
+ }
+
+ /// Consumes the `Take`, returning the wrapped reader.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io;
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// fn main() -> io::Result<()> {
+ /// let mut file = File::open("foo.txt")?;
+ ///
+ /// let mut buffer = [0; 5];
+ /// let mut handle = file.take(5);
+ /// handle.read(&mut buffer)?;
+ ///
+ /// let file = handle.into_inner();
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "io_take_into_inner", since = "1.15.0")]
+ pub fn into_inner(self) -> T {
+ self.inner
+ }
+
+ /// Gets a reference to the underlying reader.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io;
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// fn main() -> io::Result<()> {
+ /// let mut file = File::open("foo.txt")?;
+ ///
+ /// let mut buffer = [0; 5];
+ /// let mut handle = file.take(5);
+ /// handle.read(&mut buffer)?;
+ ///
+ /// let file = handle.get_ref();
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "more_io_inner_methods", since = "1.20.0")]
+ pub fn get_ref(&self) -> &T {
+ &self.inner
+ }
+
+ /// Gets a mutable reference to the underlying reader.
+ ///
+ /// Care should be taken to avoid modifying the internal I/O state of the
+ /// underlying reader as doing so may corrupt the internal limit of this
+ /// `Take`.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io;
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// fn main() -> io::Result<()> {
+ /// let mut file = File::open("foo.txt")?;
+ ///
+ /// let mut buffer = [0; 5];
+ /// let mut handle = file.take(5);
+ /// handle.read(&mut buffer)?;
+ ///
+ /// let file = handle.get_mut();
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "more_io_inner_methods", since = "1.20.0")]
+ pub fn get_mut(&mut self) -> &mut T {
+ &mut self.inner
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<T: Read> Read for Take<T> {
+ fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
+ // Don't call into inner reader at all at EOF because it may still block
+ if self.limit == 0 {
+ return Ok(0);
+ }
+
+ let max = cmp::min(buf.len() as u64, self.limit) as usize;
+ let n = self.inner.read(&mut buf[..max])?;
+ assert!(n as u64 <= self.limit, "number of read bytes exceeds limit");
+ self.limit -= n as u64;
+ Ok(n)
+ }
+
+ fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> Result<()> {
+ // Don't call into inner reader at all at EOF because it may still block
+ if self.limit == 0 {
+ return Ok(());
+ }
+
+ let prev_filled = buf.filled_len();
+
+ if self.limit <= buf.remaining() as u64 {
+ // if we just use an as cast to convert, limit may wrap around on a 32 bit target
+ let limit = cmp::min(self.limit, usize::MAX as u64) as usize;
+
+ let extra_init = cmp::min(limit as usize, buf.initialized_len() - buf.filled_len());
+
+ // SAFETY: no uninit data is written to ibuf
+ let ibuf = unsafe { &mut buf.unfilled_mut()[..limit] };
+
+ let mut sliced_buf = ReadBuf::uninit(ibuf);
+
+ // SAFETY: extra_init bytes of ibuf are known to be initialized
+ unsafe {
+ sliced_buf.assume_init(extra_init);
+ }
+
+ self.inner.read_buf(&mut sliced_buf)?;
+
+ let new_init = sliced_buf.initialized_len();
+ let filled = sliced_buf.filled_len();
+
+ // sliced_buf / ibuf must drop here
+
+ // SAFETY: new_init bytes of buf's unfilled buffer have been initialized
+ unsafe {
+ buf.assume_init(new_init);
+ }
+
+ buf.add_filled(filled);
+
+ self.limit -= filled as u64;
+ } else {
+ self.inner.read_buf(buf)?;
+
+ //inner may unfill
+ self.limit -= buf.filled_len().saturating_sub(prev_filled) as u64;
+ }
+
+ Ok(())
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<T: BufRead> BufRead for Take<T> {
+ fn fill_buf(&mut self) -> Result<&[u8]> {
+ // Don't call into inner reader at all at EOF because it may still block
+ if self.limit == 0 {
+ return Ok(&[]);
+ }
+
+ let buf = self.inner.fill_buf()?;
+ let cap = cmp::min(buf.len() as u64, self.limit) as usize;
+ Ok(&buf[..cap])
+ }
+
+ fn consume(&mut self, amt: usize) {
+ // Don't let callers reset the limit by passing an overlarge value
+ let amt = cmp::min(amt as u64, self.limit) as usize;
+ self.limit -= amt as u64;
+ self.inner.consume(amt);
+ }
+}
+
+impl<T> SizeHint for Take<T> {
+ #[inline]
+ fn lower_bound(&self) -> usize {
+ cmp::min(SizeHint::lower_bound(&self.inner) as u64, self.limit) as usize
+ }
+
+ #[inline]
+ fn upper_bound(&self) -> Option<usize> {
+ match SizeHint::upper_bound(&self.inner) {
+ Some(upper_bound) => Some(cmp::min(upper_bound as u64, self.limit) as usize),
+ None => self.limit.try_into().ok(),
+ }
+ }
+}
+
+/// An iterator over `u8` values of a reader.
+///
+/// This struct is generally created by calling [`bytes`] on a reader.
+/// Please see the documentation of [`bytes`] for more details.
+///
+/// [`bytes`]: Read::bytes
+#[stable(feature = "rust1", since = "1.0.0")]
+#[derive(Debug)]
+pub struct Bytes<R> {
+ inner: R,
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<R: Read> Iterator for Bytes<R> {
+ type Item = Result<u8>;
+
+ fn next(&mut self) -> Option<Result<u8>> {
+ let mut byte = 0;
+ loop {
+ return match self.inner.read(slice::from_mut(&mut byte)) {
+ Ok(0) => None,
+ Ok(..) => Some(Ok(byte)),
+ Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
+ Err(e) => Some(Err(e)),
+ };
+ }
+ }
+
+ fn size_hint(&self) -> (usize, Option<usize>) {
+ SizeHint::size_hint(&self.inner)
+ }
+}
+
+trait SizeHint {
+ fn lower_bound(&self) -> usize;
+
+ fn upper_bound(&self) -> Option<usize>;
+
+ fn size_hint(&self) -> (usize, Option<usize>) {
+ (self.lower_bound(), self.upper_bound())
+ }
+}
+
+impl<T> SizeHint for T {
+ #[inline]
+ default fn lower_bound(&self) -> usize {
+ 0
+ }
+
+ #[inline]
+ default fn upper_bound(&self) -> Option<usize> {
+ None
+ }
+}
+
+impl<T> SizeHint for &mut T {
+ #[inline]
+ fn lower_bound(&self) -> usize {
+ SizeHint::lower_bound(*self)
+ }
+
+ #[inline]
+ fn upper_bound(&self) -> Option<usize> {
+ SizeHint::upper_bound(*self)
+ }
+}
+
+impl<T> SizeHint for Box<T> {
+ #[inline]
+ fn lower_bound(&self) -> usize {
+ SizeHint::lower_bound(&**self)
+ }
+
+ #[inline]
+ fn upper_bound(&self) -> Option<usize> {
+ SizeHint::upper_bound(&**self)
+ }
+}
+
+impl SizeHint for &[u8] {
+ #[inline]
+ fn lower_bound(&self) -> usize {
+ self.len()
+ }
+
+ #[inline]
+ fn upper_bound(&self) -> Option<usize> {
+ Some(self.len())
+ }
+}
+
+/// An iterator over the contents of an instance of `BufRead` split on a
+/// particular byte.
+///
+/// This struct is generally created by calling [`split`] on a `BufRead`.
+/// Please see the documentation of [`split`] for more details.
+///
+/// [`split`]: BufRead::split
+#[stable(feature = "rust1", since = "1.0.0")]
+#[derive(Debug)]
+pub struct Split<B> {
+ buf: B,
+ delim: u8,
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<B: BufRead> Iterator for Split<B> {
+ type Item = Result<Vec<u8>>;
+
+ fn next(&mut self) -> Option<Result<Vec<u8>>> {
+ let mut buf = Vec::new();
+ match self.buf.read_until(self.delim, &mut buf) {
+ Ok(0) => None,
+ Ok(_n) => {
+ if buf[buf.len() - 1] == self.delim {
+ buf.pop();
+ }
+ Some(Ok(buf))
+ }
+ Err(e) => Some(Err(e)),
+ }
+ }
+}
+
+/// An iterator over the lines of an instance of `BufRead`.
+///
+/// This struct is generally created by calling [`lines`] on a `BufRead`.
+/// Please see the documentation of [`lines`] for more details.
+///
+/// [`lines`]: BufRead::lines
+#[stable(feature = "rust1", since = "1.0.0")]
+#[derive(Debug)]
+pub struct Lines<B> {
+ buf: B,
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<B: BufRead> Iterator for Lines<B> {
+ type Item = Result<String>;
+
+ fn next(&mut self) -> Option<Result<String>> {
+ let mut buf = String::new();
+ match self.buf.read_line(&mut buf) {
+ Ok(0) => None,
+ Ok(_n) => {
+ if buf.ends_with('\n') {
+ buf.pop();
+ if buf.ends_with('\r') {
+ buf.pop();
+ }
+ }
+ Some(Ok(buf))
+ }
+ Err(e) => Some(Err(e)),
+ }
+ }
+}
diff --git a/library/std/src/io/prelude.rs b/library/std/src/io/prelude.rs
new file mode 100644
index 000000000..d80643101
--- /dev/null
+++ b/library/std/src/io/prelude.rs
@@ -0,0 +1,14 @@
+//! The I/O Prelude.
+//!
+//! The purpose of this module is to alleviate imports of many common I/O traits
+//! by adding a glob import to the top of I/O heavy modules:
+//!
+//! ```
+//! # #![allow(unused_imports)]
+//! use std::io::prelude::*;
+//! ```
+
+#![stable(feature = "rust1", since = "1.0.0")]
+
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use super::{BufRead, Read, Seek, Write};
diff --git a/library/std/src/io/readbuf.rs b/library/std/src/io/readbuf.rs
new file mode 100644
index 000000000..78d1113f8
--- /dev/null
+++ b/library/std/src/io/readbuf.rs
@@ -0,0 +1,249 @@
+#![unstable(feature = "read_buf", issue = "78485")]
+
+#[cfg(test)]
+mod tests;
+
+use crate::cmp;
+use crate::fmt::{self, Debug, Formatter};
+use crate::mem::MaybeUninit;
+
+/// A wrapper around a byte buffer that is incrementally filled and initialized.
+///
+/// This type is a sort of "double cursor". It tracks three regions in the buffer: a region at the beginning of the
+/// buffer that has been logically filled with data, a region that has been initialized at some point but not yet
+/// logically filled, and a region at the end that is fully uninitialized. The filled region is guaranteed to be a
+/// subset of the initialized region.
+///
+/// In summary, the contents of the buffer can be visualized as:
+/// ```not_rust
+/// [ capacity ]
+/// [ filled | unfilled ]
+/// [ initialized | uninitialized ]
+/// ```
+pub struct ReadBuf<'a> {
+ buf: &'a mut [MaybeUninit<u8>],
+ filled: usize,
+ initialized: usize,
+}
+
+impl Debug for ReadBuf<'_> {
+ fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
+ f.debug_struct("ReadBuf")
+ .field("init", &self.initialized())
+ .field("filled", &self.filled)
+ .field("capacity", &self.capacity())
+ .finish()
+ }
+}
+
+impl<'a> ReadBuf<'a> {
+ /// Creates a new `ReadBuf` from a fully initialized buffer.
+ #[inline]
+ pub fn new(buf: &'a mut [u8]) -> ReadBuf<'a> {
+ let len = buf.len();
+
+ ReadBuf {
+ //SAFETY: initialized data never becoming uninitialized is an invariant of ReadBuf
+ buf: unsafe { (buf as *mut [u8]).as_uninit_slice_mut().unwrap() },
+ filled: 0,
+ initialized: len,
+ }
+ }
+
+ /// Creates a new `ReadBuf` from a fully uninitialized buffer.
+ ///
+ /// Use `assume_init` if part of the buffer is known to be already initialized.
+ #[inline]
+ pub fn uninit(buf: &'a mut [MaybeUninit<u8>]) -> ReadBuf<'a> {
+ ReadBuf { buf, filled: 0, initialized: 0 }
+ }
+
+ /// Returns the total capacity of the buffer.
+ #[inline]
+ pub fn capacity(&self) -> usize {
+ self.buf.len()
+ }
+
+ /// Returns a shared reference to the filled portion of the buffer.
+ #[inline]
+ pub fn filled(&self) -> &[u8] {
+ //SAFETY: We only slice the filled part of the buffer, which is always valid
+ unsafe { MaybeUninit::slice_assume_init_ref(&self.buf[0..self.filled]) }
+ }
+
+ /// Returns a mutable reference to the filled portion of the buffer.
+ #[inline]
+ pub fn filled_mut(&mut self) -> &mut [u8] {
+ //SAFETY: We only slice the filled part of the buffer, which is always valid
+ unsafe { MaybeUninit::slice_assume_init_mut(&mut self.buf[0..self.filled]) }
+ }
+
+ /// Returns a shared reference to the initialized portion of the buffer.
+ ///
+ /// This includes the filled portion.
+ #[inline]
+ pub fn initialized(&self) -> &[u8] {
+ //SAFETY: We only slice the initialized part of the buffer, which is always valid
+ unsafe { MaybeUninit::slice_assume_init_ref(&self.buf[0..self.initialized]) }
+ }
+
+ /// Returns a mutable reference to the initialized portion of the buffer.
+ ///
+ /// This includes the filled portion.
+ #[inline]
+ pub fn initialized_mut(&mut self) -> &mut [u8] {
+ //SAFETY: We only slice the initialized part of the buffer, which is always valid
+ unsafe { MaybeUninit::slice_assume_init_mut(&mut self.buf[0..self.initialized]) }
+ }
+
+ /// Returns a mutable reference to the unfilled part of the buffer without ensuring that it has been fully
+ /// initialized.
+ ///
+ /// # Safety
+ ///
+ /// The caller must not de-initialize portions of the buffer that have already been initialized.
+ #[inline]
+ pub unsafe fn unfilled_mut(&mut self) -> &mut [MaybeUninit<u8>] {
+ &mut self.buf[self.filled..]
+ }
+
+ /// Returns a mutable reference to the uninitialized part of the buffer.
+ ///
+ /// It is safe to uninitialize any of these bytes.
+ #[inline]
+ pub fn uninitialized_mut(&mut self) -> &mut [MaybeUninit<u8>] {
+ &mut self.buf[self.initialized..]
+ }
+
+ /// Returns a mutable reference to the unfilled part of the buffer, ensuring it is fully initialized.
+ ///
+ /// Since `ReadBuf` tracks the region of the buffer that has been initialized, this is effectively "free" after
+ /// the first use.
+ #[inline]
+ pub fn initialize_unfilled(&mut self) -> &mut [u8] {
+ // should optimize out the assertion
+ self.initialize_unfilled_to(self.remaining())
+ }
+
+ /// Returns a mutable reference to the first `n` bytes of the unfilled part of the buffer, ensuring it is
+ /// fully initialized.
+ ///
+ /// # Panics
+ ///
+ /// Panics if `self.remaining()` is less than `n`.
+ #[inline]
+ pub fn initialize_unfilled_to(&mut self, n: usize) -> &mut [u8] {
+ assert!(self.remaining() >= n);
+
+ let extra_init = self.initialized - self.filled;
+ // If we don't have enough initialized, do zeroing
+ if n > extra_init {
+ let uninit = n - extra_init;
+ let unfilled = &mut self.uninitialized_mut()[0..uninit];
+
+ for byte in unfilled.iter_mut() {
+ byte.write(0);
+ }
+
+ // SAFETY: we just initialized uninit bytes, and the previous bytes were already init
+ unsafe {
+ self.assume_init(n);
+ }
+ }
+
+ let filled = self.filled;
+
+ &mut self.initialized_mut()[filled..filled + n]
+ }
+
+ /// Returns the number of bytes at the end of the slice that have not yet been filled.
+ #[inline]
+ pub fn remaining(&self) -> usize {
+ self.capacity() - self.filled
+ }
+
+ /// Clears the buffer, resetting the filled region to empty.
+ ///
+ /// The number of initialized bytes is not changed, and the contents of the buffer are not modified.
+ #[inline]
+ pub fn clear(&mut self) -> &mut Self {
+ self.set_filled(0) // The assertion in `set_filled` is optimized out
+ }
+
+ /// Increases the size of the filled region of the buffer.
+ ///
+ /// The number of initialized bytes is not changed.
+ ///
+ /// # Panics
+ ///
+ /// Panics if the filled region of the buffer would become larger than the initialized region.
+ #[inline]
+ pub fn add_filled(&mut self, n: usize) -> &mut Self {
+ self.set_filled(self.filled + n)
+ }
+
+ /// Sets the size of the filled region of the buffer.
+ ///
+ /// The number of initialized bytes is not changed.
+ ///
+ /// Note that this can be used to *shrink* the filled region of the buffer in addition to growing it (for
+ /// example, by a `Read` implementation that compresses data in-place).
+ ///
+ /// # Panics
+ ///
+ /// Panics if the filled region of the buffer would become larger than the initialized region.
+ #[inline]
+ pub fn set_filled(&mut self, n: usize) -> &mut Self {
+ assert!(n <= self.initialized);
+
+ self.filled = n;
+ self
+ }
+
+ /// Asserts that the first `n` unfilled bytes of the buffer are initialized.
+ ///
+ /// `ReadBuf` assumes that bytes are never de-initialized, so this method does nothing when called with fewer
+ /// bytes than are already known to be initialized.
+ ///
+ /// # Safety
+ ///
+ /// The caller must ensure that the first `n` unfilled bytes of the buffer have already been initialized.
+ #[inline]
+ pub unsafe fn assume_init(&mut self, n: usize) -> &mut Self {
+ self.initialized = cmp::max(self.initialized, self.filled + n);
+ self
+ }
+
+ /// Appends data to the buffer, advancing the written position and possibly also the initialized position.
+ ///
+ /// # Panics
+ ///
+ /// Panics if `self.remaining()` is less than `buf.len()`.
+ #[inline]
+ pub fn append(&mut self, buf: &[u8]) {
+ assert!(self.remaining() >= buf.len());
+
+ // SAFETY: we do not de-initialize any of the elements of the slice
+ unsafe {
+ MaybeUninit::write_slice(&mut self.unfilled_mut()[..buf.len()], buf);
+ }
+
+ // SAFETY: We just added the entire contents of buf to the filled section.
+ unsafe {
+ self.assume_init(buf.len());
+ }
+ self.add_filled(buf.len());
+ }
+
+ /// Returns the amount of bytes that have been filled.
+ #[inline]
+ pub fn filled_len(&self) -> usize {
+ self.filled
+ }
+
+ /// Returns the amount of bytes that have been initialized.
+ #[inline]
+ pub fn initialized_len(&self) -> usize {
+ self.initialized
+ }
+}
diff --git a/library/std/src/io/readbuf/tests.rs b/library/std/src/io/readbuf/tests.rs
new file mode 100644
index 000000000..3b7a5a56d
--- /dev/null
+++ b/library/std/src/io/readbuf/tests.rs
@@ -0,0 +1,181 @@
+use super::ReadBuf;
+use crate::mem::MaybeUninit;
+
+/// Test that ReadBuf has the correct numbers when created with new
+#[test]
+fn new() {
+ let mut buf = [0; 16];
+ let rbuf = ReadBuf::new(&mut buf);
+
+ assert_eq!(rbuf.filled_len(), 0);
+ assert_eq!(rbuf.initialized_len(), 16);
+ assert_eq!(rbuf.capacity(), 16);
+ assert_eq!(rbuf.remaining(), 16);
+}
+
+/// Test that ReadBuf has the correct numbers when created with uninit
+#[test]
+fn uninit() {
+ let mut buf = [MaybeUninit::uninit(); 16];
+ let rbuf = ReadBuf::uninit(&mut buf);
+
+ assert_eq!(rbuf.filled_len(), 0);
+ assert_eq!(rbuf.initialized_len(), 0);
+ assert_eq!(rbuf.capacity(), 16);
+ assert_eq!(rbuf.remaining(), 16);
+}
+
+#[test]
+fn initialize_unfilled() {
+ let mut buf = [MaybeUninit::uninit(); 16];
+ let mut rbuf = ReadBuf::uninit(&mut buf);
+
+ rbuf.initialize_unfilled();
+
+ assert_eq!(rbuf.initialized_len(), 16);
+}
+
+#[test]
+fn initialize_unfilled_to() {
+ let mut buf = [MaybeUninit::uninit(); 16];
+ let mut rbuf = ReadBuf::uninit(&mut buf);
+
+ rbuf.initialize_unfilled_to(8);
+
+ assert_eq!(rbuf.initialized_len(), 8);
+
+ rbuf.initialize_unfilled_to(4);
+
+ assert_eq!(rbuf.initialized_len(), 8);
+
+ rbuf.set_filled(8);
+
+ rbuf.initialize_unfilled_to(6);
+
+ assert_eq!(rbuf.initialized_len(), 14);
+
+ rbuf.initialize_unfilled_to(8);
+
+ assert_eq!(rbuf.initialized_len(), 16);
+}
+
+#[test]
+fn add_filled() {
+ let mut buf = [0; 16];
+ let mut rbuf = ReadBuf::new(&mut buf);
+
+ rbuf.add_filled(1);
+
+ assert_eq!(rbuf.filled_len(), 1);
+ assert_eq!(rbuf.remaining(), 15);
+}
+
+#[test]
+#[should_panic]
+fn add_filled_panic() {
+ let mut buf = [MaybeUninit::uninit(); 16];
+ let mut rbuf = ReadBuf::uninit(&mut buf);
+
+ rbuf.add_filled(1);
+}
+
+#[test]
+fn set_filled() {
+ let mut buf = [0; 16];
+ let mut rbuf = ReadBuf::new(&mut buf);
+
+ rbuf.set_filled(16);
+
+ assert_eq!(rbuf.filled_len(), 16);
+ assert_eq!(rbuf.remaining(), 0);
+
+ rbuf.set_filled(6);
+
+ assert_eq!(rbuf.filled_len(), 6);
+ assert_eq!(rbuf.remaining(), 10);
+}
+
+#[test]
+#[should_panic]
+fn set_filled_panic() {
+ let mut buf = [MaybeUninit::uninit(); 16];
+ let mut rbuf = ReadBuf::uninit(&mut buf);
+
+ rbuf.set_filled(16);
+}
+
+#[test]
+fn clear() {
+ let mut buf = [255; 16];
+ let mut rbuf = ReadBuf::new(&mut buf);
+
+ rbuf.set_filled(16);
+
+ assert_eq!(rbuf.filled_len(), 16);
+ assert_eq!(rbuf.remaining(), 0);
+
+ rbuf.clear();
+
+ assert_eq!(rbuf.filled_len(), 0);
+ assert_eq!(rbuf.remaining(), 16);
+
+ assert_eq!(rbuf.initialized(), [255; 16]);
+}
+
+#[test]
+fn assume_init() {
+ let mut buf = [MaybeUninit::uninit(); 16];
+ let mut rbuf = ReadBuf::uninit(&mut buf);
+
+ unsafe {
+ rbuf.assume_init(8);
+ }
+
+ assert_eq!(rbuf.initialized_len(), 8);
+
+ rbuf.add_filled(4);
+
+ unsafe {
+ rbuf.assume_init(2);
+ }
+
+ assert_eq!(rbuf.initialized_len(), 8);
+
+ unsafe {
+ rbuf.assume_init(8);
+ }
+
+ assert_eq!(rbuf.initialized_len(), 12);
+}
+
+#[test]
+fn append() {
+ let mut buf = [MaybeUninit::new(255); 16];
+ let mut rbuf = ReadBuf::uninit(&mut buf);
+
+ rbuf.append(&[0; 8]);
+
+ assert_eq!(rbuf.initialized_len(), 8);
+ assert_eq!(rbuf.filled_len(), 8);
+ assert_eq!(rbuf.filled(), [0; 8]);
+
+ rbuf.clear();
+
+ rbuf.append(&[1; 16]);
+
+ assert_eq!(rbuf.initialized_len(), 16);
+ assert_eq!(rbuf.filled_len(), 16);
+ assert_eq!(rbuf.filled(), [1; 16]);
+}
+
+#[test]
+fn filled_mut() {
+ let mut buf = [0; 16];
+ let mut rbuf = ReadBuf::new(&mut buf);
+
+ rbuf.add_filled(8);
+
+ let filled = rbuf.filled().to_vec();
+
+ assert_eq!(&*filled, &*rbuf.filled_mut());
+}
diff --git a/library/std/src/io/stdio.rs b/library/std/src/io/stdio.rs
new file mode 100644
index 000000000..4d3736f79
--- /dev/null
+++ b/library/std/src/io/stdio.rs
@@ -0,0 +1,1042 @@
+#![cfg_attr(test, allow(unused))]
+
+#[cfg(test)]
+mod tests;
+
+use crate::io::prelude::*;
+
+use crate::cell::{Cell, RefCell};
+use crate::fmt;
+use crate::io::{self, BufReader, IoSlice, IoSliceMut, LineWriter, Lines};
+use crate::pin::Pin;
+use crate::sync::atomic::{AtomicBool, Ordering};
+use crate::sync::{Arc, Mutex, MutexGuard, OnceLock};
+use crate::sys::stdio;
+use crate::sys_common::remutex::{ReentrantMutex, ReentrantMutexGuard};
+
+type LocalStream = Arc<Mutex<Vec<u8>>>;
+
+thread_local! {
+ /// Used by the test crate to capture the output of the print macros and panics.
+ static OUTPUT_CAPTURE: Cell<Option<LocalStream>> = {
+ Cell::new(None)
+ }
+}
+
+/// Flag to indicate OUTPUT_CAPTURE is used.
+///
+/// If it is None and was never set on any thread, this flag is set to false,
+/// and OUTPUT_CAPTURE can be safely ignored on all threads, saving some time
+/// and memory registering an unused thread local.
+///
+/// Note about memory ordering: This contains information about whether a
+/// thread local variable might be in use. Although this is a global flag, the
+/// memory ordering between threads does not matter: we only want this flag to
+/// have a consistent order between set_output_capture and print_to *within
+/// the same thread*. Within the same thread, things always have a perfectly
+/// consistent order. So Ordering::Relaxed is fine.
+static OUTPUT_CAPTURE_USED: AtomicBool = AtomicBool::new(false);
+
+/// A handle to a raw instance of the standard input stream of this process.
+///
+/// This handle is not synchronized or buffered in any fashion. Constructed via
+/// the `std::io::stdio::stdin_raw` function.
+struct StdinRaw(stdio::Stdin);
+
+/// A handle to a raw instance of the standard output stream of this process.
+///
+/// This handle is not synchronized or buffered in any fashion. Constructed via
+/// the `std::io::stdio::stdout_raw` function.
+struct StdoutRaw(stdio::Stdout);
+
+/// A handle to a raw instance of the standard output stream of this process.
+///
+/// This handle is not synchronized or buffered in any fashion. Constructed via
+/// the `std::io::stdio::stderr_raw` function.
+struct StderrRaw(stdio::Stderr);
+
+/// Constructs a new raw handle to the standard input of this process.
+///
+/// The returned handle does not interact with any other handles created nor
+/// handles returned by `std::io::stdin`. Data buffered by the `std::io::stdin`
+/// handles is **not** available to raw handles returned from this function.
+///
+/// The returned handle has no external synchronization or buffering.
+#[unstable(feature = "libstd_sys_internals", issue = "none")]
+const fn stdin_raw() -> StdinRaw {
+ StdinRaw(stdio::Stdin::new())
+}
+
+/// Constructs a new raw handle to the standard output stream of this process.
+///
+/// The returned handle does not interact with any other handles created nor
+/// handles returned by `std::io::stdout`. Note that data is buffered by the
+/// `std::io::stdout` handles so writes which happen via this raw handle may
+/// appear before previous writes.
+///
+/// The returned handle has no external synchronization or buffering layered on
+/// top.
+#[unstable(feature = "libstd_sys_internals", issue = "none")]
+const fn stdout_raw() -> StdoutRaw {
+ StdoutRaw(stdio::Stdout::new())
+}
+
+/// Constructs a new raw handle to the standard error stream of this process.
+///
+/// The returned handle does not interact with any other handles created nor
+/// handles returned by `std::io::stderr`.
+///
+/// The returned handle has no external synchronization or buffering layered on
+/// top.
+#[unstable(feature = "libstd_sys_internals", issue = "none")]
+const fn stderr_raw() -> StderrRaw {
+ StderrRaw(stdio::Stderr::new())
+}
+
+impl Read for StdinRaw {
+ fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+ handle_ebadf(self.0.read(buf), 0)
+ }
+
+ fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
+ handle_ebadf(self.0.read_vectored(bufs), 0)
+ }
+
+ #[inline]
+ fn is_read_vectored(&self) -> bool {
+ self.0.is_read_vectored()
+ }
+
+ fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
+ handle_ebadf(self.0.read_to_end(buf), 0)
+ }
+
+ fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
+ handle_ebadf(self.0.read_to_string(buf), 0)
+ }
+}
+
+impl Write for StdoutRaw {
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ handle_ebadf(self.0.write(buf), buf.len())
+ }
+
+ fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+ let total = bufs.iter().map(|b| b.len()).sum();
+ handle_ebadf(self.0.write_vectored(bufs), total)
+ }
+
+ #[inline]
+ fn is_write_vectored(&self) -> bool {
+ self.0.is_write_vectored()
+ }
+
+ fn flush(&mut self) -> io::Result<()> {
+ handle_ebadf(self.0.flush(), ())
+ }
+
+ fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
+ handle_ebadf(self.0.write_all(buf), ())
+ }
+
+ fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
+ handle_ebadf(self.0.write_all_vectored(bufs), ())
+ }
+
+ fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
+ handle_ebadf(self.0.write_fmt(fmt), ())
+ }
+}
+
+impl Write for StderrRaw {
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ handle_ebadf(self.0.write(buf), buf.len())
+ }
+
+ fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+ let total = bufs.iter().map(|b| b.len()).sum();
+ handle_ebadf(self.0.write_vectored(bufs), total)
+ }
+
+ #[inline]
+ fn is_write_vectored(&self) -> bool {
+ self.0.is_write_vectored()
+ }
+
+ fn flush(&mut self) -> io::Result<()> {
+ handle_ebadf(self.0.flush(), ())
+ }
+
+ fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
+ handle_ebadf(self.0.write_all(buf), ())
+ }
+
+ fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
+ handle_ebadf(self.0.write_all_vectored(bufs), ())
+ }
+
+ fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
+ handle_ebadf(self.0.write_fmt(fmt), ())
+ }
+}
+
+fn handle_ebadf<T>(r: io::Result<T>, default: T) -> io::Result<T> {
+ match r {
+ Err(ref e) if stdio::is_ebadf(e) => Ok(default),
+ r => r,
+ }
+}
+
+/// A handle to the standard input stream of a process.
+///
+/// Each handle is a shared reference to a global buffer of input data to this
+/// process. A handle can be `lock`'d to gain full access to [`BufRead`] methods
+/// (e.g., `.lines()`). Reads to this handle are otherwise locked with respect
+/// to other reads.
+///
+/// This handle implements the `Read` trait, but beware that concurrent reads
+/// of `Stdin` must be executed with care.
+///
+/// Created by the [`io::stdin`] method.
+///
+/// [`io::stdin`]: stdin
+///
+/// ### Note: Windows Portability Considerations
+///
+/// When operating in a console, the Windows implementation of this stream does not support
+/// non-UTF-8 byte sequences. Attempting to read bytes that are not valid UTF-8 will return
+/// an error.
+///
+/// In a process with a detached console, such as one using
+/// `#![windows_subsystem = "windows"]`, or in a child process spawned from such a process,
+/// the contained handle will be null. In such cases, the standard library's `Read` and
+/// `Write` will do nothing and silently succeed. All other I/O operations, via the
+/// standard library or via raw Windows API calls, will fail.
+///
+/// # Examples
+///
+/// ```no_run
+/// use std::io;
+///
+/// fn main() -> io::Result<()> {
+/// let mut buffer = String::new();
+/// let stdin = io::stdin(); // We get `Stdin` here.
+/// stdin.read_line(&mut buffer)?;
+/// Ok(())
+/// }
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct Stdin {
+ inner: &'static Mutex<BufReader<StdinRaw>>,
+}
+
+/// A locked reference to the [`Stdin`] handle.
+///
+/// This handle implements both the [`Read`] and [`BufRead`] traits, and
+/// is constructed via the [`Stdin::lock`] method.
+///
+/// ### Note: Windows Portability Considerations
+///
+/// When operating in a console, the Windows implementation of this stream does not support
+/// non-UTF-8 byte sequences. Attempting to read bytes that are not valid UTF-8 will return
+/// an error.
+///
+/// In a process with a detached console, such as one using
+/// `#![windows_subsystem = "windows"]`, or in a child process spawned from such a process,
+/// the contained handle will be null. In such cases, the standard library's `Read` and
+/// `Write` will do nothing and silently succeed. All other I/O operations, via the
+/// standard library or via raw Windows API calls, will fail.
+///
+/// # Examples
+///
+/// ```no_run
+/// use std::io::{self, BufRead};
+///
+/// fn main() -> io::Result<()> {
+/// let mut buffer = String::new();
+/// let stdin = io::stdin(); // We get `Stdin` here.
+/// {
+/// let mut handle = stdin.lock(); // We get `StdinLock` here.
+/// handle.read_line(&mut buffer)?;
+/// } // `StdinLock` is dropped here.
+/// Ok(())
+/// }
+/// ```
+#[must_use = "if unused stdin will immediately unlock"]
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct StdinLock<'a> {
+ inner: MutexGuard<'a, BufReader<StdinRaw>>,
+}
+
+/// Constructs a new handle to the standard input of the current process.
+///
+/// Each handle returned is a reference to a shared global buffer whose access
+/// is synchronized via a mutex. If you need more explicit control over
+/// locking, see the [`Stdin::lock`] method.
+///
+/// ### Note: Windows Portability Considerations
+///
+/// When operating in a console, the Windows implementation of this stream does not support
+/// non-UTF-8 byte sequences. Attempting to read bytes that are not valid UTF-8 will return
+/// an error.
+///
+/// In a process with a detached console, such as one using
+/// `#![windows_subsystem = "windows"]`, or in a child process spawned from such a process,
+/// the contained handle will be null. In such cases, the standard library's `Read` and
+/// `Write` will do nothing and silently succeed. All other I/O operations, via the
+/// standard library or via raw Windows API calls, will fail.
+///
+/// # Examples
+///
+/// Using implicit synchronization:
+///
+/// ```no_run
+/// use std::io;
+///
+/// fn main() -> io::Result<()> {
+/// let mut buffer = String::new();
+/// io::stdin().read_line(&mut buffer)?;
+/// Ok(())
+/// }
+/// ```
+///
+/// Using explicit synchronization:
+///
+/// ```no_run
+/// use std::io::{self, BufRead};
+///
+/// fn main() -> io::Result<()> {
+/// let mut buffer = String::new();
+/// let stdin = io::stdin();
+/// let mut handle = stdin.lock();
+///
+/// handle.read_line(&mut buffer)?;
+/// Ok(())
+/// }
+/// ```
+#[must_use]
+#[stable(feature = "rust1", since = "1.0.0")]
+pub fn stdin() -> Stdin {
+ static INSTANCE: OnceLock<Mutex<BufReader<StdinRaw>>> = OnceLock::new();
+ Stdin {
+ inner: INSTANCE.get_or_init(|| {
+ Mutex::new(BufReader::with_capacity(stdio::STDIN_BUF_SIZE, stdin_raw()))
+ }),
+ }
+}
+
+impl Stdin {
+ /// Locks this handle to the standard input stream, returning a readable
+ /// guard.
+ ///
+ /// The lock is released when the returned lock goes out of scope. The
+ /// returned guard also implements the [`Read`] and [`BufRead`] traits for
+ /// accessing the underlying data.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::{self, BufRead};
+ ///
+ /// fn main() -> io::Result<()> {
+ /// let mut buffer = String::new();
+ /// let stdin = io::stdin();
+ /// let mut handle = stdin.lock();
+ ///
+ /// handle.read_line(&mut buffer)?;
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn lock(&self) -> StdinLock<'static> {
+ // Locks this handle with 'static lifetime. This depends on the
+ // implementation detail that the underlying `Mutex` is static.
+ StdinLock { inner: self.inner.lock().unwrap_or_else(|e| e.into_inner()) }
+ }
+
+ /// Locks this handle and reads a line of input, appending it to the specified buffer.
+ ///
+ /// For detailed semantics of this method, see the documentation on
+ /// [`BufRead::read_line`].
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io;
+ ///
+ /// let mut input = String::new();
+ /// match io::stdin().read_line(&mut input) {
+ /// Ok(n) => {
+ /// println!("{n} bytes read");
+ /// println!("{input}");
+ /// }
+ /// Err(error) => println!("error: {error}"),
+ /// }
+ /// ```
+ ///
+ /// You can run the example one of two ways:
+ ///
+ /// - Pipe some text to it, e.g., `printf foo | path/to/executable`
+ /// - Give it text interactively by running the executable directly,
+ /// in which case it will wait for the Enter key to be pressed before
+ /// continuing
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn read_line(&self, buf: &mut String) -> io::Result<usize> {
+ self.lock().read_line(buf)
+ }
+
+ /// Consumes this handle and returns an iterator over input lines.
+ ///
+ /// For detailed semantics of this method, see the documentation on
+ /// [`BufRead::lines`].
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io;
+ ///
+ /// let lines = io::stdin().lines();
+ /// for line in lines {
+ /// println!("got a line: {}", line.unwrap());
+ /// }
+ /// ```
+ #[must_use = "`self` will be dropped if the result is not used"]
+ #[stable(feature = "stdin_forwarders", since = "1.62.0")]
+ pub fn lines(self) -> Lines<StdinLock<'static>> {
+ self.lock().lines()
+ }
+}
+
+#[stable(feature = "std_debug", since = "1.16.0")]
+impl fmt::Debug for Stdin {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("Stdin").finish_non_exhaustive()
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Read for Stdin {
+ fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+ self.lock().read(buf)
+ }
+ fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
+ self.lock().read_vectored(bufs)
+ }
+ #[inline]
+ fn is_read_vectored(&self) -> bool {
+ self.lock().is_read_vectored()
+ }
+ fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
+ self.lock().read_to_end(buf)
+ }
+ fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
+ self.lock().read_to_string(buf)
+ }
+ fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
+ self.lock().read_exact(buf)
+ }
+}
+
+// only used by platform-dependent io::copy specializations, i.e. unused on some platforms
+#[cfg(any(target_os = "linux", target_os = "android"))]
+impl StdinLock<'_> {
+ pub(crate) fn as_mut_buf(&mut self) -> &mut BufReader<impl Read> {
+ &mut self.inner
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Read for StdinLock<'_> {
+ fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+ self.inner.read(buf)
+ }
+
+ fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
+ self.inner.read_vectored(bufs)
+ }
+
+ #[inline]
+ fn is_read_vectored(&self) -> bool {
+ self.inner.is_read_vectored()
+ }
+
+ fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
+ self.inner.read_to_end(buf)
+ }
+
+ fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
+ self.inner.read_to_string(buf)
+ }
+
+ fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
+ self.inner.read_exact(buf)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl BufRead for StdinLock<'_> {
+ fn fill_buf(&mut self) -> io::Result<&[u8]> {
+ self.inner.fill_buf()
+ }
+
+ fn consume(&mut self, n: usize) {
+ self.inner.consume(n)
+ }
+
+ fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> {
+ self.inner.read_until(byte, buf)
+ }
+
+ fn read_line(&mut self, buf: &mut String) -> io::Result<usize> {
+ self.inner.read_line(buf)
+ }
+}
+
+#[stable(feature = "std_debug", since = "1.16.0")]
+impl fmt::Debug for StdinLock<'_> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("StdinLock").finish_non_exhaustive()
+ }
+}
+
+/// A handle to the global standard output stream of the current process.
+///
+/// Each handle shares a global buffer of data to be written to the standard
+/// output stream. Access is also synchronized via a lock and explicit control
+/// over locking is available via the [`lock`] method.
+///
+/// Created by the [`io::stdout`] method.
+///
+/// ### Note: Windows Portability Considerations
+///
+/// When operating in a console, the Windows implementation of this stream does not support
+/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
+/// an error.
+///
+/// In a process with a detached console, such as one using
+/// `#![windows_subsystem = "windows"]`, or in a child process spawned from such a process,
+/// the contained handle will be null. In such cases, the standard library's `Read` and
+/// `Write` will do nothing and silently succeed. All other I/O operations, via the
+/// standard library or via raw Windows API calls, will fail.
+///
+/// [`lock`]: Stdout::lock
+/// [`io::stdout`]: stdout
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct Stdout {
+ // FIXME: this should be LineWriter or BufWriter depending on the state of
+ // stdout (tty or not). Note that if this is not line buffered it
+ // should also flush-on-panic or some form of flush-on-abort.
+ inner: Pin<&'static ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>>,
+}
+
+/// A locked reference to the [`Stdout`] handle.
+///
+/// This handle implements the [`Write`] trait, and is constructed via
+/// the [`Stdout::lock`] method. See its documentation for more.
+///
+/// ### Note: Windows Portability Considerations
+///
+/// When operating in a console, the Windows implementation of this stream does not support
+/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
+/// an error.
+///
+/// In a process with a detached console, such as one using
+/// `#![windows_subsystem = "windows"]`, or in a child process spawned from such a process,
+/// the contained handle will be null. In such cases, the standard library's `Read` and
+/// `Write` will do nothing and silently succeed. All other I/O operations, via the
+/// standard library or via raw Windows API calls, will fail.
+#[must_use = "if unused stdout will immediately unlock"]
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct StdoutLock<'a> {
+ inner: ReentrantMutexGuard<'a, RefCell<LineWriter<StdoutRaw>>>,
+}
+
+static STDOUT: OnceLock<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>> = OnceLock::new();
+
+/// Constructs a new handle to the standard output of the current process.
+///
+/// Each handle returned is a reference to a shared global buffer whose access
+/// is synchronized via a mutex. If you need more explicit control over
+/// locking, see the [`Stdout::lock`] method.
+///
+/// ### Note: Windows Portability Considerations
+///
+/// When operating in a console, the Windows implementation of this stream does not support
+/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
+/// an error.
+///
+/// In a process with a detached console, such as one using
+/// `#![windows_subsystem = "windows"]`, or in a child process spawned from such a process,
+/// the contained handle will be null. In such cases, the standard library's `Read` and
+/// `Write` will do nothing and silently succeed. All other I/O operations, via the
+/// standard library or via raw Windows API calls, will fail.
+///
+/// # Examples
+///
+/// Using implicit synchronization:
+///
+/// ```no_run
+/// use std::io::{self, Write};
+///
+/// fn main() -> io::Result<()> {
+/// io::stdout().write_all(b"hello world")?;
+///
+/// Ok(())
+/// }
+/// ```
+///
+/// Using explicit synchronization:
+///
+/// ```no_run
+/// use std::io::{self, Write};
+///
+/// fn main() -> io::Result<()> {
+/// let stdout = io::stdout();
+/// let mut handle = stdout.lock();
+///
+/// handle.write_all(b"hello world")?;
+///
+/// Ok(())
+/// }
+/// ```
+#[must_use]
+#[stable(feature = "rust1", since = "1.0.0")]
+pub fn stdout() -> Stdout {
+ Stdout {
+ inner: Pin::static_ref(&STDOUT).get_or_init_pin(
+ || unsafe { ReentrantMutex::new(RefCell::new(LineWriter::new(stdout_raw()))) },
+ |mutex| unsafe { mutex.init() },
+ ),
+ }
+}
+
+pub fn cleanup() {
+ if let Some(instance) = STDOUT.get() {
+ // Flush the data and disable buffering during shutdown
+ // by replacing the line writer by one with zero
+ // buffering capacity.
+ // We use try_lock() instead of lock(), because someone
+ // might have leaked a StdoutLock, which would
+ // otherwise cause a deadlock here.
+ if let Some(lock) = Pin::static_ref(instance).try_lock() {
+ *lock.borrow_mut() = LineWriter::with_capacity(0, stdout_raw());
+ }
+ }
+}
+
+impl Stdout {
+ /// Locks this handle to the standard output stream, returning a writable
+ /// guard.
+ ///
+ /// The lock is released when the returned lock goes out of scope. The
+ /// returned guard also implements the `Write` trait for writing data.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::{self, Write};
+ ///
+ /// fn main() -> io::Result<()> {
+ /// let mut stdout = io::stdout().lock();
+ ///
+ /// stdout.write_all(b"hello world")?;
+ ///
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn lock(&self) -> StdoutLock<'static> {
+ // Locks this handle with 'static lifetime. This depends on the
+ // implementation detail that the underlying `ReentrantMutex` is
+ // static.
+ StdoutLock { inner: self.inner.lock() }
+ }
+}
+
+#[stable(feature = "std_debug", since = "1.16.0")]
+impl fmt::Debug for Stdout {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("Stdout").finish_non_exhaustive()
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Write for Stdout {
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ (&*self).write(buf)
+ }
+ fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+ (&*self).write_vectored(bufs)
+ }
+ #[inline]
+ fn is_write_vectored(&self) -> bool {
+ io::Write::is_write_vectored(&&*self)
+ }
+ fn flush(&mut self) -> io::Result<()> {
+ (&*self).flush()
+ }
+ fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
+ (&*self).write_all(buf)
+ }
+ fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
+ (&*self).write_all_vectored(bufs)
+ }
+ fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> {
+ (&*self).write_fmt(args)
+ }
+}
+
+#[stable(feature = "write_mt", since = "1.48.0")]
+impl Write for &Stdout {
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ self.lock().write(buf)
+ }
+ fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+ self.lock().write_vectored(bufs)
+ }
+ #[inline]
+ fn is_write_vectored(&self) -> bool {
+ self.lock().is_write_vectored()
+ }
+ fn flush(&mut self) -> io::Result<()> {
+ self.lock().flush()
+ }
+ fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
+ self.lock().write_all(buf)
+ }
+ fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
+ self.lock().write_all_vectored(bufs)
+ }
+ fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> {
+ self.lock().write_fmt(args)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Write for StdoutLock<'_> {
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ self.inner.borrow_mut().write(buf)
+ }
+ fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+ self.inner.borrow_mut().write_vectored(bufs)
+ }
+ #[inline]
+ fn is_write_vectored(&self) -> bool {
+ self.inner.borrow_mut().is_write_vectored()
+ }
+ fn flush(&mut self) -> io::Result<()> {
+ self.inner.borrow_mut().flush()
+ }
+ fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
+ self.inner.borrow_mut().write_all(buf)
+ }
+ fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
+ self.inner.borrow_mut().write_all_vectored(bufs)
+ }
+}
+
+#[stable(feature = "std_debug", since = "1.16.0")]
+impl fmt::Debug for StdoutLock<'_> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("StdoutLock").finish_non_exhaustive()
+ }
+}
+
+/// A handle to the standard error stream of a process.
+///
+/// For more information, see the [`io::stderr`] method.
+///
+/// [`io::stderr`]: stderr
+///
+/// ### Note: Windows Portability Considerations
+///
+/// When operating in a console, the Windows implementation of this stream does not support
+/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
+/// an error.
+///
+/// In a process with a detached console, such as one using
+/// `#![windows_subsystem = "windows"]`, or in a child process spawned from such a process,
+/// the contained handle will be null. In such cases, the standard library's `Read` and
+/// `Write` will do nothing and silently succeed. All other I/O operations, via the
+/// standard library or via raw Windows API calls, will fail.
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct Stderr {
+ inner: Pin<&'static ReentrantMutex<RefCell<StderrRaw>>>,
+}
+
+/// A locked reference to the [`Stderr`] handle.
+///
+/// This handle implements the [`Write`] trait and is constructed via
+/// the [`Stderr::lock`] method. See its documentation for more.
+///
+/// ### Note: Windows Portability Considerations
+///
+/// When operating in a console, the Windows implementation of this stream does not support
+/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
+/// an error.
+///
+/// In a process with a detached console, such as one using
+/// `#![windows_subsystem = "windows"]`, or in a child process spawned from such a process,
+/// the contained handle will be null. In such cases, the standard library's `Read` and
+/// `Write` will do nothing and silently succeed. All other I/O operations, via the
+/// standard library or via raw Windows API calls, will fail.
+#[must_use = "if unused stderr will immediately unlock"]
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct StderrLock<'a> {
+ inner: ReentrantMutexGuard<'a, RefCell<StderrRaw>>,
+}
+
+/// Constructs a new handle to the standard error of the current process.
+///
+/// This handle is not buffered.
+///
+/// ### Note: Windows Portability Considerations
+///
+/// When operating in a console, the Windows implementation of this stream does not support
+/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
+/// an error.
+///
+/// In a process with a detached console, such as one using
+/// `#![windows_subsystem = "windows"]`, or in a child process spawned from such a process,
+/// the contained handle will be null. In such cases, the standard library's `Read` and
+/// `Write` will do nothing and silently succeed. All other I/O operations, via the
+/// standard library or via raw Windows API calls, will fail.
+///
+/// # Examples
+///
+/// Using implicit synchronization:
+///
+/// ```no_run
+/// use std::io::{self, Write};
+///
+/// fn main() -> io::Result<()> {
+/// io::stderr().write_all(b"hello world")?;
+///
+/// Ok(())
+/// }
+/// ```
+///
+/// Using explicit synchronization:
+///
+/// ```no_run
+/// use std::io::{self, Write};
+///
+/// fn main() -> io::Result<()> {
+/// let stderr = io::stderr();
+/// let mut handle = stderr.lock();
+///
+/// handle.write_all(b"hello world")?;
+///
+/// Ok(())
+/// }
+/// ```
+#[must_use]
+#[stable(feature = "rust1", since = "1.0.0")]
+pub fn stderr() -> Stderr {
+ // Note that unlike `stdout()` we don't use `at_exit` here to register a
+ // destructor. Stderr is not buffered , so there's no need to run a
+ // destructor for flushing the buffer
+ static INSTANCE: OnceLock<ReentrantMutex<RefCell<StderrRaw>>> = OnceLock::new();
+
+ Stderr {
+ inner: Pin::static_ref(&INSTANCE).get_or_init_pin(
+ || unsafe { ReentrantMutex::new(RefCell::new(stderr_raw())) },
+ |mutex| unsafe { mutex.init() },
+ ),
+ }
+}
+
+impl Stderr {
+ /// Locks this handle to the standard error stream, returning a writable
+ /// guard.
+ ///
+ /// The lock is released when the returned lock goes out of scope. The
+ /// returned guard also implements the [`Write`] trait for writing data.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::{self, Write};
+ ///
+ /// fn foo() -> io::Result<()> {
+ /// let stderr = io::stderr();
+ /// let mut handle = stderr.lock();
+ ///
+ /// handle.write_all(b"hello world")?;
+ ///
+ /// Ok(())
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn lock(&self) -> StderrLock<'static> {
+ // Locks this handle with 'static lifetime. This depends on the
+ // implementation detail that the underlying `ReentrantMutex` is
+ // static.
+ StderrLock { inner: self.inner.lock() }
+ }
+}
+
+#[stable(feature = "std_debug", since = "1.16.0")]
+impl fmt::Debug for Stderr {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("Stderr").finish_non_exhaustive()
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Write for Stderr {
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ (&*self).write(buf)
+ }
+ fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+ (&*self).write_vectored(bufs)
+ }
+ #[inline]
+ fn is_write_vectored(&self) -> bool {
+ io::Write::is_write_vectored(&&*self)
+ }
+ fn flush(&mut self) -> io::Result<()> {
+ (&*self).flush()
+ }
+ fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
+ (&*self).write_all(buf)
+ }
+ fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
+ (&*self).write_all_vectored(bufs)
+ }
+ fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> {
+ (&*self).write_fmt(args)
+ }
+}
+
+#[stable(feature = "write_mt", since = "1.48.0")]
+impl Write for &Stderr {
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ self.lock().write(buf)
+ }
+ fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+ self.lock().write_vectored(bufs)
+ }
+ #[inline]
+ fn is_write_vectored(&self) -> bool {
+ self.lock().is_write_vectored()
+ }
+ fn flush(&mut self) -> io::Result<()> {
+ self.lock().flush()
+ }
+ fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
+ self.lock().write_all(buf)
+ }
+ fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
+ self.lock().write_all_vectored(bufs)
+ }
+ fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> {
+ self.lock().write_fmt(args)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Write for StderrLock<'_> {
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ self.inner.borrow_mut().write(buf)
+ }
+ fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+ self.inner.borrow_mut().write_vectored(bufs)
+ }
+ #[inline]
+ fn is_write_vectored(&self) -> bool {
+ self.inner.borrow_mut().is_write_vectored()
+ }
+ fn flush(&mut self) -> io::Result<()> {
+ self.inner.borrow_mut().flush()
+ }
+ fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
+ self.inner.borrow_mut().write_all(buf)
+ }
+ fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
+ self.inner.borrow_mut().write_all_vectored(bufs)
+ }
+}
+
+#[stable(feature = "std_debug", since = "1.16.0")]
+impl fmt::Debug for StderrLock<'_> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("StderrLock").finish_non_exhaustive()
+ }
+}
+
+/// Sets the thread-local output capture buffer and returns the old one.
+#[unstable(
+ feature = "internal_output_capture",
+ reason = "this function is meant for use in the test crate \
+ and may disappear in the future",
+ issue = "none"
+)]
+#[doc(hidden)]
+pub fn set_output_capture(sink: Option<LocalStream>) -> Option<LocalStream> {
+ if sink.is_none() && !OUTPUT_CAPTURE_USED.load(Ordering::Relaxed) {
+ // OUTPUT_CAPTURE is definitely None since OUTPUT_CAPTURE_USED is false.
+ return None;
+ }
+ OUTPUT_CAPTURE_USED.store(true, Ordering::Relaxed);
+ OUTPUT_CAPTURE.with(move |slot| slot.replace(sink))
+}
+
+/// Write `args` to the capture buffer if enabled and possible, or `global_s`
+/// otherwise. `label` identifies the stream in a panic message.
+///
+/// This function is used to print error messages, so it takes extra
+/// care to avoid causing a panic when `local_s` is unusable.
+/// For instance, if the TLS key for the local stream is
+/// already destroyed, or if the local stream is locked by another
+/// thread, it will just fall back to the global stream.
+///
+/// However, if the actual I/O causes an error, this function does panic.
+fn print_to<T>(args: fmt::Arguments<'_>, global_s: fn() -> T, label: &str)
+where
+ T: Write,
+{
+ if OUTPUT_CAPTURE_USED.load(Ordering::Relaxed)
+ && OUTPUT_CAPTURE.try_with(|s| {
+ // Note that we completely remove a local sink to write to in case
+ // our printing recursively panics/prints, so the recursive
+ // panic/print goes to the global sink instead of our local sink.
+ s.take().map(|w| {
+ let _ = w.lock().unwrap_or_else(|e| e.into_inner()).write_fmt(args);
+ s.set(Some(w));
+ })
+ }) == Ok(Some(()))
+ {
+ // Successfully wrote to capture buffer.
+ return;
+ }
+
+ if let Err(e) = global_s().write_fmt(args) {
+ panic!("failed printing to {label}: {e}");
+ }
+}
+
+#[unstable(
+ feature = "print_internals",
+ reason = "implementation detail which may disappear or be replaced at any time",
+ issue = "none"
+)]
+#[doc(hidden)]
+#[cfg(not(test))]
+pub fn _print(args: fmt::Arguments<'_>) {
+ print_to(args, stdout, "stdout");
+}
+
+#[unstable(
+ feature = "print_internals",
+ reason = "implementation detail which may disappear or be replaced at any time",
+ issue = "none"
+)]
+#[doc(hidden)]
+#[cfg(not(test))]
+pub fn _eprint(args: fmt::Arguments<'_>) {
+ print_to(args, stderr, "stderr");
+}
+
+#[cfg(test)]
+pub use realstd::io::{_eprint, _print};
diff --git a/library/std/src/io/stdio/tests.rs b/library/std/src/io/stdio/tests.rs
new file mode 100644
index 000000000..f89fd27ce
--- /dev/null
+++ b/library/std/src/io/stdio/tests.rs
@@ -0,0 +1,166 @@
+use super::*;
+use crate::panic::{RefUnwindSafe, UnwindSafe};
+use crate::sync::mpsc::sync_channel;
+use crate::thread;
+
+#[test]
+fn stdout_unwind_safe() {
+ assert_unwind_safe::<Stdout>();
+}
+#[test]
+fn stdoutlock_unwind_safe() {
+ assert_unwind_safe::<StdoutLock<'_>>();
+ assert_unwind_safe::<StdoutLock<'static>>();
+}
+#[test]
+fn stderr_unwind_safe() {
+ assert_unwind_safe::<Stderr>();
+}
+#[test]
+fn stderrlock_unwind_safe() {
+ assert_unwind_safe::<StderrLock<'_>>();
+ assert_unwind_safe::<StderrLock<'static>>();
+}
+
+fn assert_unwind_safe<T: UnwindSafe + RefUnwindSafe>() {}
+
+#[test]
+#[cfg_attr(target_os = "emscripten", ignore)]
+fn panic_doesnt_poison() {
+ thread::spawn(|| {
+ let _a = stdin();
+ let _a = _a.lock();
+ let _a = stdout();
+ let _a = _a.lock();
+ let _a = stderr();
+ let _a = _a.lock();
+ panic!();
+ })
+ .join()
+ .unwrap_err();
+
+ let _a = stdin();
+ let _a = _a.lock();
+ let _a = stdout();
+ let _a = _a.lock();
+ let _a = stderr();
+ let _a = _a.lock();
+}
+
+#[test]
+#[cfg_attr(target_os = "emscripten", ignore)]
+fn test_lock_stderr() {
+ test_lock(stderr, || stderr().lock());
+}
+#[test]
+#[cfg_attr(target_os = "emscripten", ignore)]
+fn test_lock_stdin() {
+ test_lock(stdin, || stdin().lock());
+}
+#[test]
+#[cfg_attr(target_os = "emscripten", ignore)]
+fn test_lock_stdout() {
+ test_lock(stdout, || stdout().lock());
+}
+
+// Helper trait to make lock testing function generic.
+trait Stdio<'a>: 'static
+where
+ Self::Lock: 'a,
+{
+ type Lock;
+ fn lock(&'a self) -> Self::Lock;
+}
+impl<'a> Stdio<'a> for Stderr {
+ type Lock = StderrLock<'a>;
+ fn lock(&'a self) -> StderrLock<'a> {
+ self.lock()
+ }
+}
+impl<'a> Stdio<'a> for Stdin {
+ type Lock = StdinLock<'a>;
+ fn lock(&'a self) -> StdinLock<'a> {
+ self.lock()
+ }
+}
+impl<'a> Stdio<'a> for Stdout {
+ type Lock = StdoutLock<'a>;
+ fn lock(&'a self) -> StdoutLock<'a> {
+ self.lock()
+ }
+}
+
+// Helper trait to make lock testing function generic.
+trait StdioOwnedLock: 'static {}
+impl StdioOwnedLock for StderrLock<'static> {}
+impl StdioOwnedLock for StdinLock<'static> {}
+impl StdioOwnedLock for StdoutLock<'static> {}
+
+// Tests locking on stdio handles by starting two threads and checking that
+// they block each other appropriately.
+fn test_lock<T, U>(get_handle: fn() -> T, get_locked: fn() -> U)
+where
+ T: for<'a> Stdio<'a>,
+ U: StdioOwnedLock,
+{
+ // State enum to track different phases of the test, primarily when
+ // each lock is acquired and released.
+ #[derive(Debug, PartialEq)]
+ enum State {
+ Start1,
+ Acquire1,
+ Start2,
+ Release1,
+ Acquire2,
+ Release2,
+ }
+ use State::*;
+ // Logging vector to be checked to make sure lock acquisitions and
+ // releases happened in the correct order.
+ let log = Arc::new(Mutex::new(Vec::new()));
+ let ((tx1, rx1), (tx2, rx2)) = (sync_channel(0), sync_channel(0));
+ let th1 = {
+ let (log, tx) = (Arc::clone(&log), tx1);
+ thread::spawn(move || {
+ log.lock().unwrap().push(Start1);
+ let handle = get_handle();
+ {
+ let locked = handle.lock();
+ log.lock().unwrap().push(Acquire1);
+ tx.send(Acquire1).unwrap(); // notify of acquisition
+ tx.send(Release1).unwrap(); // wait for release command
+ log.lock().unwrap().push(Release1);
+ }
+ tx.send(Acquire1).unwrap(); // wait for th2 acquire
+ {
+ let locked = handle.lock();
+ log.lock().unwrap().push(Acquire1);
+ }
+ log.lock().unwrap().push(Release1);
+ })
+ };
+ let th2 = {
+ let (log, tx) = (Arc::clone(&log), tx2);
+ thread::spawn(move || {
+ tx.send(Start2).unwrap(); // wait for start command
+ let locked = get_locked();
+ log.lock().unwrap().push(Acquire2);
+ tx.send(Acquire2).unwrap(); // notify of acquisition
+ tx.send(Release2).unwrap(); // wait for release command
+ log.lock().unwrap().push(Release2);
+ })
+ };
+ assert_eq!(rx1.recv().unwrap(), Acquire1); // wait for th1 acquire
+ log.lock().unwrap().push(Start2);
+ assert_eq!(rx2.recv().unwrap(), Start2); // block th2
+ assert_eq!(rx1.recv().unwrap(), Release1); // release th1
+ assert_eq!(rx2.recv().unwrap(), Acquire2); // wait for th2 acquire
+ assert_eq!(rx1.recv().unwrap(), Acquire1); // block th1
+ assert_eq!(rx2.recv().unwrap(), Release2); // release th2
+ th2.join().unwrap();
+ th1.join().unwrap();
+ assert_eq!(
+ *log.lock().unwrap(),
+ [Start1, Acquire1, Start2, Release1, Acquire2, Release2, Acquire1, Release1]
+ );
+}
diff --git a/library/std/src/io/tests.rs b/library/std/src/io/tests.rs
new file mode 100644
index 000000000..f357f33ec
--- /dev/null
+++ b/library/std/src/io/tests.rs
@@ -0,0 +1,623 @@
+use super::{repeat, Cursor, ReadBuf, SeekFrom};
+use crate::cmp::{self, min};
+use crate::io::{self, IoSlice, IoSliceMut};
+use crate::io::{BufRead, BufReader, Read, Seek, Write};
+use crate::mem::MaybeUninit;
+use crate::ops::Deref;
+
+#[test]
+#[cfg_attr(target_os = "emscripten", ignore)]
+fn read_until() {
+ let mut buf = Cursor::new(&b"12"[..]);
+ let mut v = Vec::new();
+ assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 2);
+ assert_eq!(v, b"12");
+
+ let mut buf = Cursor::new(&b"1233"[..]);
+ let mut v = Vec::new();
+ assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 3);
+ assert_eq!(v, b"123");
+ v.truncate(0);
+ assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 1);
+ assert_eq!(v, b"3");
+ v.truncate(0);
+ assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 0);
+ assert_eq!(v, []);
+}
+
+#[test]
+fn split() {
+ let buf = Cursor::new(&b"12"[..]);
+ let mut s = buf.split(b'3');
+ assert_eq!(s.next().unwrap().unwrap(), vec![b'1', b'2']);
+ assert!(s.next().is_none());
+
+ let buf = Cursor::new(&b"1233"[..]);
+ let mut s = buf.split(b'3');
+ assert_eq!(s.next().unwrap().unwrap(), vec![b'1', b'2']);
+ assert_eq!(s.next().unwrap().unwrap(), vec![]);
+ assert!(s.next().is_none());
+}
+
+#[test]
+fn read_line() {
+ let mut buf = Cursor::new(&b"12"[..]);
+ let mut v = String::new();
+ assert_eq!(buf.read_line(&mut v).unwrap(), 2);
+ assert_eq!(v, "12");
+
+ let mut buf = Cursor::new(&b"12\n\n"[..]);
+ let mut v = String::new();
+ assert_eq!(buf.read_line(&mut v).unwrap(), 3);
+ assert_eq!(v, "12\n");
+ v.truncate(0);
+ assert_eq!(buf.read_line(&mut v).unwrap(), 1);
+ assert_eq!(v, "\n");
+ v.truncate(0);
+ assert_eq!(buf.read_line(&mut v).unwrap(), 0);
+ assert_eq!(v, "");
+}
+
+#[test]
+fn lines() {
+ let buf = Cursor::new(&b"12\r"[..]);
+ let mut s = buf.lines();
+ assert_eq!(s.next().unwrap().unwrap(), "12\r".to_string());
+ assert!(s.next().is_none());
+
+ let buf = Cursor::new(&b"12\r\n\n"[..]);
+ let mut s = buf.lines();
+ assert_eq!(s.next().unwrap().unwrap(), "12".to_string());
+ assert_eq!(s.next().unwrap().unwrap(), "".to_string());
+ assert!(s.next().is_none());
+}
+
+#[test]
+fn buf_read_has_data_left() {
+ let mut buf = Cursor::new(&b"abcd"[..]);
+ assert!(buf.has_data_left().unwrap());
+ buf.read_exact(&mut [0; 2]).unwrap();
+ assert!(buf.has_data_left().unwrap());
+ buf.read_exact(&mut [0; 2]).unwrap();
+ assert!(!buf.has_data_left().unwrap());
+}
+
+#[test]
+fn read_to_end() {
+ let mut c = Cursor::new(&b""[..]);
+ let mut v = Vec::new();
+ assert_eq!(c.read_to_end(&mut v).unwrap(), 0);
+ assert_eq!(v, []);
+
+ let mut c = Cursor::new(&b"1"[..]);
+ let mut v = Vec::new();
+ assert_eq!(c.read_to_end(&mut v).unwrap(), 1);
+ assert_eq!(v, b"1");
+
+ let cap = 1024 * 1024;
+ let data = (0..cap).map(|i| (i / 3) as u8).collect::<Vec<_>>();
+ let mut v = Vec::new();
+ let (a, b) = data.split_at(data.len() / 2);
+ assert_eq!(Cursor::new(a).read_to_end(&mut v).unwrap(), a.len());
+ assert_eq!(Cursor::new(b).read_to_end(&mut v).unwrap(), b.len());
+ assert_eq!(v, data);
+}
+
+#[test]
+fn read_to_string() {
+ let mut c = Cursor::new(&b""[..]);
+ let mut v = String::new();
+ assert_eq!(c.read_to_string(&mut v).unwrap(), 0);
+ assert_eq!(v, "");
+
+ let mut c = Cursor::new(&b"1"[..]);
+ let mut v = String::new();
+ assert_eq!(c.read_to_string(&mut v).unwrap(), 1);
+ assert_eq!(v, "1");
+
+ let mut c = Cursor::new(&b"\xff"[..]);
+ let mut v = String::new();
+ assert!(c.read_to_string(&mut v).is_err());
+}
+
+#[test]
+fn read_exact() {
+ let mut buf = [0; 4];
+
+ let mut c = Cursor::new(&b""[..]);
+ assert_eq!(c.read_exact(&mut buf).unwrap_err().kind(), io::ErrorKind::UnexpectedEof);
+
+ let mut c = Cursor::new(&b"123"[..]).chain(Cursor::new(&b"456789"[..]));
+ c.read_exact(&mut buf).unwrap();
+ assert_eq!(&buf, b"1234");
+ c.read_exact(&mut buf).unwrap();
+ assert_eq!(&buf, b"5678");
+ assert_eq!(c.read_exact(&mut buf).unwrap_err().kind(), io::ErrorKind::UnexpectedEof);
+}
+
+#[test]
+fn read_exact_slice() {
+ let mut buf = [0; 4];
+
+ let mut c = &b""[..];
+ assert_eq!(c.read_exact(&mut buf).unwrap_err().kind(), io::ErrorKind::UnexpectedEof);
+
+ let mut c = &b"123"[..];
+ assert_eq!(c.read_exact(&mut buf).unwrap_err().kind(), io::ErrorKind::UnexpectedEof);
+ // make sure the optimized (early returning) method is being used
+ assert_eq!(&buf, &[0; 4]);
+
+ let mut c = &b"1234"[..];
+ c.read_exact(&mut buf).unwrap();
+ assert_eq!(&buf, b"1234");
+
+ let mut c = &b"56789"[..];
+ c.read_exact(&mut buf).unwrap();
+ assert_eq!(&buf, b"5678");
+ assert_eq!(c, b"9");
+}
+
+#[test]
+fn read_buf_exact() {
+ let mut buf = [0; 4];
+ let mut buf = ReadBuf::new(&mut buf);
+
+ let mut c = Cursor::new(&b""[..]);
+ assert_eq!(c.read_buf_exact(&mut buf).unwrap_err().kind(), io::ErrorKind::UnexpectedEof);
+
+ let mut c = Cursor::new(&b"123456789"[..]);
+ c.read_buf_exact(&mut buf).unwrap();
+ assert_eq!(buf.filled(), b"1234");
+
+ buf.clear();
+
+ c.read_buf_exact(&mut buf).unwrap();
+ assert_eq!(buf.filled(), b"5678");
+
+ buf.clear();
+
+ assert_eq!(c.read_buf_exact(&mut buf).unwrap_err().kind(), io::ErrorKind::UnexpectedEof);
+}
+
+#[test]
+fn take_eof() {
+ struct R;
+
+ impl Read for R {
+ fn read(&mut self, _: &mut [u8]) -> io::Result<usize> {
+ Err(io::const_io_error!(io::ErrorKind::Other, ""))
+ }
+ }
+ impl BufRead for R {
+ fn fill_buf(&mut self) -> io::Result<&[u8]> {
+ Err(io::const_io_error!(io::ErrorKind::Other, ""))
+ }
+ fn consume(&mut self, _amt: usize) {}
+ }
+
+ let mut buf = [0; 1];
+ assert_eq!(0, R.take(0).read(&mut buf).unwrap());
+ assert_eq!(b"", R.take(0).fill_buf().unwrap());
+}
+
+fn cmp_bufread<Br1: BufRead, Br2: BufRead>(mut br1: Br1, mut br2: Br2, exp: &[u8]) {
+ let mut cat = Vec::new();
+ loop {
+ let consume = {
+ let buf1 = br1.fill_buf().unwrap();
+ let buf2 = br2.fill_buf().unwrap();
+ let minlen = if buf1.len() < buf2.len() { buf1.len() } else { buf2.len() };
+ assert_eq!(buf1[..minlen], buf2[..minlen]);
+ cat.extend_from_slice(&buf1[..minlen]);
+ minlen
+ };
+ if consume == 0 {
+ break;
+ }
+ br1.consume(consume);
+ br2.consume(consume);
+ }
+ assert_eq!(br1.fill_buf().unwrap().len(), 0);
+ assert_eq!(br2.fill_buf().unwrap().len(), 0);
+ assert_eq!(&cat[..], &exp[..])
+}
+
+#[test]
+fn chain_bufread() {
+ let testdata = b"ABCDEFGHIJKL";
+ let chain1 =
+ (&testdata[..3]).chain(&testdata[3..6]).chain(&testdata[6..9]).chain(&testdata[9..]);
+ let chain2 = (&testdata[..4]).chain(&testdata[4..8]).chain(&testdata[8..]);
+ cmp_bufread(chain1, chain2, &testdata[..]);
+}
+
+#[test]
+fn bufreader_size_hint() {
+ let testdata = b"ABCDEFGHIJKL";
+ let mut buf_reader = BufReader::new(&testdata[..]);
+ assert_eq!(buf_reader.buffer().len(), 0);
+
+ let buffer_length = testdata.len();
+ buf_reader.fill_buf().unwrap();
+
+ // Check that size hint matches buffer contents
+ let mut buffered_bytes = buf_reader.bytes();
+ let (lower_bound, _upper_bound) = buffered_bytes.size_hint();
+ assert_eq!(lower_bound, buffer_length);
+
+ // Check that size hint matches buffer contents after advancing
+ buffered_bytes.next().unwrap().unwrap();
+ let (lower_bound, _upper_bound) = buffered_bytes.size_hint();
+ assert_eq!(lower_bound, buffer_length - 1);
+}
+
+#[test]
+fn empty_size_hint() {
+ let size_hint = io::empty().bytes().size_hint();
+ assert_eq!(size_hint, (0, Some(0)));
+}
+
+#[test]
+fn slice_size_hint() {
+ let size_hint = (&[1, 2, 3]).bytes().size_hint();
+ assert_eq!(size_hint, (3, Some(3)));
+}
+
+#[test]
+fn take_size_hint() {
+ let size_hint = (&[1, 2, 3]).take(2).bytes().size_hint();
+ assert_eq!(size_hint, (2, Some(2)));
+
+ let size_hint = (&[1, 2, 3]).take(4).bytes().size_hint();
+ assert_eq!(size_hint, (3, Some(3)));
+
+ let size_hint = io::repeat(0).take(3).bytes().size_hint();
+ assert_eq!(size_hint, (3, Some(3)));
+}
+
+#[test]
+fn chain_empty_size_hint() {
+ let chain = io::empty().chain(io::empty());
+ let size_hint = chain.bytes().size_hint();
+ assert_eq!(size_hint, (0, Some(0)));
+}
+
+#[test]
+fn chain_size_hint() {
+ let testdata = b"ABCDEFGHIJKL";
+ let mut buf_reader_1 = BufReader::new(&testdata[..6]);
+ let mut buf_reader_2 = BufReader::new(&testdata[6..]);
+
+ buf_reader_1.fill_buf().unwrap();
+ buf_reader_2.fill_buf().unwrap();
+
+ let chain = buf_reader_1.chain(buf_reader_2);
+ let size_hint = chain.bytes().size_hint();
+ assert_eq!(size_hint, (testdata.len(), Some(testdata.len())));
+}
+
+#[test]
+fn chain_zero_length_read_is_not_eof() {
+ let a = b"A";
+ let b = b"B";
+ let mut s = String::new();
+ let mut chain = (&a[..]).chain(&b[..]);
+ chain.read(&mut []).unwrap();
+ chain.read_to_string(&mut s).unwrap();
+ assert_eq!("AB", s);
+}
+
+#[bench]
+#[cfg_attr(target_os = "emscripten", ignore)]
+fn bench_read_to_end(b: &mut test::Bencher) {
+ b.iter(|| {
+ let mut lr = repeat(1).take(10000000);
+ let mut vec = Vec::with_capacity(1024);
+ super::default_read_to_end(&mut lr, &mut vec)
+ });
+}
+
+#[test]
+fn seek_len() -> io::Result<()> {
+ let mut c = Cursor::new(vec![0; 15]);
+ assert_eq!(c.stream_len()?, 15);
+
+ c.seek(SeekFrom::End(0))?;
+ let old_pos = c.stream_position()?;
+ assert_eq!(c.stream_len()?, 15);
+ assert_eq!(c.stream_position()?, old_pos);
+
+ c.seek(SeekFrom::Start(7))?;
+ c.seek(SeekFrom::Current(2))?;
+ let old_pos = c.stream_position()?;
+ assert_eq!(c.stream_len()?, 15);
+ assert_eq!(c.stream_position()?, old_pos);
+
+ Ok(())
+}
+
+#[test]
+fn seek_position() -> io::Result<()> {
+ // All `asserts` are duplicated here to make sure the method does not
+ // change anything about the seek state.
+ let mut c = Cursor::new(vec![0; 15]);
+ assert_eq!(c.stream_position()?, 0);
+ assert_eq!(c.stream_position()?, 0);
+
+ c.seek(SeekFrom::End(0))?;
+ assert_eq!(c.stream_position()?, 15);
+ assert_eq!(c.stream_position()?, 15);
+
+ c.seek(SeekFrom::Start(7))?;
+ c.seek(SeekFrom::Current(2))?;
+ assert_eq!(c.stream_position()?, 9);
+ assert_eq!(c.stream_position()?, 9);
+
+ c.seek(SeekFrom::End(-3))?;
+ c.seek(SeekFrom::Current(1))?;
+ c.seek(SeekFrom::Current(-5))?;
+ assert_eq!(c.stream_position()?, 8);
+ assert_eq!(c.stream_position()?, 8);
+
+ c.rewind()?;
+ assert_eq!(c.stream_position()?, 0);
+ assert_eq!(c.stream_position()?, 0);
+
+ Ok(())
+}
+
+// A simple example reader which uses the default implementation of
+// read_to_end.
+struct ExampleSliceReader<'a> {
+ slice: &'a [u8],
+}
+
+impl<'a> Read for ExampleSliceReader<'a> {
+ fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+ let len = cmp::min(self.slice.len(), buf.len());
+ buf[..len].copy_from_slice(&self.slice[..len]);
+ self.slice = &self.slice[len..];
+ Ok(len)
+ }
+}
+
+#[test]
+fn test_read_to_end_capacity() -> io::Result<()> {
+ let input = &b"foo"[..];
+
+ // read_to_end() takes care not to over-allocate when a buffer is the
+ // exact size needed.
+ let mut vec1 = Vec::with_capacity(input.len());
+ ExampleSliceReader { slice: input }.read_to_end(&mut vec1)?;
+ assert_eq!(vec1.len(), input.len());
+ assert_eq!(vec1.capacity(), input.len(), "did not allocate more");
+
+ Ok(())
+}
+
+#[test]
+fn io_slice_mut_advance_slices() {
+ let mut buf1 = [1; 8];
+ let mut buf2 = [2; 16];
+ let mut buf3 = [3; 8];
+ let mut bufs = &mut [
+ IoSliceMut::new(&mut buf1),
+ IoSliceMut::new(&mut buf2),
+ IoSliceMut::new(&mut buf3),
+ ][..];
+
+ // Only in a single buffer..
+ IoSliceMut::advance_slices(&mut bufs, 1);
+ assert_eq!(bufs[0].deref(), [1; 7].as_ref());
+ assert_eq!(bufs[1].deref(), [2; 16].as_ref());
+ assert_eq!(bufs[2].deref(), [3; 8].as_ref());
+
+ // Removing a buffer, leaving others as is.
+ IoSliceMut::advance_slices(&mut bufs, 7);
+ assert_eq!(bufs[0].deref(), [2; 16].as_ref());
+ assert_eq!(bufs[1].deref(), [3; 8].as_ref());
+
+ // Removing a buffer and removing from the next buffer.
+ IoSliceMut::advance_slices(&mut bufs, 18);
+ assert_eq!(bufs[0].deref(), [3; 6].as_ref());
+}
+
+#[test]
+#[should_panic]
+fn io_slice_mut_advance_slices_empty_slice() {
+ let mut empty_bufs = &mut [][..];
+ IoSliceMut::advance_slices(&mut empty_bufs, 1);
+}
+
+#[test]
+#[should_panic]
+fn io_slice_mut_advance_slices_beyond_total_length() {
+ let mut buf1 = [1; 8];
+ let mut bufs = &mut [IoSliceMut::new(&mut buf1)][..];
+
+ IoSliceMut::advance_slices(&mut bufs, 9);
+ assert!(bufs.is_empty());
+}
+
+#[test]
+fn io_slice_advance_slices() {
+ let buf1 = [1; 8];
+ let buf2 = [2; 16];
+ let buf3 = [3; 8];
+ let mut bufs = &mut [IoSlice::new(&buf1), IoSlice::new(&buf2), IoSlice::new(&buf3)][..];
+
+ // Only in a single buffer..
+ IoSlice::advance_slices(&mut bufs, 1);
+ assert_eq!(bufs[0].deref(), [1; 7].as_ref());
+ assert_eq!(bufs[1].deref(), [2; 16].as_ref());
+ assert_eq!(bufs[2].deref(), [3; 8].as_ref());
+
+ // Removing a buffer, leaving others as is.
+ IoSlice::advance_slices(&mut bufs, 7);
+ assert_eq!(bufs[0].deref(), [2; 16].as_ref());
+ assert_eq!(bufs[1].deref(), [3; 8].as_ref());
+
+ // Removing a buffer and removing from the next buffer.
+ IoSlice::advance_slices(&mut bufs, 18);
+ assert_eq!(bufs[0].deref(), [3; 6].as_ref());
+}
+
+#[test]
+#[should_panic]
+fn io_slice_advance_slices_empty_slice() {
+ let mut empty_bufs = &mut [][..];
+ IoSlice::advance_slices(&mut empty_bufs, 1);
+}
+
+#[test]
+#[should_panic]
+fn io_slice_advance_slices_beyond_total_length() {
+ let buf1 = [1; 8];
+ let mut bufs = &mut [IoSlice::new(&buf1)][..];
+
+ IoSlice::advance_slices(&mut bufs, 9);
+ assert!(bufs.is_empty());
+}
+
+/// Create a new writer that reads from at most `n_bufs` and reads
+/// `per_call` bytes (in total) per call to write.
+fn test_writer(n_bufs: usize, per_call: usize) -> TestWriter {
+ TestWriter { n_bufs, per_call, written: Vec::new() }
+}
+
+struct TestWriter {
+ n_bufs: usize,
+ per_call: usize,
+ written: Vec<u8>,
+}
+
+impl Write for TestWriter {
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ self.write_vectored(&[IoSlice::new(buf)])
+ }
+
+ fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+ let mut left = self.per_call;
+ let mut written = 0;
+ for buf in bufs.iter().take(self.n_bufs) {
+ let n = min(left, buf.len());
+ self.written.extend_from_slice(&buf[0..n]);
+ left -= n;
+ written += n;
+ }
+ Ok(written)
+ }
+
+ fn flush(&mut self) -> io::Result<()> {
+ Ok(())
+ }
+}
+
+#[test]
+fn test_writer_read_from_one_buf() {
+ let mut writer = test_writer(1, 2);
+
+ assert_eq!(writer.write(&[]).unwrap(), 0);
+ assert_eq!(writer.write_vectored(&[]).unwrap(), 0);
+
+ // Read at most 2 bytes.
+ assert_eq!(writer.write(&[1, 1, 1]).unwrap(), 2);
+ let bufs = &[IoSlice::new(&[2, 2, 2])];
+ assert_eq!(writer.write_vectored(bufs).unwrap(), 2);
+
+ // Only read from first buf.
+ let bufs = &[IoSlice::new(&[3]), IoSlice::new(&[4, 4])];
+ assert_eq!(writer.write_vectored(bufs).unwrap(), 1);
+
+ assert_eq!(writer.written, &[1, 1, 2, 2, 3]);
+}
+
+#[test]
+fn test_writer_read_from_multiple_bufs() {
+ let mut writer = test_writer(3, 3);
+
+ // Read at most 3 bytes from two buffers.
+ let bufs = &[IoSlice::new(&[1]), IoSlice::new(&[2, 2, 2])];
+ assert_eq!(writer.write_vectored(bufs).unwrap(), 3);
+
+ // Read at most 3 bytes from three buffers.
+ let bufs = &[IoSlice::new(&[3]), IoSlice::new(&[4]), IoSlice::new(&[5, 5])];
+ assert_eq!(writer.write_vectored(bufs).unwrap(), 3);
+
+ assert_eq!(writer.written, &[1, 2, 2, 3, 4, 5]);
+}
+
+#[test]
+fn test_write_all_vectored() {
+ #[rustfmt::skip] // Becomes unreadable otherwise.
+ let tests: Vec<(_, &'static [u8])> = vec![
+ (vec![], &[]),
+ (vec![IoSlice::new(&[]), IoSlice::new(&[])], &[]),
+ (vec![IoSlice::new(&[1])], &[1]),
+ (vec![IoSlice::new(&[1, 2])], &[1, 2]),
+ (vec![IoSlice::new(&[1, 2, 3])], &[1, 2, 3]),
+ (vec![IoSlice::new(&[1, 2, 3, 4])], &[1, 2, 3, 4]),
+ (vec![IoSlice::new(&[1, 2, 3, 4, 5])], &[1, 2, 3, 4, 5]),
+ (vec![IoSlice::new(&[1]), IoSlice::new(&[2])], &[1, 2]),
+ (vec![IoSlice::new(&[1]), IoSlice::new(&[2, 2])], &[1, 2, 2]),
+ (vec![IoSlice::new(&[1, 1]), IoSlice::new(&[2, 2])], &[1, 1, 2, 2]),
+ (vec![IoSlice::new(&[1, 1]), IoSlice::new(&[2, 2, 2])], &[1, 1, 2, 2, 2]),
+ (vec![IoSlice::new(&[1, 1]), IoSlice::new(&[2, 2, 2])], &[1, 1, 2, 2, 2]),
+ (vec![IoSlice::new(&[1, 1, 1]), IoSlice::new(&[2, 2, 2])], &[1, 1, 1, 2, 2, 2]),
+ (vec![IoSlice::new(&[1, 1, 1]), IoSlice::new(&[2, 2, 2, 2])], &[1, 1, 1, 2, 2, 2, 2]),
+ (vec![IoSlice::new(&[1, 1, 1, 1]), IoSlice::new(&[2, 2, 2, 2])], &[1, 1, 1, 1, 2, 2, 2, 2]),
+ (vec![IoSlice::new(&[1]), IoSlice::new(&[2]), IoSlice::new(&[3])], &[1, 2, 3]),
+ (vec![IoSlice::new(&[1, 1]), IoSlice::new(&[2, 2]), IoSlice::new(&[3, 3])], &[1, 1, 2, 2, 3, 3]),
+ (vec![IoSlice::new(&[1]), IoSlice::new(&[2, 2]), IoSlice::new(&[3, 3, 3])], &[1, 2, 2, 3, 3, 3]),
+ (vec![IoSlice::new(&[1, 1, 1]), IoSlice::new(&[2, 2, 2]), IoSlice::new(&[3, 3, 3])], &[1, 1, 1, 2, 2, 2, 3, 3, 3]),
+ ];
+
+ let writer_configs = &[(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)];
+
+ for (n_bufs, per_call) in writer_configs.iter().copied() {
+ for (mut input, wanted) in tests.clone().into_iter() {
+ let mut writer = test_writer(n_bufs, per_call);
+ assert!(writer.write_all_vectored(&mut *input).is_ok());
+ assert_eq!(&*writer.written, &*wanted);
+ }
+ }
+}
+
+// Issue 94981
+#[test]
+#[should_panic = "number of read bytes exceeds limit"]
+fn test_take_wrong_length() {
+ struct LieAboutSize(bool);
+
+ impl Read for LieAboutSize {
+ fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+ // Lie about the read size at first time of read.
+ if core::mem::take(&mut self.0) { Ok(buf.len() + 1) } else { Ok(buf.len()) }
+ }
+ }
+
+ let mut buffer = vec![0; 4];
+ let mut reader = LieAboutSize(true).take(4);
+ // Primed the `Limit` by lying about the read size.
+ let _ = reader.read(&mut buffer[..]);
+}
+
+#[bench]
+fn bench_take_read(b: &mut test::Bencher) {
+ b.iter(|| {
+ let mut buf = [0; 64];
+
+ [255; 128].take(64).read(&mut buf).unwrap();
+ });
+}
+
+#[bench]
+fn bench_take_read_buf(b: &mut test::Bencher) {
+ b.iter(|| {
+ let mut buf = [MaybeUninit::uninit(); 64];
+
+ let mut rbuf = ReadBuf::uninit(&mut buf);
+
+ [255; 128].take(64).read_buf(&mut rbuf).unwrap();
+ });
+}
diff --git a/library/std/src/io/util.rs b/library/std/src/io/util.rs
new file mode 100644
index 000000000..c1300cd67
--- /dev/null
+++ b/library/std/src/io/util.rs
@@ -0,0 +1,270 @@
+#![allow(missing_copy_implementations)]
+
+#[cfg(test)]
+mod tests;
+
+use crate::fmt;
+use crate::io::{
+ self, BufRead, IoSlice, IoSliceMut, Read, ReadBuf, Seek, SeekFrom, SizeHint, Write,
+};
+
+/// A reader which is always at EOF.
+///
+/// This struct is generally created by calling [`empty()`]. Please see
+/// the documentation of [`empty()`] for more details.
+#[stable(feature = "rust1", since = "1.0.0")]
+#[non_exhaustive]
+#[derive(Copy, Clone, Default)]
+pub struct Empty;
+
+/// Constructs a new handle to an empty reader.
+///
+/// All reads from the returned reader will return <code>[Ok]\(0)</code>.
+///
+/// # Examples
+///
+/// A slightly sad example of not reading anything into a buffer:
+///
+/// ```
+/// use std::io::{self, Read};
+///
+/// let mut buffer = String::new();
+/// io::empty().read_to_string(&mut buffer).unwrap();
+/// assert!(buffer.is_empty());
+/// ```
+#[must_use]
+#[stable(feature = "rust1", since = "1.0.0")]
+#[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
+pub const fn empty() -> Empty {
+ Empty
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Read for Empty {
+ #[inline]
+ fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
+ Ok(0)
+ }
+
+ #[inline]
+ fn read_buf(&mut self, _buf: &mut ReadBuf<'_>) -> io::Result<()> {
+ Ok(())
+ }
+}
+#[stable(feature = "rust1", since = "1.0.0")]
+impl BufRead for Empty {
+ #[inline]
+ fn fill_buf(&mut self) -> io::Result<&[u8]> {
+ Ok(&[])
+ }
+ #[inline]
+ fn consume(&mut self, _n: usize) {}
+}
+
+#[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)
+ }
+}
+
+#[stable(feature = "std_debug", since = "1.16.0")]
+impl fmt::Debug for Empty {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("Empty").finish_non_exhaustive()
+ }
+}
+
+impl SizeHint for Empty {
+ #[inline]
+ fn upper_bound(&self) -> Option<usize> {
+ Some(0)
+ }
+}
+
+/// A reader which yields one byte over and over and over and over and over and...
+///
+/// This struct is generally created by calling [`repeat()`]. Please
+/// see the documentation of [`repeat()`] for more details.
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct Repeat {
+ byte: u8,
+}
+
+/// 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.
+///
+/// # 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]);
+/// ```
+#[must_use]
+#[stable(feature = "rust1", since = "1.0.0")]
+#[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
+pub const fn repeat(byte: u8) -> Repeat {
+ Repeat { byte }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Read for Repeat {
+ #[inline]
+ fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+ for slot in &mut *buf {
+ *slot = self.byte;
+ }
+ Ok(buf.len())
+ }
+
+ fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> {
+ // SAFETY: No uninit bytes are being written
+ for slot in unsafe { buf.unfilled_mut() } {
+ slot.write(self.byte);
+ }
+
+ let remaining = buf.remaining();
+
+ // SAFETY: the entire unfilled portion of buf has been initialized
+ unsafe {
+ buf.assume_init(remaining);
+ }
+
+ buf.add_filled(remaining);
+
+ Ok(())
+ }
+
+ #[inline]
+ fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
+ let mut nwritten = 0;
+ for buf in bufs {
+ nwritten += self.read(buf)?;
+ }
+ Ok(nwritten)
+ }
+
+ #[inline]
+ fn is_read_vectored(&self) -> bool {
+ true
+ }
+}
+
+impl SizeHint for Repeat {
+ #[inline]
+ fn lower_bound(&self) -> usize {
+ usize::MAX
+ }
+
+ #[inline]
+ fn upper_bound(&self) -> Option<usize> {
+ None
+ }
+}
+
+#[stable(feature = "std_debug", since = "1.16.0")]
+impl fmt::Debug for Repeat {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("Repeat").finish_non_exhaustive()
+ }
+}
+
+/// A writer which will move data into the void.
+///
+/// This struct is generally created by calling [`sink`]. Please
+/// see the documentation of [`sink()`] for more details.
+#[stable(feature = "rust1", since = "1.0.0")]
+#[non_exhaustive]
+#[derive(Copy, Clone, Default)]
+pub struct Sink;
+
+/// Creates an instance of a writer which will successfully consume all data.
+///
+/// All calls to [`write`] on the returned instance will return `Ok(buf.len())`
+/// and the contents of the buffer will not be inspected.
+///
+/// [`write`]: Write::write
+///
+/// # Examples
+///
+/// ```rust
+/// use std::io::{self, Write};
+///
+/// let buffer = vec![1, 2, 3, 5, 8];
+/// let num_bytes = io::sink().write(&buffer).unwrap();
+/// assert_eq!(num_bytes, 5);
+/// ```
+#[must_use]
+#[stable(feature = "rust1", since = "1.0.0")]
+#[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
+pub const fn sink() -> Sink {
+ Sink
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Write for Sink {
+ #[inline]
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ Ok(buf.len())
+ }
+
+ #[inline]
+ fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+ let total_len = bufs.iter().map(|b| b.len()).sum();
+ Ok(total_len)
+ }
+
+ #[inline]
+ fn is_write_vectored(&self) -> bool {
+ true
+ }
+
+ #[inline]
+ fn flush(&mut self) -> io::Result<()> {
+ Ok(())
+ }
+}
+
+#[stable(feature = "write_mt", since = "1.48.0")]
+impl Write for &Sink {
+ #[inline]
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ Ok(buf.len())
+ }
+
+ #[inline]
+ fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+ let total_len = bufs.iter().map(|b| b.len()).sum();
+ Ok(total_len)
+ }
+
+ #[inline]
+ fn is_write_vectored(&self) -> bool {
+ true
+ }
+
+ #[inline]
+ fn flush(&mut self) -> io::Result<()> {
+ Ok(())
+ }
+}
+
+#[stable(feature = "std_debug", since = "1.16.0")]
+impl fmt::Debug for Sink {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("Sink").finish_non_exhaustive()
+ }
+}
diff --git a/library/std/src/io/util/tests.rs b/library/std/src/io/util/tests.rs
new file mode 100644
index 000000000..08972a59a
--- /dev/null
+++ b/library/std/src/io/util/tests.rs
@@ -0,0 +1,147 @@
+use crate::cmp::{max, min};
+use crate::io::prelude::*;
+use crate::io::{
+ copy, empty, repeat, sink, BufWriter, Empty, ReadBuf, Repeat, Result, SeekFrom, Sink,
+ DEFAULT_BUF_SIZE,
+};
+
+use crate::mem::MaybeUninit;
+
+#[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);
+ assert_eq!(copy(&mut r as &mut dyn Read, &mut w as &mut dyn Write).unwrap(), 1 << 17);
+}
+
+struct ShortReader {
+ cap: usize,
+ read_size: usize,
+ observed_buffer: usize,
+}
+
+impl Read for ShortReader {
+ fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
+ let bytes = min(self.cap, self.read_size);
+ self.cap -= bytes;
+ self.observed_buffer = max(self.observed_buffer, buf.len());
+ Ok(bytes)
+ }
+}
+
+struct WriteObserver {
+ observed_buffer: usize,
+}
+
+impl Write for WriteObserver {
+ fn write(&mut self, buf: &[u8]) -> Result<usize> {
+ self.observed_buffer = max(self.observed_buffer, buf.len());
+ Ok(buf.len())
+ }
+
+ fn flush(&mut self) -> Result<()> {
+ Ok(())
+ }
+}
+
+#[test]
+fn copy_specializes_bufwriter() {
+ let cap = 117 * 1024;
+ let buf_sz = 16 * 1024;
+ let mut r = ShortReader { cap, observed_buffer: 0, read_size: 1337 };
+ let mut w = BufWriter::with_capacity(buf_sz, WriteObserver { observed_buffer: 0 });
+ assert_eq!(
+ copy(&mut r, &mut w).unwrap(),
+ cap as u64,
+ "expected the whole capacity to be copied"
+ );
+ assert_eq!(r.observed_buffer, buf_sz, "expected a large buffer to be provided to the reader");
+ assert!(w.get_mut().observed_buffer > DEFAULT_BUF_SIZE, "expected coalesced writes");
+}
+
+#[test]
+fn sink_sinks() {
+ let mut s = sink();
+ 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);
+}
+
+#[test]
+fn empty_reads() {
+ let mut e = empty();
+ 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);
+
+ let mut buf = [];
+ let mut buf = ReadBuf::uninit(&mut buf);
+ e.read_buf(&mut buf).unwrap();
+ assert_eq!(buf.filled_len(), 0);
+ assert_eq!(buf.initialized_len(), 0);
+
+ let mut buf = [MaybeUninit::uninit()];
+ let mut buf = ReadBuf::uninit(&mut buf);
+ e.read_buf(&mut buf).unwrap();
+ assert_eq!(buf.filled_len(), 0);
+ assert_eq!(buf.initialized_len(), 0);
+
+ let mut buf = [MaybeUninit::uninit(); 1024];
+ let mut buf = ReadBuf::uninit(&mut buf);
+ e.read_buf(&mut buf).unwrap();
+ assert_eq!(buf.filled_len(), 0);
+ assert_eq!(buf.initialized_len(), 0);
+
+ let mut buf = [MaybeUninit::uninit(); 1024];
+ let mut buf = ReadBuf::uninit(&mut buf);
+ e.by_ref().read_buf(&mut buf).unwrap();
+ assert_eq!(buf.filled_len(), 0);
+ assert_eq!(buf.initialized_len(), 0);
+}
+
+#[test]
+fn empty_seeks() {
+ let mut e = empty();
+ assert!(matches!(e.seek(SeekFrom::Start(0)), Ok(0)));
+ assert!(matches!(e.seek(SeekFrom::Start(1)), Ok(0)));
+ assert!(matches!(e.seek(SeekFrom::Start(u64::MAX)), Ok(0)));
+
+ assert!(matches!(e.seek(SeekFrom::End(i64::MIN)), Ok(0)));
+ assert!(matches!(e.seek(SeekFrom::End(-1)), Ok(0)));
+ assert!(matches!(e.seek(SeekFrom::End(0)), Ok(0)));
+ assert!(matches!(e.seek(SeekFrom::End(1)), Ok(0)));
+ assert!(matches!(e.seek(SeekFrom::End(i64::MAX)), Ok(0)));
+
+ assert!(matches!(e.seek(SeekFrom::Current(i64::MIN)), Ok(0)));
+ assert!(matches!(e.seek(SeekFrom::Current(-1)), Ok(0)));
+ assert!(matches!(e.seek(SeekFrom::Current(0)), Ok(0)));
+ assert!(matches!(e.seek(SeekFrom::Current(1)), Ok(0)));
+ assert!(matches!(e.seek(SeekFrom::Current(i64::MAX)), Ok(0)));
+}
+
+#[test]
+fn repeat_repeats() {
+ let mut r = repeat(4);
+ let mut b = [0; 1024];
+ assert_eq!(r.read(&mut b).unwrap(), 1024);
+ assert!(b.iter().all(|b| *b == 4));
+}
+
+#[test]
+fn take_some_bytes() {
+ assert_eq!(repeat(4).take(100).bytes().count(), 100);
+ assert_eq!(repeat(4).take(100).bytes().next().unwrap().unwrap(), 4);
+ assert_eq!(repeat(1).take(10).chain(repeat(2).take(10)).bytes().count(), 20);
+}
+
+#[allow(dead_code)]
+fn const_utils() {
+ const _: Empty = empty();
+ const _: Repeat = repeat(b'c');
+ const _: Sink = sink();
+}