Skip to main content

std/io/
mod.rs

1//! Traits, helpers, and type definitions for core I/O functionality.
2//!
3//! The `std::io` module contains a number of common things you'll need
4//! when doing input and output. The most core part of this module is
5//! the [`Read`] and [`Write`] traits, which provide the
6//! most general interface for reading and writing input and output.
7//!
8//! ## Read and Write
9//!
10//! Because they are traits, [`Read`] and [`Write`] are implemented by a number
11//! of other types, and you can implement them for your types too. As such,
12//! you'll see a few different types of I/O throughout the documentation in
13//! this module: [`File`]s, [`TcpStream`]s, and sometimes even [`Vec<T>`]s. For
14//! example, [`Read`] adds a [`read`][`Read::read`] method, which we can use on
15//! [`File`]s:
16//!
17//! ```no_run
18//! use std::io;
19//! use std::io::prelude::*;
20//! use std::fs::File;
21//!
22//! fn main() -> io::Result<()> {
23//!     let mut f = File::open("foo.txt")?;
24//!     let mut buffer = [0; 10];
25//!
26//!     // read up to 10 bytes
27//!     let n = f.read(&mut buffer)?;
28//!
29//!     println!("The bytes: {:?}", &buffer[..n]);
30//!     Ok(())
31//! }
32//! ```
33//!
34//! [`Read`] and [`Write`] are so important, implementors of the two traits have a
35//! nickname: readers and writers. So you'll sometimes see 'a reader' instead
36//! of 'a type that implements the [`Read`] trait'. Much easier!
37//!
38//! ## Seek and BufRead
39//!
40//! Beyond that, there are two important traits that are provided: [`Seek`]
41//! and [`BufRead`]. Both of these build on top of a reader to control
42//! how the reading happens. [`Seek`] lets you control where the next byte is
43//! coming from:
44//!
45//! ```no_run
46//! use std::io;
47//! use std::io::prelude::*;
48//! use std::io::SeekFrom;
49//! use std::fs::File;
50//!
51//! fn main() -> io::Result<()> {
52//!     let mut f = File::open("foo.txt")?;
53//!     let mut buffer = [0; 10];
54//!
55//!     // skip to the last 10 bytes of the file
56//!     f.seek(SeekFrom::End(-10))?;
57//!
58//!     // read up to 10 bytes
59//!     let n = f.read(&mut buffer)?;
60//!
61//!     println!("The bytes: {:?}", &buffer[..n]);
62//!     Ok(())
63//! }
64//! ```
65//!
66//! [`BufRead`] uses an internal buffer to provide a number of other ways to read, but
67//! to show it off, we'll need to talk about buffers in general. Keep reading!
68//!
69//! ## BufReader and BufWriter
70//!
71//! Byte-based interfaces are unwieldy and can be inefficient, as we'd need to be
72//! making near-constant calls to the operating system. To help with this,
73//! `std::io` comes with two structs, [`BufReader`] and [`BufWriter`], which wrap
74//! readers and writers. The wrapper uses a buffer, reducing the number of
75//! calls and providing nicer methods for accessing exactly what you want.
76//!
77//! For example, [`BufReader`] works with the [`BufRead`] trait to add extra
78//! methods to any reader:
79//!
80//! ```no_run
81//! use std::io;
82//! use std::io::prelude::*;
83//! use std::io::BufReader;
84//! use std::fs::File;
85//!
86//! fn main() -> io::Result<()> {
87//!     let f = File::open("foo.txt")?;
88//!     let mut reader = BufReader::new(f);
89//!     let mut buffer = String::new();
90//!
91//!     // read a line into buffer
92//!     reader.read_line(&mut buffer)?;
93//!
94//!     println!("{buffer}");
95//!     Ok(())
96//! }
97//! ```
98//!
99//! [`BufWriter`] doesn't add any new ways of writing; it just buffers every call
100//! to [`write`][`Write::write`]:
101//!
102//! ```no_run
103//! use std::io;
104//! use std::io::prelude::*;
105//! use std::io::BufWriter;
106//! use std::fs::File;
107//!
108//! fn main() -> io::Result<()> {
109//!     let f = File::create("foo.txt")?;
110//!     {
111//!         let mut writer = BufWriter::new(f);
112//!
113//!         // write a byte to the buffer
114//!         writer.write(&[42])?;
115//!
116//!     } // the buffer is flushed once writer goes out of scope
117//!
118//!     Ok(())
119//! }
120//! ```
121//!
122//! ## Standard input and output
123//!
124//! A very common source of input is standard input:
125//!
126//! ```no_run
127//! use std::io;
128//!
129//! fn main() -> io::Result<()> {
130//!     let mut input = String::new();
131//!
132//!     io::stdin().read_line(&mut input)?;
133//!
134//!     println!("You typed: {}", input.trim());
135//!     Ok(())
136//! }
137//! ```
138//!
139//! Note that you cannot use the [`?` operator] in functions that do not return
140//! a [`Result<T, E>`][`Result`]. Instead, you can call [`.unwrap()`]
141//! or `match` on the return value to catch any possible errors:
142//!
143//! ```no_run
144//! use std::io;
145//!
146//! let mut input = String::new();
147//!
148//! io::stdin().read_line(&mut input).unwrap();
149//! ```
150//!
151//! And a very common source of output is standard output:
152//!
153//! ```no_run
154//! use std::io;
155//! use std::io::prelude::*;
156//!
157//! fn main() -> io::Result<()> {
158//!     io::stdout().write(&[42])?;
159//!     Ok(())
160//! }
161//! ```
162//!
163//! Of course, using [`io::stdout`] directly is less common than something like
164//! [`println!`].
165//!
166//! ## Iterator types
167//!
168//! A large number of the structures provided by `std::io` are for various
169//! ways of iterating over I/O. For example, [`Lines`] is used to split over
170//! lines:
171//!
172//! ```no_run
173//! use std::io;
174//! use std::io::prelude::*;
175//! use std::io::BufReader;
176//! use std::fs::File;
177//!
178//! fn main() -> io::Result<()> {
179//!     let f = File::open("foo.txt")?;
180//!     let reader = BufReader::new(f);
181//!
182//!     for line in reader.lines() {
183//!         println!("{}", line?);
184//!     }
185//!     Ok(())
186//! }
187//! ```
188//!
189//! ## Functions
190//!
191//! There are a number of [functions][functions-list] that offer access to various
192//! features. For example, we can use three of these functions to copy everything
193//! from standard input to standard output:
194//!
195//! ```no_run
196//! use std::io;
197//!
198//! fn main() -> io::Result<()> {
199//!     io::copy(&mut io::stdin(), &mut io::stdout())?;
200//!     Ok(())
201//! }
202//! ```
203//!
204//! [functions-list]: #functions-1
205//!
206//! ## io::Result
207//!
208//! Last, but certainly not least, is [`io::Result`]. This type is used
209//! as the return type of many `std::io` functions that can cause an error, and
210//! can be returned from your own functions as well. Many of the examples in this
211//! module use the [`?` operator]:
212//!
213//! ```
214//! use std::io;
215//!
216//! fn read_input() -> io::Result<()> {
217//!     let mut input = String::new();
218//!
219//!     io::stdin().read_line(&mut input)?;
220//!
221//!     println!("You typed: {}", input.trim());
222//!
223//!     Ok(())
224//! }
225//! ```
226//!
227//! The return type of `read_input()`, [`io::Result<()>`][`io::Result`], is a very
228//! common type for functions which don't have a 'real' return value, but do want to
229//! return errors if they happen. In this case, the only purpose of this function is
230//! to read the line and print it, so we use `()`.
231//!
232//! ## Platform-specific behavior
233//!
234//! Many I/O functions throughout the standard library are documented to indicate
235//! what various library or syscalls they are delegated to. This is done to help
236//! applications both understand what's happening under the hood as well as investigate
237//! any possibly unclear semantics. Note, however, that this is informative, not a binding
238//! contract. The implementation of many of these functions are subject to change over
239//! time and may call fewer or more syscalls/library functions.
240//!
241//! ## I/O Safety
242//!
243//! Rust follows an I/O safety discipline that is comparable to its memory safety discipline. This
244//! means that file descriptors can be *exclusively owned*. (Here, "file descriptor" is meant to
245//! subsume similar concepts that exist across a wide range of operating systems even if they might
246//! use a different name, such as "handle".) An exclusively owned file descriptor is one that no
247//! other code is allowed to access in any way, but the owner is allowed to access and even close
248//! it any time. A type that owns its file descriptor should usually close it in its `drop`
249//! function. Types like [`File`] own their file descriptor. Similarly, file descriptors
250//! can be *borrowed*, granting the temporary right to perform operations on this file descriptor.
251//! This indicates that the file descriptor will not be closed for the lifetime of the borrow, but
252//! it does *not* imply any right to close this file descriptor, since it will likely be owned by
253//! someone else.
254//!
255//! The platform-specific parts of the Rust standard library expose types that reflect these
256//! concepts, see [`os::unix`] and [`os::windows`].
257//!
258//! To uphold I/O safety, it is crucial that no code acts on file descriptors it does not own or
259//! borrow, and no code closes file descriptors it does not own. In other words, a safe function
260//! that takes a regular integer, treats it as a file descriptor, and acts on it, is *unsound*.
261//!
262//! Not upholding I/O safety and acting on a file descriptor without proof of ownership can lead to
263//! misbehavior and even Undefined Behavior in code that relies on ownership of its file
264//! descriptors: a closed file descriptor could be re-allocated, so the original owner of that file
265//! descriptor is now working on the wrong file. Some code might even rely on fully encapsulating
266//! its file descriptors with no operations being performed by any other part of the program.
267//!
268//! Note that exclusive ownership of a file descriptor does *not* imply exclusive ownership of the
269//! underlying kernel object that the file descriptor references (also called "open file description" on
270//! some operating systems). File descriptors basically work like [`Arc`]: when you receive an owned
271//! file descriptor, you cannot know whether there are any other file descriptors that reference the
272//! same kernel object. However, when you create a new kernel object, you know that you are holding
273//! the only reference to it. Just be careful not to lend it to anyone, since they can obtain a
274//! clone and then you can no longer know what the reference count is! In that sense, [`OwnedFd`] is
275//! like `Arc` and [`BorrowedFd<'a>`] is like `&'a Arc` (and similar for the Windows types). In
276//! particular, given a `BorrowedFd<'a>`, you are not allowed to close the file descriptor -- just
277//! like how, given a `&'a Arc`, you are not allowed to decrement the reference count and
278//! potentially free the underlying object. There is no equivalent to `Box` for file descriptors in
279//! the standard library (that would be a type that guarantees that the reference count is `1`),
280//! however, it would be possible for a crate to define a type with those semantics.
281//!
282//! [`File`]: crate::fs::File
283//! [`TcpStream`]: crate::net::TcpStream
284//! [`io::stdout`]: stdout
285//! [`io::Result`]: self::Result
286//! [`?` operator]: ../../book/appendix-02-operators.html
287//! [`Result`]: crate::result::Result
288//! [`.unwrap()`]: crate::result::Result::unwrap
289//! [`os::unix`]: ../os/unix/io/index.html
290//! [`os::windows`]: ../os/windows/io/index.html
291//! [`OwnedFd`]: ../os/fd/struct.OwnedFd.html
292//! [`BorrowedFd<'a>`]: ../os/fd/struct.BorrowedFd.html
293//! [`Arc`]: crate::sync::Arc
294
295#![stable(feature = "rust1", since = "1.0.0")]
296
297#[cfg(test)]
298mod tests;
299
300use core::slice::memchr;
301
302use alloc_crate::io::OsFunctions;
303#[unstable(feature = "raw_os_error_ty", issue = "107792")]
304pub use alloc_crate::io::RawOsError;
305#[doc(hidden)]
306#[unstable(feature = "io_const_error_internals", issue = "none")]
307pub use alloc_crate::io::SimpleMessage;
308#[unstable(feature = "io_const_error", issue = "133448")]
309pub use alloc_crate::io::const_error;
310#[unstable(feature = "read_buf", issue = "78485")]
311pub use alloc_crate::io::{BorrowedBuf, BorrowedCursor};
312#[stable(feature = "rust1", since = "1.0.0")]
313pub use alloc_crate::io::{
314    Chain, Empty, Error, ErrorKind, Repeat, Result, Sink, Take, empty, repeat, sink,
315};
316#[stable(feature = "iovec", since = "1.36.0")]
317pub use alloc_crate::io::{IoSlice, IoSliceMut};
318
319#[stable(feature = "bufwriter_into_parts", since = "1.56.0")]
320pub use self::buffered::WriterPanicked;
321#[stable(feature = "anonymous_pipe", since = "1.87.0")]
322pub use self::pipe::{PipeReader, PipeWriter, pipe};
323#[stable(feature = "is_terminal", since = "1.70.0")]
324pub use self::stdio::IsTerminal;
325pub(crate) use self::stdio::attempt_print_to_stderr;
326#[unstable(feature = "print_internals", issue = "none")]
327#[doc(hidden)]
328pub use self::stdio::{_eprint, _print};
329#[unstable(feature = "internal_output_capture", issue = "none")]
330#[doc(no_inline, hidden)]
331pub use self::stdio::{set_output_capture, try_set_output_capture};
332#[stable(feature = "rust1", since = "1.0.0")]
333pub use self::{
334    buffered::{BufReader, BufWriter, IntoInnerError, LineWriter},
335    copy::copy,
336    cursor::Cursor,
337    stdio::{Stderr, StderrLock, Stdin, StdinLock, Stdout, StdoutLock, stderr, stdin, stdout},
338};
339use crate::mem::MaybeUninit;
340use crate::{cmp, fmt, slice, str};
341
342mod buffered;
343pub(crate) mod copy;
344mod cursor;
345mod error;
346mod impls;
347mod pipe;
348pub mod prelude;
349mod stdio;
350mod util;
351
352const DEFAULT_BUF_SIZE: usize = crate::sys::io::DEFAULT_BUF_SIZE;
353
354pub(crate) use stdio::cleanup;
355
356struct Guard<'a> {
357    buf: &'a mut Vec<u8>,
358    len: usize,
359}
360
361impl Drop for Guard<'_> {
362    fn drop(&mut self) {
363        unsafe {
364            self.buf.set_len(self.len);
365        }
366    }
367}
368
369// Several `read_to_string` and `read_line` methods in the standard library will
370// append data into a `String` buffer, but we need to be pretty careful when
371// doing this. The implementation will just call `.as_mut_vec()` and then
372// delegate to a byte-oriented reading method, but we must ensure that when
373// returning we never leave `buf` in a state such that it contains invalid UTF-8
374// in its bounds.
375//
376// To this end, we use an RAII guard (to protect against panics) which updates
377// the length of the string when it is dropped. This guard initially truncates
378// the string to the prior length and only after we've validated that the
379// new contents are valid UTF-8 do we allow it to set a longer length.
380//
381// The unsafety in this function is twofold:
382//
383// 1. We're looking at the raw bytes of `buf`, so we take on the burden of UTF-8
384//    checks.
385// 2. We're passing a raw buffer to the function `f`, and it is expected that
386//    the function only *appends* bytes to the buffer. We'll get undefined
387//    behavior if existing bytes are overwritten to have non-UTF-8 data.
388pub(crate) unsafe fn append_to_string<F>(buf: &mut String, f: F) -> Result<usize>
389where
390    F: FnOnce(&mut Vec<u8>) -> Result<usize>,
391{
392    let mut g = Guard { len: buf.len(), buf: unsafe { buf.as_mut_vec() } };
393    let ret = f(g.buf);
394
395    // SAFETY: the caller promises to only append data to `buf`
396    let appended = unsafe { g.buf.get_unchecked(g.len..) };
397    if str::from_utf8(appended).is_err() {
398        ret.and_then(|_| Err(Error::INVALID_UTF8))
399    } else {
400        g.len = g.buf.len();
401        ret
402    }
403}
404
405// Here we must serve many masters with conflicting goals:
406//
407// - avoid allocating unless necessary
408// - avoid overallocating if we know the exact size (#89165)
409// - avoid passing large buffers to readers that always initialize the free capacity if they perform short reads (#23815, #23820)
410// - pass large buffers to readers that do not initialize the spare capacity. this can amortize per-call overheads
411// - and finally pass not-too-small and not-too-large buffers to Windows read APIs because they manage to suffer from both problems
412//   at the same time, i.e. small reads suffer from syscall overhead, all reads incur costs proportional to buffer size (#110650)
413//
414pub(crate) fn default_read_to_end<R: Read + ?Sized>(
415    r: &mut R,
416    buf: &mut Vec<u8>,
417    size_hint: Option<usize>,
418) -> Result<usize> {
419    let start_len = buf.len();
420    let start_cap = buf.capacity();
421    // Optionally limit the maximum bytes read on each iteration.
422    // This adds an arbitrary fiddle factor to allow for more data than we expect.
423    let mut max_read_size = size_hint
424        .and_then(|s| s.checked_add(1024)?.checked_next_multiple_of(DEFAULT_BUF_SIZE))
425        .unwrap_or(DEFAULT_BUF_SIZE);
426
427    const PROBE_SIZE: usize = 32;
428
429    fn small_probe_read<R: Read + ?Sized>(r: &mut R, buf: &mut Vec<u8>) -> Result<usize> {
430        let mut probe = [0u8; PROBE_SIZE];
431
432        loop {
433            match r.read(&mut probe) {
434                Ok(n) => {
435                    // there is no way to recover from allocation failure here
436                    // because the data has already been read.
437                    buf.extend_from_slice(&probe[..n]);
438                    return Ok(n);
439                }
440                Err(ref e) if e.is_interrupted() => continue,
441                Err(e) => return Err(e),
442            }
443        }
444    }
445
446    // avoid inflating empty/small vecs before we have determined that there's anything to read
447    if (size_hint.is_none() || size_hint == Some(0)) && buf.capacity() - buf.len() < PROBE_SIZE {
448        let read = small_probe_read(r, buf)?;
449
450        if read == 0 {
451            return Ok(0);
452        }
453    }
454
455    loop {
456        if buf.len() == buf.capacity() && buf.capacity() == start_cap {
457            // The buffer might be an exact fit. Let's read into a probe buffer
458            // and see if it returns `Ok(0)`. If so, we've avoided an
459            // unnecessary doubling of the capacity. But if not, append the
460            // probe buffer to the primary buffer and let its capacity grow.
461            let read = small_probe_read(r, buf)?;
462
463            if read == 0 {
464                return Ok(buf.len() - start_len);
465            }
466        }
467
468        if buf.len() == buf.capacity() {
469            // buf is full, need more space
470            buf.try_reserve(PROBE_SIZE)?;
471        }
472
473        let mut spare = buf.spare_capacity_mut();
474        let buf_len = cmp::min(spare.len(), max_read_size);
475        spare = &mut spare[..buf_len];
476        let mut read_buf: BorrowedBuf<'_, u8> = spare.into();
477
478        // Note that we don't track already initialized bytes here, but this is fine
479        // because we explicitly limit the read size
480        let mut cursor = read_buf.unfilled();
481        let result = loop {
482            match r.read_buf(cursor.reborrow()) {
483                Err(e) if e.is_interrupted() => continue,
484                // Do not stop now in case of error: we might have received both data
485                // and an error
486                res => break res,
487            }
488        };
489
490        let bytes_read = cursor.written();
491        let is_init = read_buf.is_init();
492
493        // SAFETY: BorrowedBuf's invariants mean this much memory is initialized.
494        unsafe {
495            let new_len = bytes_read + buf.len();
496            buf.set_len(new_len);
497        }
498
499        // Now that all data is pushed to the vector, we can fail without data loss
500        result?;
501
502        if bytes_read == 0 {
503            return Ok(buf.len() - start_len);
504        }
505
506        // Use heuristics to determine the max read size if no initial size hint was provided
507        if size_hint.is_none() {
508            // The reader is returning short reads but it doesn't call ensure_init().
509            // In that case we no longer need to restrict read sizes to avoid
510            // initialization costs.
511            // When reading from disk we usually don't get any short reads except at EOF.
512            // So we wait for at least 2 short reads before uncapping the read buffer;
513            // this helps with the Windows issue.
514            if !is_init {
515                max_read_size = usize::MAX;
516            }
517            // we have passed a larger buffer than previously and the
518            // reader still hasn't returned a short read
519            else if buf_len >= max_read_size && bytes_read == buf_len {
520                max_read_size = max_read_size.saturating_mul(2);
521            }
522        }
523    }
524}
525
526pub(crate) fn default_read_to_string<R: Read + ?Sized>(
527    r: &mut R,
528    buf: &mut String,
529    size_hint: Option<usize>,
530) -> Result<usize> {
531    // Note that we do *not* call `r.read_to_end()` here. We are passing
532    // `&mut Vec<u8>` (the raw contents of `buf`) into the `read_to_end`
533    // method to fill it up. An arbitrary implementation could overwrite the
534    // entire contents of the vector, not just append to it (which is what
535    // we are expecting).
536    //
537    // To prevent extraneously checking the UTF-8-ness of the entire buffer
538    // we pass it to our hardcoded `default_read_to_end` implementation which
539    // we know is guaranteed to only read data into the end of the buffer.
540    unsafe { append_to_string(buf, |b| default_read_to_end(r, b, size_hint)) }
541}
542
543pub(crate) fn default_read_vectored<F>(read: F, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
544where
545    F: FnOnce(&mut [u8]) -> Result<usize>,
546{
547    let buf = bufs.iter_mut().find(|b| !b.is_empty()).map_or(&mut [][..], |b| &mut **b);
548    read(buf)
549}
550
551pub(crate) fn default_write_vectored<F>(write: F, bufs: &[IoSlice<'_>]) -> Result<usize>
552where
553    F: FnOnce(&[u8]) -> Result<usize>,
554{
555    let buf = bufs.iter().find(|b| !b.is_empty()).map_or(&[][..], |b| &**b);
556    write(buf)
557}
558
559pub(crate) fn default_read_exact<R: Read + ?Sized>(this: &mut R, mut buf: &mut [u8]) -> Result<()> {
560    while !buf.is_empty() {
561        match this.read(buf) {
562            Ok(0) => break,
563            Ok(n) => {
564                buf = &mut buf[n..];
565            }
566            Err(ref e) if e.is_interrupted() => {}
567            Err(e) => return Err(e),
568        }
569    }
570    if !buf.is_empty() { Err(Error::READ_EXACT_EOF) } else { Ok(()) }
571}
572
573pub(crate) fn default_read_buf<F>(read: F, mut cursor: BorrowedCursor<'_, u8>) -> Result<()>
574where
575    F: FnOnce(&mut [u8]) -> Result<usize>,
576{
577    let n = read(cursor.ensure_init())?;
578    cursor.advance_checked(n);
579    Ok(())
580}
581
582pub(crate) fn default_read_buf_exact<R: Read + ?Sized>(
583    this: &mut R,
584    mut cursor: BorrowedCursor<'_, u8>,
585) -> Result<()> {
586    while cursor.capacity() > 0 {
587        let prev_written = cursor.written();
588        match this.read_buf(cursor.reborrow()) {
589            Ok(()) => {}
590            Err(e) if e.is_interrupted() => continue,
591            Err(e) => return Err(e),
592        }
593
594        if cursor.written() == prev_written {
595            return Err(Error::READ_EXACT_EOF);
596        }
597    }
598
599    Ok(())
600}
601
602pub(crate) fn default_write_fmt<W: Write + ?Sized>(
603    this: &mut W,
604    args: fmt::Arguments<'_>,
605) -> Result<()> {
606    // Create a shim which translates a `Write` to a `fmt::Write` and saves off
607    // I/O errors, instead of discarding them.
608    struct Adapter<'a, T: ?Sized + 'a> {
609        inner: &'a mut T,
610        error: Result<()>,
611    }
612
613    impl<T: Write + ?Sized> fmt::Write for Adapter<'_, T> {
614        fn write_str(&mut self, s: &str) -> fmt::Result {
615            match self.inner.write_all(s.as_bytes()) {
616                Ok(()) => Ok(()),
617                Err(e) => {
618                    self.error = Err(e);
619                    Err(fmt::Error)
620                }
621            }
622        }
623    }
624
625    let mut output = Adapter { inner: this, error: Ok(()) };
626    match fmt::write(&mut output, args) {
627        Ok(()) => Ok(()),
628        Err(..) => {
629            // Check whether the error came from the underlying `Write`.
630            if output.error.is_err() {
631                output.error
632            } else {
633                // This shouldn't happen: the underlying stream did not error,
634                // but somehow the formatter still errored?
635                panic!(
636                    "a formatting trait implementation returned an error when the underlying stream did not"
637                );
638            }
639        }
640    }
641}
642
643/// The `Read` trait allows for reading bytes from a source.
644///
645/// Implementors of the `Read` trait are called 'readers'.
646///
647/// Readers are defined by one required method, [`read()`]. Each call to [`read()`]
648/// will attempt to pull bytes from this source into a provided buffer. A
649/// number of other methods are implemented in terms of [`read()`], giving
650/// implementors a number of ways to read bytes while only needing to implement
651/// a single method.
652///
653/// Readers are intended to be composable with one another. Many implementors
654/// throughout [`std::io`] take and provide types which implement the `Read`
655/// trait.
656///
657/// Please note that each call to [`read()`] may involve a system call, and
658/// therefore, using something that implements [`BufRead`], such as
659/// [`BufReader`], will be more efficient.
660///
661/// Repeated calls to the reader use the same cursor, so for example
662/// calling `read_to_end` twice on a [`File`] will only return the file's
663/// contents once. It's recommended to first call `rewind()` in that case.
664///
665/// # Examples
666///
667/// [`File`]s implement `Read`:
668///
669/// ```no_run
670/// use std::io;
671/// use std::io::prelude::*;
672/// use std::fs::File;
673///
674/// fn main() -> io::Result<()> {
675///     let mut f = File::open("foo.txt")?;
676///     let mut buffer = [0; 10];
677///
678///     // read up to 10 bytes
679///     f.read(&mut buffer)?;
680///
681///     let mut buffer = Vec::new();
682///     // read the whole file
683///     f.read_to_end(&mut buffer)?;
684///
685///     // read into a String, so that you don't need to do the conversion.
686///     let mut buffer = String::new();
687///     f.read_to_string(&mut buffer)?;
688///
689///     // and more! See the other methods for more details.
690///     Ok(())
691/// }
692/// ```
693///
694/// Read from [`&str`] because [`&[u8]`][prim@slice] implements `Read`:
695///
696/// ```no_run
697/// # use std::io;
698/// use std::io::prelude::*;
699///
700/// fn main() -> io::Result<()> {
701///     let mut b = "This string will be read".as_bytes();
702///     let mut buffer = [0; 10];
703///
704///     // read up to 10 bytes
705///     b.read(&mut buffer)?;
706///
707///     // etc... it works exactly as a File does!
708///     Ok(())
709/// }
710/// ```
711///
712/// [`read()`]: Read::read
713/// [`&str`]: prim@str
714/// [`std::io`]: self
715/// [`File`]: crate::fs::File
716#[stable(feature = "rust1", since = "1.0.0")]
717#[doc(notable_trait)]
718#[cfg_attr(not(test), rustc_diagnostic_item = "IoRead")]
719pub trait Read {
720    /// Pull some bytes from this source into the specified buffer, returning
721    /// how many bytes were read.
722    ///
723    /// This function does not provide any guarantees about whether it blocks
724    /// waiting for data, but if an object needs to block for a read and cannot,
725    /// it will typically signal this via an [`Err`] return value.
726    ///
727    /// If the return value of this method is [`Ok(n)`], then implementations must
728    /// guarantee that `0 <= n <= buf.len()`. A nonzero `n` value indicates
729    /// that the buffer `buf` has been filled in with `n` bytes of data from this
730    /// source. If `n` is `0`, then it can indicate one of two scenarios:
731    ///
732    /// 1. This reader has reached its "end of file" and will likely no longer
733    ///    be able to produce bytes. Note that this does not mean that the
734    ///    reader will *always* no longer be able to produce bytes. As an example,
735    ///    on Linux, this method will call the `recv` syscall for a [`TcpStream`],
736    ///    where returning zero indicates the connection was shut down correctly. While
737    ///    for [`File`], it is possible to reach the end of file and get zero as result,
738    ///    but if more data is appended to the file, future calls to `read` will return
739    ///    more data.
740    /// 2. The buffer specified was 0 bytes in length.
741    ///
742    /// It is not an error if the returned value `n` is smaller than the buffer size,
743    /// even when the reader is not at the end of the stream yet.
744    /// This may happen for example because fewer bytes are actually available right now
745    /// (e. g. being close to end-of-file) or because read() was interrupted by a signal.
746    ///
747    /// As this trait is safe to implement, callers in unsafe code cannot rely on
748    /// `n <= buf.len()` for safety.
749    /// Extra care needs to be taken when `unsafe` functions are used to access the read bytes.
750    /// Callers have to ensure that no unchecked out-of-bounds accesses are possible even if
751    /// `n > buf.len()`.
752    ///
753    /// *Implementations* of this method can make no assumptions about the contents of `buf` when
754    /// this function is called. It is recommended that implementations only write data to `buf`
755    /// instead of reading its contents.
756    ///
757    /// Correspondingly, however, *callers* of this method in unsafe code must not assume
758    /// any guarantees about how the implementation uses `buf`. The trait is safe to implement,
759    /// so it is possible that the code that's supposed to write to the buffer might also read
760    /// from it. It is your responsibility to make sure that `buf` is initialized
761    /// before calling `read`. Calling `read` with an uninitialized `buf` (of the kind one
762    /// obtains via [`MaybeUninit<T>`]) is not safe, and can lead to undefined behavior.
763    ///
764    /// [`MaybeUninit<T>`]: crate::mem::MaybeUninit
765    ///
766    /// # Errors
767    ///
768    /// If this function encounters any form of I/O or other error, an error
769    /// variant will be returned. If an error is returned then it must be
770    /// guaranteed that no bytes were read.
771    ///
772    /// An error of the [`ErrorKind::Interrupted`] kind is non-fatal and the read
773    /// operation should be retried if there is nothing else to do.
774    ///
775    /// # Examples
776    ///
777    /// [`File`]s implement `Read`:
778    ///
779    /// [`Ok(n)`]: Ok
780    /// [`File`]: crate::fs::File
781    /// [`TcpStream`]: crate::net::TcpStream
782    ///
783    /// ```no_run
784    /// use std::io;
785    /// use std::io::prelude::*;
786    /// use std::fs::File;
787    ///
788    /// fn main() -> io::Result<()> {
789    ///     let mut f = File::open("foo.txt")?;
790    ///     let mut buffer = [0; 10];
791    ///
792    ///     // read up to 10 bytes
793    ///     let n = f.read(&mut buffer[..])?;
794    ///
795    ///     println!("The bytes: {:?}", &buffer[..n]);
796    ///     Ok(())
797    /// }
798    /// ```
799    #[stable(feature = "rust1", since = "1.0.0")]
800    fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
801
802    /// Like `read`, except that it reads into a slice of buffers.
803    ///
804    /// Data is copied to fill each buffer in order, with the final buffer
805    /// written to possibly being only partially filled. This method must
806    /// behave equivalently to a single call to `read` with concatenated
807    /// buffers.
808    ///
809    /// The default implementation calls `read` with either the first nonempty
810    /// buffer provided, or an empty one if none exists.
811    #[stable(feature = "iovec", since = "1.36.0")]
812    fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize> {
813        default_read_vectored(|b| self.read(b), bufs)
814    }
815
816    /// Determines if this `Read`er has an efficient `read_vectored`
817    /// implementation.
818    ///
819    /// If a `Read`er does not override the default `read_vectored`
820    /// implementation, code using it may want to avoid the method all together
821    /// and coalesce writes into a single buffer for higher performance.
822    ///
823    /// The default implementation returns `false`.
824    #[unstable(feature = "can_vector", issue = "69941")]
825    fn is_read_vectored(&self) -> bool {
826        false
827    }
828
829    /// Reads all bytes until EOF in this source, placing them into `buf`.
830    ///
831    /// All bytes read from this source will be appended to the specified buffer
832    /// `buf`. This function will continuously call [`read()`] to append more data to
833    /// `buf` until [`read()`] returns either [`Ok(0)`] or an error of
834    /// non-[`ErrorKind::Interrupted`] kind.
835    ///
836    /// If successful, this function will return the total number of bytes read.
837    ///
838    /// # Errors
839    ///
840    /// If this function encounters an error of the kind
841    /// [`ErrorKind::Interrupted`] then the error is ignored and the operation
842    /// will continue.
843    ///
844    /// If any other read error is encountered then this function immediately
845    /// returns. Any bytes which have already been read will be appended to
846    /// `buf`.
847    ///
848    /// # Examples
849    ///
850    /// [`File`]s implement `Read`:
851    ///
852    /// [`read()`]: Read::read
853    /// [`Ok(0)`]: Ok
854    /// [`File`]: crate::fs::File
855    ///
856    /// ```no_run
857    /// use std::io;
858    /// use std::io::prelude::*;
859    /// use std::fs::File;
860    ///
861    /// fn main() -> io::Result<()> {
862    ///     let mut f = File::open("foo.txt")?;
863    ///     let mut buffer = Vec::new();
864    ///
865    ///     // read the whole file
866    ///     f.read_to_end(&mut buffer)?;
867    ///     Ok(())
868    /// }
869    /// ```
870    ///
871    /// (See also the [`std::fs::read`] convenience function for reading from a
872    /// file.)
873    ///
874    /// [`std::fs::read`]: crate::fs::read
875    ///
876    /// ## Implementing `read_to_end`
877    ///
878    /// When implementing the `io::Read` trait, it is recommended to allocate
879    /// memory using [`Vec::try_reserve`]. However, this behavior is not guaranteed
880    /// by all implementations, and `read_to_end` may not handle out-of-memory
881    /// situations gracefully.
882    ///
883    /// ```no_run
884    /// # use std::io::{self, BufRead};
885    /// # struct Example { example_datasource: io::Empty } impl Example {
886    /// # fn get_some_data_for_the_example(&self) -> &'static [u8] { &[] }
887    /// fn read_to_end(&mut self, dest_vec: &mut Vec<u8>) -> io::Result<usize> {
888    ///     let initial_vec_len = dest_vec.len();
889    ///     loop {
890    ///         let src_buf = self.example_datasource.fill_buf()?;
891    ///         if src_buf.is_empty() {
892    ///             break;
893    ///         }
894    ///         dest_vec.try_reserve(src_buf.len())?;
895    ///         dest_vec.extend_from_slice(src_buf);
896    ///
897    ///         // Any irreversible side effects should happen after `try_reserve` succeeds,
898    ///         // to avoid losing data on allocation error.
899    ///         let read = src_buf.len();
900    ///         self.example_datasource.consume(read);
901    ///     }
902    ///     Ok(dest_vec.len() - initial_vec_len)
903    /// }
904    /// # }
905    /// ```
906    ///
907    /// # Usage Notes
908    ///
909    /// `read_to_end` attempts to read a source until EOF, but many sources are continuous streams
910    /// that do not send EOF. In these cases, `read_to_end` will block indefinitely. Standard input
911    /// is one such stream which may be finite if piped, but is typically continuous. For example,
912    /// `cat file | my-rust-program` will correctly terminate with an `EOF` upon closure of cat.
913    /// Reading user input or running programs that remain open indefinitely will never terminate
914    /// the stream with `EOF` (e.g. `yes | my-rust-program`).
915    ///
916    /// Using `.lines()` with a [`BufReader`] or using [`read`] can provide a better solution
917    ///
918    ///[`read`]: Read::read
919    ///
920    /// [`Vec::try_reserve`]: crate::vec::Vec::try_reserve
921    #[stable(feature = "rust1", since = "1.0.0")]
922    fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
923        default_read_to_end(self, buf, None)
924    }
925
926    /// Reads all bytes until EOF in this source, appending them to `buf`.
927    ///
928    /// If successful, this function returns the number of bytes which were read
929    /// and appended to `buf`.
930    ///
931    /// # Errors
932    ///
933    /// If the data in this stream is *not* valid UTF-8 then an error is
934    /// returned and `buf` is unchanged.
935    ///
936    /// See [`read_to_end`] for other error semantics.
937    ///
938    /// [`read_to_end`]: Read::read_to_end
939    ///
940    /// # Examples
941    ///
942    /// [`File`]s implement `Read`:
943    ///
944    /// [`File`]: crate::fs::File
945    ///
946    /// ```no_run
947    /// use std::io;
948    /// use std::io::prelude::*;
949    /// use std::fs::File;
950    ///
951    /// fn main() -> io::Result<()> {
952    ///     let mut f = File::open("foo.txt")?;
953    ///     let mut buffer = String::new();
954    ///
955    ///     f.read_to_string(&mut buffer)?;
956    ///     Ok(())
957    /// }
958    /// ```
959    ///
960    /// (See also the [`std::fs::read_to_string`] convenience function for
961    /// reading from a file.)
962    ///
963    /// # Usage Notes
964    ///
965    /// `read_to_string` attempts to read a source until EOF, but many sources are continuous streams
966    /// that do not send EOF. In these cases, `read_to_string` will block indefinitely. Standard input
967    /// is one such stream which may be finite if piped, but is typically continuous. For example,
968    /// `cat file | my-rust-program` will correctly terminate with an `EOF` upon closure of cat.
969    /// Reading user input or running programs that remain open indefinitely will never terminate
970    /// the stream with `EOF` (e.g. `yes | my-rust-program`).
971    ///
972    /// Using `.lines()` with a [`BufReader`] or using [`read`] can provide a better solution
973    ///
974    ///[`read`]: Read::read
975    ///
976    /// [`std::fs::read_to_string`]: crate::fs::read_to_string
977    #[stable(feature = "rust1", since = "1.0.0")]
978    fn read_to_string(&mut self, buf: &mut String) -> Result<usize> {
979        default_read_to_string(self, buf, None)
980    }
981
982    /// Reads the exact number of bytes required to fill `buf`.
983    ///
984    /// This function reads as many bytes as necessary to completely fill the
985    /// specified buffer `buf`.
986    ///
987    /// *Implementations* of this method can make no assumptions about the contents of `buf` when
988    /// this function is called. It is recommended that implementations only write data to `buf`
989    /// instead of reading its contents. The documentation on [`read`] has a more detailed
990    /// explanation of this subject.
991    ///
992    /// # Errors
993    ///
994    /// If this function encounters an error of the kind
995    /// [`ErrorKind::Interrupted`] then the error is ignored and the operation
996    /// will continue.
997    ///
998    /// If this function encounters an "end of file" before completely filling
999    /// the buffer, it returns an error of the kind [`ErrorKind::UnexpectedEof`].
1000    /// The contents of `buf` are unspecified in this case.
1001    ///
1002    /// If any other read error is encountered then this function immediately
1003    /// returns. The contents of `buf` are unspecified in this case.
1004    ///
1005    /// If this function returns an error, it is unspecified how many bytes it
1006    /// has read, but it will never read more than would be necessary to
1007    /// completely fill the buffer.
1008    ///
1009    /// # Examples
1010    ///
1011    /// [`File`]s implement `Read`:
1012    ///
1013    /// [`read`]: Read::read
1014    /// [`File`]: crate::fs::File
1015    ///
1016    /// ```no_run
1017    /// use std::io;
1018    /// use std::io::prelude::*;
1019    /// use std::fs::File;
1020    ///
1021    /// fn main() -> io::Result<()> {
1022    ///     let mut f = File::open("foo.txt")?;
1023    ///     let mut buffer = [0; 10];
1024    ///
1025    ///     // read exactly 10 bytes
1026    ///     f.read_exact(&mut buffer)?;
1027    ///     Ok(())
1028    /// }
1029    /// ```
1030    #[stable(feature = "read_exact", since = "1.6.0")]
1031    fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
1032        default_read_exact(self, buf)
1033    }
1034
1035    /// Pull some bytes from this source into the specified buffer.
1036    ///
1037    /// This is equivalent to the [`read`](Read::read) method, except that it is passed a [`BorrowedCursor`] rather than `[u8]` to allow use
1038    /// with uninitialized buffers. The new data will be appended to any existing contents of `buf`.
1039    ///
1040    /// The default implementation delegates to `read`.
1041    ///
1042    /// This method makes it possible to return both data and an error but it is advised against.
1043    #[unstable(feature = "read_buf", issue = "78485")]
1044    fn read_buf(&mut self, buf: BorrowedCursor<'_, u8>) -> Result<()> {
1045        default_read_buf(|b| self.read(b), buf)
1046    }
1047
1048    /// Reads the exact number of bytes required to fill `cursor`.
1049    ///
1050    /// This is similar to the [`read_exact`](Read::read_exact) method, except
1051    /// that it is passed a [`BorrowedCursor`] rather than `[u8]` to allow use
1052    /// with uninitialized buffers.
1053    ///
1054    /// # Errors
1055    ///
1056    /// If this function encounters an error of the kind [`ErrorKind::Interrupted`]
1057    /// then the error is ignored and the operation will continue.
1058    ///
1059    /// If this function encounters an "end of file" before completely filling
1060    /// the buffer, it returns an error of the kind [`ErrorKind::UnexpectedEof`].
1061    ///
1062    /// If any other read error is encountered then this function immediately
1063    /// returns.
1064    ///
1065    /// If this function returns an error, all bytes read will be appended to `cursor`.
1066    #[unstable(feature = "read_buf", issue = "78485")]
1067    fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_, u8>) -> Result<()> {
1068        default_read_buf_exact(self, cursor)
1069    }
1070
1071    /// Creates a "by reference" adapter for this instance of `Read`.
1072    ///
1073    /// The returned adapter also implements `Read` and will simply borrow this
1074    /// current reader.
1075    ///
1076    /// # Examples
1077    ///
1078    /// [`File`]s implement `Read`:
1079    ///
1080    /// [`File`]: crate::fs::File
1081    ///
1082    /// ```no_run
1083    /// use std::io;
1084    /// use std::io::Read;
1085    /// use std::fs::File;
1086    ///
1087    /// fn main() -> io::Result<()> {
1088    ///     let mut f = File::open("foo.txt")?;
1089    ///     let mut buffer = Vec::new();
1090    ///     let mut other_buffer = Vec::new();
1091    ///
1092    ///     {
1093    ///         let reference = f.by_ref();
1094    ///
1095    ///         // read at most 5 bytes
1096    ///         reference.take(5).read_to_end(&mut buffer)?;
1097    ///
1098    ///     } // drop our &mut reference so we can use f again
1099    ///
1100    ///     // original file still usable, read the rest
1101    ///     f.read_to_end(&mut other_buffer)?;
1102    ///     Ok(())
1103    /// }
1104    /// ```
1105    #[stable(feature = "rust1", since = "1.0.0")]
1106    fn by_ref(&mut self) -> &mut Self
1107    where
1108        Self: Sized,
1109    {
1110        self
1111    }
1112
1113    /// Transforms this `Read` instance to an [`Iterator`] over its bytes.
1114    ///
1115    /// The returned type implements [`Iterator`] where the [`Item`] is
1116    /// <code>[Result]<[u8], [io::Error]></code>.
1117    /// The yielded item is [`Ok`] if a byte was successfully read and [`Err`]
1118    /// otherwise. EOF is mapped to returning [`None`] from this iterator.
1119    ///
1120    /// The default implementation calls `read` for each byte,
1121    /// which can be very inefficient for data that's not in memory,
1122    /// such as [`File`]. Consider using a [`BufReader`] in such cases.
1123    ///
1124    /// # Examples
1125    ///
1126    /// [`File`]s implement `Read`:
1127    ///
1128    /// [`Item`]: Iterator::Item
1129    /// [`File`]: crate::fs::File "fs::File"
1130    /// [Result]: crate::result::Result "Result"
1131    /// [io::Error]: self::Error "io::Error"
1132    ///
1133    /// ```no_run
1134    /// use std::io;
1135    /// use std::io::prelude::*;
1136    /// use std::io::BufReader;
1137    /// use std::fs::File;
1138    ///
1139    /// fn main() -> io::Result<()> {
1140    ///     let f = BufReader::new(File::open("foo.txt")?);
1141    ///
1142    ///     for byte in f.bytes() {
1143    ///         println!("{}", byte?);
1144    ///     }
1145    ///     Ok(())
1146    /// }
1147    /// ```
1148    #[stable(feature = "rust1", since = "1.0.0")]
1149    fn bytes(self) -> Bytes<Self>
1150    where
1151        Self: Sized,
1152    {
1153        Bytes { inner: self }
1154    }
1155
1156    /// Creates an adapter which will chain this stream with another.
1157    ///
1158    /// The returned `Read` instance will first read all bytes from this object
1159    /// until EOF is encountered. Afterwards the output is equivalent to the
1160    /// output of `next`.
1161    ///
1162    /// # Examples
1163    ///
1164    /// [`File`]s implement `Read`:
1165    ///
1166    /// [`File`]: crate::fs::File
1167    ///
1168    /// ```no_run
1169    /// use std::io;
1170    /// use std::io::prelude::*;
1171    /// use std::fs::File;
1172    ///
1173    /// fn main() -> io::Result<()> {
1174    ///     let f1 = File::open("foo.txt")?;
1175    ///     let f2 = File::open("bar.txt")?;
1176    ///
1177    ///     let mut handle = f1.chain(f2);
1178    ///     let mut buffer = String::new();
1179    ///
1180    ///     // read the value into a String. We could use any Read method here,
1181    ///     // this is just one example.
1182    ///     handle.read_to_string(&mut buffer)?;
1183    ///     Ok(())
1184    /// }
1185    /// ```
1186    #[stable(feature = "rust1", since = "1.0.0")]
1187    fn chain<R: Read>(self, next: R) -> Chain<Self, R>
1188    where
1189        Self: Sized,
1190    {
1191        core::io::chain(self, next)
1192    }
1193
1194    /// Creates an adapter which will read at most `limit` bytes from it.
1195    ///
1196    /// This function returns a new instance of `Read` which will read at most
1197    /// `limit` bytes, after which it will always return EOF ([`Ok(0)`]). Any
1198    /// read errors will not count towards the number of bytes read and future
1199    /// calls to [`read()`] may succeed.
1200    ///
1201    /// # Examples
1202    ///
1203    /// [`File`]s implement `Read`:
1204    ///
1205    /// [`File`]: crate::fs::File
1206    /// [`Ok(0)`]: Ok
1207    /// [`read()`]: Read::read
1208    ///
1209    /// ```no_run
1210    /// use std::io;
1211    /// use std::io::prelude::*;
1212    /// use std::fs::File;
1213    ///
1214    /// fn main() -> io::Result<()> {
1215    ///     let f = File::open("foo.txt")?;
1216    ///     let mut buffer = [0; 5];
1217    ///
1218    ///     // read at most five bytes
1219    ///     let mut handle = f.take(5);
1220    ///
1221    ///     handle.read(&mut buffer)?;
1222    ///     Ok(())
1223    /// }
1224    /// ```
1225    #[stable(feature = "rust1", since = "1.0.0")]
1226    fn take(self, limit: u64) -> Take<Self>
1227    where
1228        Self: Sized,
1229    {
1230        core::io::take(self, limit)
1231    }
1232
1233    /// Read and return a fixed array of bytes from this source.
1234    ///
1235    /// This function uses an array sized based on a const generic size known at compile time. You
1236    /// can specify the size with turbofish (`reader.read_array::<8>()`), or let type inference
1237    /// determine the number of bytes needed based on how the return value gets used. For instance,
1238    /// this function works well with functions like [`u64::from_le_bytes`] to turn an array of
1239    /// bytes into an integer of the same size.
1240    ///
1241    /// Like `read_exact`, if this function encounters an "end of file" before reading the desired
1242    /// number of bytes, it returns an error of the kind [`ErrorKind::UnexpectedEof`].
1243    ///
1244    /// ```
1245    /// #![feature(read_array)]
1246    /// use std::io::Cursor;
1247    /// use std::io::prelude::*;
1248    ///
1249    /// fn main() -> std::io::Result<()> {
1250    ///     let mut buf = Cursor::new([1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2]);
1251    ///     let x = u64::from_le_bytes(buf.read_array()?);
1252    ///     let y = u32::from_be_bytes(buf.read_array()?);
1253    ///     let z = u16::from_be_bytes(buf.read_array()?);
1254    ///     assert_eq!(x, 0x807060504030201);
1255    ///     assert_eq!(y, 0x9080706);
1256    ///     assert_eq!(z, 0x504);
1257    ///     Ok(())
1258    /// }
1259    /// ```
1260    #[unstable(feature = "read_array", issue = "148848")]
1261    fn read_array<const N: usize>(&mut self) -> Result<[u8; N]>
1262    where
1263        Self: Sized,
1264    {
1265        let mut buf = [MaybeUninit::uninit(); N];
1266        let mut borrowed_buf = BorrowedBuf::from(buf.as_mut_slice());
1267        self.read_buf_exact(borrowed_buf.unfilled())?;
1268        // Guard against incorrect `read_buf_exact` implementations.
1269        assert_eq!(borrowed_buf.len(), N);
1270        Ok(unsafe { MaybeUninit::array_assume_init(buf) })
1271    }
1272
1273    /// Read and return a type (e.g. an integer) in little-endian order.
1274    ///
1275    /// You can specify the type with turbofish (`reader.read_le::<u64>()`), or let type inference
1276    /// determine the type based on how the return value gets used.
1277    ///
1278    /// Like `read_exact`, if this function encounters an "end of file" before reading the desired
1279    /// number of bytes, it returns an error of the kind [`ErrorKind::UnexpectedEof`].
1280    ///
1281    /// ```
1282    /// #![feature(read_le)]
1283    /// use std::io::Cursor;
1284    /// use std::io::prelude::*;
1285    ///
1286    /// fn main() -> std::io::Result<()> {
1287    ///     let mut buf = Cursor::new([1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2]);
1288    ///     let x: u64 = buf.read_le()?;
1289    ///     let y: u32 = buf.read_le()?;
1290    ///     let z = buf.read_le::<u16>()?;
1291    ///     assert_eq!(x, 0x807060504030201);
1292    ///     assert_eq!(y, 0x6070809);
1293    ///     assert_eq!(z, 0x405);
1294    ///     Ok(())
1295    /// }
1296    /// ```
1297    #[unstable(feature = "read_le", issue = "156983")]
1298    #[inline]
1299    fn read_le<T: FromEndianBytes>(&mut self) -> Result<T>
1300    where
1301        Self: Sized,
1302    {
1303        T::read_le_from(self)
1304    }
1305
1306    /// Read and return a type (e.g. an integer) in big-endian order.
1307    ///
1308    /// You can specify the type with turbofish (`reader.read_be::<u64>()`), or let type inference
1309    /// determine the type based on how the return value gets used.
1310    ///
1311    /// Like `read_exact`, if this function encounters an "end of file" before reading the desired
1312    /// number of bytes, it returns an error of the kind [`ErrorKind::UnexpectedEof`].
1313    ///
1314    /// ```
1315    /// #![feature(read_le)]
1316    /// use std::io::Cursor;
1317    /// use std::io::prelude::*;
1318    ///
1319    /// fn main() -> std::io::Result<()> {
1320    ///     let mut buf = Cursor::new([1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2]);
1321    ///     let x: u64 = buf.read_be()?;
1322    ///     let y: u32 = buf.read_be()?;
1323    ///     let z = buf.read_be::<u16>()?;
1324    ///     assert_eq!(x, 0x102030405060708);
1325    ///     assert_eq!(y, 0x9080706);
1326    ///     assert_eq!(z, 0x504);
1327    ///     Ok(())
1328    /// }
1329    /// ```
1330    #[unstable(feature = "read_le", issue = "156983")]
1331    #[inline]
1332    fn read_be<T: FromEndianBytes>(&mut self) -> Result<T>
1333    where
1334        Self: Sized,
1335    {
1336        T::read_be_from(self)
1337    }
1338}
1339
1340/// Reads all bytes from a [reader][Read] into a new [`String`].
1341///
1342/// This is a convenience function for [`Read::read_to_string`]. Using this
1343/// function avoids having to create a variable first and provides more type
1344/// safety since you can only get the buffer out if there were no errors. (If you
1345/// use [`Read::read_to_string`] you have to remember to check whether the read
1346/// succeeded because otherwise your buffer will be empty or only partially full.)
1347///
1348/// # Performance
1349///
1350/// The downside of this function's increased ease of use and type safety is
1351/// that it gives you less control over performance. For example, you can't
1352/// pre-allocate memory like you can using [`String::with_capacity`] and
1353/// [`Read::read_to_string`]. Also, you can't re-use the buffer if an error
1354/// occurs while reading.
1355///
1356/// In many cases, this function's performance will be adequate and the ease of use
1357/// and type safety tradeoffs will be worth it. However, there are cases where you
1358/// need more control over performance, and in those cases you should definitely use
1359/// [`Read::read_to_string`] directly.
1360///
1361/// Note that in some special cases, such as when reading files, this function will
1362/// pre-allocate memory based on the size of the input it is reading. In those
1363/// cases, the performance should be as good as if you had used
1364/// [`Read::read_to_string`] with a manually pre-allocated buffer.
1365///
1366/// # Errors
1367///
1368/// This function forces you to handle errors because the output (the `String`)
1369/// is wrapped in a [`Result`]. See [`Read::read_to_string`] for the errors
1370/// that can occur. If any error occurs, you will get an [`Err`], so you
1371/// don't have to worry about your buffer being empty or partially full.
1372///
1373/// # Examples
1374///
1375/// ```no_run
1376/// # use std::io;
1377/// fn main() -> io::Result<()> {
1378///     let stdin = io::read_to_string(io::stdin())?;
1379///     println!("Stdin was:");
1380///     println!("{stdin}");
1381///     Ok(())
1382/// }
1383/// ```
1384///
1385/// # Usage Notes
1386///
1387/// `read_to_string` attempts to read a source until EOF, but many sources are continuous streams
1388/// that do not send EOF. In these cases, `read_to_string` will block indefinitely. Standard input
1389/// is one such stream which may be finite if piped, but is typically continuous. For example,
1390/// `cat file | my-rust-program` will correctly terminate with an `EOF` upon closure of cat.
1391/// Reading user input or running programs that remain open indefinitely will never terminate
1392/// the stream with `EOF` (e.g. `yes | my-rust-program`).
1393///
1394/// Using `.lines()` with a [`BufReader`] or using [`read`] can provide a better solution
1395///
1396///[`read`]: Read::read
1397///
1398#[stable(feature = "io_read_to_string", since = "1.65.0")]
1399pub fn read_to_string<R: Read>(mut reader: R) -> Result<String> {
1400    let mut buf = String::new();
1401    reader.read_to_string(&mut buf)?;
1402    Ok(buf)
1403}
1404
1405/// A trait for objects which are byte-oriented sinks.
1406///
1407/// Implementors of the `Write` trait are sometimes called 'writers'.
1408///
1409/// Writers are defined by two required methods, [`write`] and [`flush`]:
1410///
1411/// * The [`write`] method will attempt to write some data into the object,
1412///   returning how many bytes were successfully written.
1413///
1414/// * The [`flush`] method is useful for adapters and explicit buffers
1415///   themselves for ensuring that all buffered data has been pushed out to the
1416///   'true sink'.
1417///
1418/// Writers are intended to be composable with one another. Many implementors
1419/// throughout [`std::io`] take and provide types which implement the `Write`
1420/// trait.
1421///
1422/// [`write`]: Write::write
1423/// [`flush`]: Write::flush
1424/// [`std::io`]: self
1425///
1426/// # Examples
1427///
1428/// ```no_run
1429/// use std::io::prelude::*;
1430/// use std::fs::File;
1431///
1432/// fn main() -> std::io::Result<()> {
1433///     let data = b"some bytes";
1434///
1435///     let mut pos = 0;
1436///     let mut buffer = File::create("foo.txt")?;
1437///
1438///     while pos < data.len() {
1439///         let bytes_written = buffer.write(&data[pos..])?;
1440///         pos += bytes_written;
1441///     }
1442///     Ok(())
1443/// }
1444/// ```
1445///
1446/// The trait also provides convenience methods like [`write_all`], which calls
1447/// `write` in a loop until its entire input has been written.
1448///
1449/// [`write_all`]: Write::write_all
1450#[stable(feature = "rust1", since = "1.0.0")]
1451#[doc(notable_trait)]
1452#[cfg_attr(not(test), rustc_diagnostic_item = "IoWrite")]
1453pub trait Write {
1454    /// Writes a buffer into this writer, returning how many bytes were written.
1455    ///
1456    /// This function will attempt to write the entire contents of `buf`, but
1457    /// the entire write might not succeed, or the write may also generate an
1458    /// error. Typically, a call to `write` represents one attempt to write to
1459    /// any wrapped object.
1460    ///
1461    /// Calls to `write` are not guaranteed to block waiting for data to be
1462    /// written, and a write which would otherwise block can be indicated through
1463    /// an [`Err`] variant.
1464    ///
1465    /// If this method consumed `n > 0` bytes of `buf` it must return [`Ok(n)`].
1466    /// If the return value is `Ok(n)` then `n` must satisfy `n <= buf.len()`.
1467    /// A return value of `Ok(0)` typically means that the underlying object is
1468    /// no longer able to accept bytes and will likely not be able to in the
1469    /// future as well, or that the buffer provided is empty.
1470    ///
1471    /// # Errors
1472    ///
1473    /// Each call to `write` may generate an I/O error indicating that the
1474    /// operation could not be completed. If an error is returned then no bytes
1475    /// in the buffer were written to this writer.
1476    ///
1477    /// It is **not** considered an error if the entire buffer could not be
1478    /// written to this writer.
1479    ///
1480    /// An error of the [`ErrorKind::Interrupted`] kind is non-fatal and the
1481    /// write operation should be retried if there is nothing else to do.
1482    ///
1483    /// # Examples
1484    ///
1485    /// ```no_run
1486    /// use std::io::prelude::*;
1487    /// use std::fs::File;
1488    ///
1489    /// fn main() -> std::io::Result<()> {
1490    ///     let mut buffer = File::create("foo.txt")?;
1491    ///
1492    ///     // Writes some prefix of the byte string, not necessarily all of it.
1493    ///     buffer.write(b"some bytes")?;
1494    ///     Ok(())
1495    /// }
1496    /// ```
1497    ///
1498    /// [`Ok(n)`]: Ok
1499    #[stable(feature = "rust1", since = "1.0.0")]
1500    fn write(&mut self, buf: &[u8]) -> Result<usize>;
1501
1502    /// Like [`write`], except that it writes from a slice of buffers.
1503    ///
1504    /// Data is copied from each buffer in order, with the final buffer
1505    /// read from possibly being only partially consumed. This method must
1506    /// behave as a call to [`write`] with the buffers concatenated would.
1507    ///
1508    /// The default implementation calls [`write`] with either the first nonempty
1509    /// buffer provided, or an empty one if none exists.
1510    ///
1511    /// # Examples
1512    ///
1513    /// ```no_run
1514    /// use std::io::IoSlice;
1515    /// use std::io::prelude::*;
1516    /// use std::fs::File;
1517    ///
1518    /// fn main() -> std::io::Result<()> {
1519    ///     let data1 = [1; 8];
1520    ///     let data2 = [15; 8];
1521    ///     let io_slice1 = IoSlice::new(&data1);
1522    ///     let io_slice2 = IoSlice::new(&data2);
1523    ///
1524    ///     let mut buffer = File::create("foo.txt")?;
1525    ///
1526    ///     // Writes some prefix of the byte string, not necessarily all of it.
1527    ///     buffer.write_vectored(&[io_slice1, io_slice2])?;
1528    ///     Ok(())
1529    /// }
1530    /// ```
1531    ///
1532    /// [`write`]: Write::write
1533    #[stable(feature = "iovec", since = "1.36.0")]
1534    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize> {
1535        default_write_vectored(|b| self.write(b), bufs)
1536    }
1537
1538    /// Determines if this `Write`r has an efficient [`write_vectored`]
1539    /// implementation.
1540    ///
1541    /// If a `Write`r does not override the default [`write_vectored`]
1542    /// implementation, code using it may want to avoid the method all together
1543    /// and coalesce writes into a single buffer for higher performance.
1544    ///
1545    /// The default implementation returns `false`.
1546    ///
1547    /// [`write_vectored`]: Write::write_vectored
1548    #[unstable(feature = "can_vector", issue = "69941")]
1549    fn is_write_vectored(&self) -> bool {
1550        false
1551    }
1552
1553    /// Flushes this output stream, ensuring that all intermediately buffered
1554    /// contents reach their destination.
1555    ///
1556    /// # Errors
1557    ///
1558    /// It is considered an error if not all bytes could be written due to
1559    /// I/O errors or EOF being reached.
1560    ///
1561    /// # Examples
1562    ///
1563    /// ```no_run
1564    /// use std::io::prelude::*;
1565    /// use std::io::BufWriter;
1566    /// use std::fs::File;
1567    ///
1568    /// fn main() -> std::io::Result<()> {
1569    ///     let mut buffer = BufWriter::new(File::create("foo.txt")?);
1570    ///
1571    ///     buffer.write_all(b"some bytes")?;
1572    ///     buffer.flush()?;
1573    ///     Ok(())
1574    /// }
1575    /// ```
1576    #[stable(feature = "rust1", since = "1.0.0")]
1577    fn flush(&mut self) -> Result<()>;
1578
1579    /// Attempts to write an entire buffer into this writer.
1580    ///
1581    /// This method will continuously call [`write`] until there is no more data
1582    /// to be written or an error of non-[`ErrorKind::Interrupted`] kind is
1583    /// returned. This method will not return until the entire buffer has been
1584    /// successfully written or such an error occurs. The first error that is
1585    /// not of [`ErrorKind::Interrupted`] kind generated from this method will be
1586    /// returned.
1587    ///
1588    /// If the buffer contains no data, this will never call [`write`].
1589    ///
1590    /// # Errors
1591    ///
1592    /// This function will return the first error of
1593    /// non-[`ErrorKind::Interrupted`] kind that [`write`] returns.
1594    ///
1595    /// [`write`]: Write::write
1596    ///
1597    /// # Examples
1598    ///
1599    /// ```no_run
1600    /// use std::io::prelude::*;
1601    /// use std::fs::File;
1602    ///
1603    /// fn main() -> std::io::Result<()> {
1604    ///     let mut buffer = File::create("foo.txt")?;
1605    ///
1606    ///     buffer.write_all(b"some bytes")?;
1607    ///     Ok(())
1608    /// }
1609    /// ```
1610    #[stable(feature = "rust1", since = "1.0.0")]
1611    fn write_all(&mut self, mut buf: &[u8]) -> Result<()> {
1612        while !buf.is_empty() {
1613            match self.write(buf) {
1614                Ok(0) => {
1615                    return Err(Error::WRITE_ALL_EOF);
1616                }
1617                Ok(n) => buf = &buf[n..],
1618                Err(ref e) if e.is_interrupted() => {}
1619                Err(e) => return Err(e),
1620            }
1621        }
1622        Ok(())
1623    }
1624
1625    /// Attempts to write multiple buffers into this writer.
1626    ///
1627    /// This method will continuously call [`write_vectored`] until there is no
1628    /// more data to be written or an error of non-[`ErrorKind::Interrupted`]
1629    /// kind is returned. This method will not return until all buffers have
1630    /// been successfully written or such an error occurs. The first error that
1631    /// is not of [`ErrorKind::Interrupted`] kind generated from this method
1632    /// will be returned.
1633    ///
1634    /// If the buffer contains no data, this will never call [`write_vectored`].
1635    ///
1636    /// # Notes
1637    ///
1638    /// Unlike [`write_vectored`], this takes a *mutable* reference to
1639    /// a slice of [`IoSlice`]s, not an immutable one. That's because we need to
1640    /// modify the slice to keep track of the bytes already written.
1641    ///
1642    /// Once this function returns, the contents of `bufs` are unspecified, as
1643    /// this depends on how many calls to [`write_vectored`] were necessary. It is
1644    /// best to understand this function as taking ownership of `bufs` and to
1645    /// not use `bufs` afterwards. The underlying buffers, to which the
1646    /// [`IoSlice`]s point (but not the [`IoSlice`]s themselves), are unchanged and
1647    /// can be reused.
1648    ///
1649    /// [`write_vectored`]: Write::write_vectored
1650    ///
1651    /// # Examples
1652    ///
1653    /// ```
1654    /// #![feature(write_all_vectored)]
1655    /// # fn main() -> std::io::Result<()> {
1656    ///
1657    /// use std::io::{Write, IoSlice};
1658    ///
1659    /// let mut writer = Vec::new();
1660    /// let bufs = &mut [
1661    ///     IoSlice::new(&[1]),
1662    ///     IoSlice::new(&[2, 3]),
1663    ///     IoSlice::new(&[4, 5, 6]),
1664    /// ];
1665    ///
1666    /// writer.write_all_vectored(bufs)?;
1667    /// // Note: the contents of `bufs` is now undefined, see the Notes section.
1668    ///
1669    /// assert_eq!(writer, &[1, 2, 3, 4, 5, 6]);
1670    /// # Ok(()) }
1671    /// ```
1672    #[unstable(feature = "write_all_vectored", issue = "70436")]
1673    fn write_all_vectored(&mut self, mut bufs: &mut [IoSlice<'_>]) -> Result<()> {
1674        // Guarantee that bufs is empty if it contains no data,
1675        // to avoid calling write_vectored if there is no data to be written.
1676        IoSlice::advance_slices(&mut bufs, 0);
1677        while !bufs.is_empty() {
1678            match self.write_vectored(bufs) {
1679                Ok(0) => {
1680                    return Err(Error::WRITE_ALL_EOF);
1681                }
1682                Ok(n) => IoSlice::advance_slices(&mut bufs, n),
1683                Err(ref e) if e.is_interrupted() => {}
1684                Err(e) => return Err(e),
1685            }
1686        }
1687        Ok(())
1688    }
1689
1690    /// Writes a formatted string into this writer, returning any error
1691    /// encountered.
1692    ///
1693    /// This method is primarily used to interface with the
1694    /// [`format_args!()`] macro, and it is rare that this should
1695    /// explicitly be called. The [`write!()`] macro should be favored to
1696    /// invoke this method instead.
1697    ///
1698    /// This function internally uses the [`write_all`] method on
1699    /// this trait and hence will continuously write data so long as no errors
1700    /// are received. This also means that partial writes are not indicated in
1701    /// this signature.
1702    ///
1703    /// [`write_all`]: Write::write_all
1704    ///
1705    /// # Errors
1706    ///
1707    /// This function will return any I/O error reported while formatting.
1708    ///
1709    /// # Examples
1710    ///
1711    /// ```no_run
1712    /// use std::io::prelude::*;
1713    /// use std::fs::File;
1714    ///
1715    /// fn main() -> std::io::Result<()> {
1716    ///     let mut buffer = File::create("foo.txt")?;
1717    ///
1718    ///     // this call
1719    ///     write!(buffer, "{:.*}", 2, 1.234567)?;
1720    ///     // turns into this:
1721    ///     buffer.write_fmt(format_args!("{:.*}", 2, 1.234567))?;
1722    ///     Ok(())
1723    /// }
1724    /// ```
1725    #[stable(feature = "rust1", since = "1.0.0")]
1726    fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> Result<()> {
1727        if let Some(s) = args.as_statically_known_str() {
1728            self.write_all(s.as_bytes())
1729        } else {
1730            default_write_fmt(self, args)
1731        }
1732    }
1733
1734    /// Creates a "by reference" adapter for this instance of `Write`.
1735    ///
1736    /// The returned adapter also implements `Write` and will simply borrow this
1737    /// current writer.
1738    ///
1739    /// # Examples
1740    ///
1741    /// ```no_run
1742    /// use std::io::Write;
1743    /// use std::fs::File;
1744    ///
1745    /// fn main() -> std::io::Result<()> {
1746    ///     let mut buffer = File::create("foo.txt")?;
1747    ///
1748    ///     let reference = buffer.by_ref();
1749    ///
1750    ///     // we can use reference just like our original buffer
1751    ///     reference.write_all(b"some bytes")?;
1752    ///     Ok(())
1753    /// }
1754    /// ```
1755    #[stable(feature = "rust1", since = "1.0.0")]
1756    fn by_ref(&mut self) -> &mut Self
1757    where
1758        Self: Sized,
1759    {
1760        self
1761    }
1762}
1763
1764/// The `Seek` trait provides a cursor which can be moved within a stream of
1765/// bytes.
1766///
1767/// The stream typically has a fixed size, allowing seeking relative to either
1768/// end or the current offset.
1769///
1770/// # Examples
1771///
1772/// [`File`]s implement `Seek`:
1773///
1774/// [`File`]: crate::fs::File
1775///
1776/// ```no_run
1777/// use std::io;
1778/// use std::io::prelude::*;
1779/// use std::fs::File;
1780/// use std::io::SeekFrom;
1781///
1782/// fn main() -> io::Result<()> {
1783///     let mut f = File::open("foo.txt")?;
1784///
1785///     // move the cursor 42 bytes from the start of the file
1786///     f.seek(SeekFrom::Start(42))?;
1787///     Ok(())
1788/// }
1789/// ```
1790#[stable(feature = "rust1", since = "1.0.0")]
1791#[cfg_attr(not(test), rustc_diagnostic_item = "IoSeek")]
1792pub trait Seek {
1793    /// Seek to an offset, in bytes, in a stream.
1794    ///
1795    /// A seek beyond the end of a stream is allowed, but behavior is defined
1796    /// by the implementation.
1797    ///
1798    /// If the seek operation completed successfully,
1799    /// this method returns the new position from the start of the stream.
1800    /// That position can be used later with [`SeekFrom::Start`].
1801    ///
1802    /// # Errors
1803    ///
1804    /// Seeking can fail, for example because it might involve flushing a buffer.
1805    ///
1806    /// Seeking to a negative offset is considered an error.
1807    #[stable(feature = "rust1", since = "1.0.0")]
1808    fn seek(&mut self, pos: SeekFrom) -> Result<u64>;
1809
1810    /// Rewind to the beginning of a stream.
1811    ///
1812    /// This is a convenience method, equivalent to `seek(SeekFrom::Start(0))`.
1813    ///
1814    /// # Errors
1815    ///
1816    /// Rewinding can fail, for example because it might involve flushing a buffer.
1817    ///
1818    /// # Example
1819    ///
1820    /// ```no_run
1821    /// use std::io::{Read, Seek, Write};
1822    /// use std::fs::OpenOptions;
1823    ///
1824    /// let mut f = OpenOptions::new()
1825    ///     .write(true)
1826    ///     .read(true)
1827    ///     .create(true)
1828    ///     .open("foo.txt")?;
1829    ///
1830    /// let hello = "Hello!\n";
1831    /// write!(f, "{hello}")?;
1832    /// f.rewind()?;
1833    ///
1834    /// let mut buf = String::new();
1835    /// f.read_to_string(&mut buf)?;
1836    /// assert_eq!(&buf, hello);
1837    /// # std::io::Result::Ok(())
1838    /// ```
1839    #[stable(feature = "seek_rewind", since = "1.55.0")]
1840    fn rewind(&mut self) -> Result<()> {
1841        self.seek(SeekFrom::Start(0))?;
1842        Ok(())
1843    }
1844
1845    /// Returns the length of this stream (in bytes).
1846    ///
1847    /// The default implementation uses up to three seek operations. If this
1848    /// method returns successfully, the seek position is unchanged (i.e. the
1849    /// position before calling this method is the same as afterwards).
1850    /// However, if this method returns an error, the seek position is
1851    /// unspecified.
1852    ///
1853    /// If you need to obtain the length of *many* streams and you don't care
1854    /// about the seek position afterwards, you can reduce the number of seek
1855    /// operations by simply calling `seek(SeekFrom::End(0))` and using its
1856    /// return value (it is also the stream length).
1857    ///
1858    /// Note that length of a stream can change over time (for example, when
1859    /// data is appended to a file). So calling this method multiple times does
1860    /// not necessarily return the same length each time.
1861    ///
1862    /// # Example
1863    ///
1864    /// ```no_run
1865    /// #![feature(seek_stream_len)]
1866    /// use std::{
1867    ///     io::{self, Seek},
1868    ///     fs::File,
1869    /// };
1870    ///
1871    /// fn main() -> io::Result<()> {
1872    ///     let mut f = File::open("foo.txt")?;
1873    ///
1874    ///     let len = f.stream_len()?;
1875    ///     println!("The file is currently {len} bytes long");
1876    ///     Ok(())
1877    /// }
1878    /// ```
1879    #[unstable(feature = "seek_stream_len", issue = "59359")]
1880    fn stream_len(&mut self) -> Result<u64> {
1881        stream_len_default(self)
1882    }
1883
1884    /// Returns the current seek position from the start of the stream.
1885    ///
1886    /// This is equivalent to `self.seek(SeekFrom::Current(0))`.
1887    ///
1888    /// # Example
1889    ///
1890    /// ```no_run
1891    /// use std::{
1892    ///     io::{self, BufRead, BufReader, Seek},
1893    ///     fs::File,
1894    /// };
1895    ///
1896    /// fn main() -> io::Result<()> {
1897    ///     let mut f = BufReader::new(File::open("foo.txt")?);
1898    ///
1899    ///     let before = f.stream_position()?;
1900    ///     f.read_line(&mut String::new())?;
1901    ///     let after = f.stream_position()?;
1902    ///
1903    ///     println!("The first line was {} bytes long", after - before);
1904    ///     Ok(())
1905    /// }
1906    /// ```
1907    #[stable(feature = "seek_convenience", since = "1.51.0")]
1908    fn stream_position(&mut self) -> Result<u64> {
1909        self.seek(SeekFrom::Current(0))
1910    }
1911
1912    /// Seeks relative to the current position.
1913    ///
1914    /// This is equivalent to `self.seek(SeekFrom::Current(offset))` but
1915    /// doesn't return the new position which can allow some implementations
1916    /// such as [`BufReader`] to perform more efficient seeks.
1917    ///
1918    /// # Example
1919    ///
1920    /// ```no_run
1921    /// use std::{
1922    ///     io::{self, Seek},
1923    ///     fs::File,
1924    /// };
1925    ///
1926    /// fn main() -> io::Result<()> {
1927    ///     let mut f = File::open("foo.txt")?;
1928    ///     f.seek_relative(10)?;
1929    ///     assert_eq!(f.stream_position()?, 10);
1930    ///     Ok(())
1931    /// }
1932    /// ```
1933    ///
1934    /// [`BufReader`]: crate::io::BufReader
1935    #[stable(feature = "seek_seek_relative", since = "1.80.0")]
1936    fn seek_relative(&mut self, offset: i64) -> Result<()> {
1937        self.seek(SeekFrom::Current(offset))?;
1938        Ok(())
1939    }
1940}
1941
1942pub(crate) fn stream_len_default<T: Seek + ?Sized>(self_: &mut T) -> Result<u64> {
1943    let old_pos = self_.stream_position()?;
1944    let len = self_.seek(SeekFrom::End(0))?;
1945
1946    // Avoid seeking a third time when we were already at the end of the
1947    // stream. The branch is usually way cheaper than a seek operation.
1948    if old_pos != len {
1949        self_.seek(SeekFrom::Start(old_pos))?;
1950    }
1951
1952    Ok(len)
1953}
1954
1955/// Enumeration of possible methods to seek within an I/O object.
1956///
1957/// It is used by the [`Seek`] trait.
1958#[derive(Copy, PartialEq, Eq, Clone, Debug)]
1959#[stable(feature = "rust1", since = "1.0.0")]
1960#[cfg_attr(not(test), rustc_diagnostic_item = "SeekFrom")]
1961pub enum SeekFrom {
1962    /// Sets the offset to the provided number of bytes.
1963    #[stable(feature = "rust1", since = "1.0.0")]
1964    Start(#[stable(feature = "rust1", since = "1.0.0")] u64),
1965
1966    /// Sets the offset to the size of this object plus the specified number of
1967    /// bytes.
1968    ///
1969    /// It is possible to seek beyond the end of an object, but it's an error to
1970    /// seek before byte 0.
1971    #[stable(feature = "rust1", since = "1.0.0")]
1972    End(#[stable(feature = "rust1", since = "1.0.0")] i64),
1973
1974    /// Sets the offset to the current position plus the specified number of
1975    /// bytes.
1976    ///
1977    /// It is possible to seek beyond the end of an object, but it's an error to
1978    /// seek before byte 0.
1979    #[stable(feature = "rust1", since = "1.0.0")]
1980    Current(#[stable(feature = "rust1", since = "1.0.0")] i64),
1981}
1982
1983/// Marks that a type `T` can have IO traits such as [`Seek`], [`Write`], etc. automatically
1984/// implemented for handle types like [`Arc`][arc] as well.
1985///
1986/// This trait should only be implemented for types where `<&T as Trait>::method(&mut &value, ..)`
1987/// would be identical to `<T as Trait>::method(&mut value, ..)`.
1988///
1989/// [`File`][file] passes this test, as operations on `&File` and `File` both affect
1990/// the same underlying file.
1991/// `[u8]` fails, because any modification to `&mut &[u8]` would only affect a temporary
1992/// and be lost after the method has been called.
1993///
1994/// [file]: crate::fs::File
1995/// [arc]: crate::sync::Arc
1996pub(crate) trait IoHandle {}
1997
1998fn read_until<R: BufRead + ?Sized>(r: &mut R, delim: u8, buf: &mut Vec<u8>) -> Result<usize> {
1999    let mut read = 0;
2000    loop {
2001        let (done, used) = {
2002            let available = match r.fill_buf() {
2003                Ok(n) => n,
2004                Err(ref e) if e.is_interrupted() => continue,
2005                Err(e) => return Err(e),
2006            };
2007            match memchr::memchr(delim, available) {
2008                Some(i) => {
2009                    buf.extend_from_slice(&available[..=i]);
2010                    (true, i + 1)
2011                }
2012                None => {
2013                    buf.extend_from_slice(available);
2014                    (false, available.len())
2015                }
2016            }
2017        };
2018        r.consume(used);
2019        read += used;
2020        if done || used == 0 {
2021            return Ok(read);
2022        }
2023    }
2024}
2025
2026fn skip_until<R: BufRead + ?Sized>(r: &mut R, delim: u8) -> Result<usize> {
2027    let mut read = 0;
2028    loop {
2029        let (done, used) = {
2030            let available = match r.fill_buf() {
2031                Ok(n) => n,
2032                Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
2033                Err(e) => return Err(e),
2034            };
2035            match memchr::memchr(delim, available) {
2036                Some(i) => (true, i + 1),
2037                None => (false, available.len()),
2038            }
2039        };
2040        r.consume(used);
2041        read += used;
2042        if done || used == 0 {
2043            return Ok(read);
2044        }
2045    }
2046}
2047
2048/// A `BufRead` is a type of `Read`er which has an internal buffer, allowing it
2049/// to perform extra ways of reading.
2050///
2051/// For example, reading line-by-line is inefficient without using a buffer, so
2052/// if you want to read by line, you'll need `BufRead`, which includes a
2053/// [`read_line`] method as well as a [`lines`] iterator.
2054///
2055/// # Examples
2056///
2057/// A locked standard input implements `BufRead`:
2058///
2059/// ```no_run
2060/// use std::io;
2061/// use std::io::prelude::*;
2062///
2063/// let stdin = io::stdin();
2064/// for line in stdin.lock().lines() {
2065///     println!("{}", line?);
2066/// }
2067/// # std::io::Result::Ok(())
2068/// ```
2069///
2070/// If you have something that implements [`Read`], you can use the [`BufReader`
2071/// type][`BufReader`] to turn it into a `BufRead`.
2072///
2073/// For example, [`File`] implements [`Read`], but not `BufRead`.
2074/// [`BufReader`] to the rescue!
2075///
2076/// [`File`]: crate::fs::File
2077/// [`read_line`]: BufRead::read_line
2078/// [`lines`]: BufRead::lines
2079///
2080/// ```no_run
2081/// use std::io::{self, BufReader};
2082/// use std::io::prelude::*;
2083/// use std::fs::File;
2084///
2085/// fn main() -> io::Result<()> {
2086///     let f = File::open("foo.txt")?;
2087///     let f = BufReader::new(f);
2088///
2089///     for line in f.lines() {
2090///         let line = line?;
2091///         println!("{line}");
2092///     }
2093///
2094///     Ok(())
2095/// }
2096/// ```
2097#[stable(feature = "rust1", since = "1.0.0")]
2098#[cfg_attr(not(test), rustc_diagnostic_item = "IoBufRead")]
2099pub trait BufRead: Read {
2100    /// Returns the contents of the internal buffer, filling it with more data, via `Read` methods, if empty.
2101    ///
2102    /// This is a lower-level method and is meant to be used together with [`consume`],
2103    /// which can be used to mark bytes that should not be returned by subsequent calls to `read`.
2104    ///
2105    /// [`consume`]: BufRead::consume
2106    ///
2107    /// Returns an empty buffer when the stream has reached EOF.
2108    ///
2109    /// # Errors
2110    ///
2111    /// This function will return an I/O error if a `Read` method was called, but returned an error.
2112    ///
2113    /// # Examples
2114    ///
2115    /// A locked standard input implements `BufRead`:
2116    ///
2117    /// ```no_run
2118    /// use std::io;
2119    /// use std::io::prelude::*;
2120    ///
2121    /// let stdin = io::stdin();
2122    /// let mut stdin = stdin.lock();
2123    ///
2124    /// let buffer = stdin.fill_buf()?;
2125    ///
2126    /// // work with buffer
2127    /// println!("{buffer:?}");
2128    ///
2129    /// // mark the bytes we worked with as read
2130    /// let length = buffer.len();
2131    /// stdin.consume(length);
2132    /// # std::io::Result::Ok(())
2133    /// ```
2134    #[stable(feature = "rust1", since = "1.0.0")]
2135    fn fill_buf(&mut self) -> Result<&[u8]>;
2136
2137    /// Marks the given `amount` of additional bytes from the internal buffer as having been read.
2138    /// Subsequent calls to `read` only return bytes that have not been marked as read.
2139    ///
2140    /// This is a lower-level method and is meant to be used together with [`fill_buf`],
2141    /// which can be used to fill the internal buffer via `Read` methods.
2142    ///
2143    /// It is a logic error if `amount` exceeds the number of unread bytes in the internal buffer, which is returned by [`fill_buf`].
2144    ///
2145    /// # Examples
2146    ///
2147    /// Since `consume()` is meant to be used with [`fill_buf`],
2148    /// that method's example includes an example of `consume()`.
2149    ///
2150    /// [`fill_buf`]: BufRead::fill_buf
2151    #[stable(feature = "rust1", since = "1.0.0")]
2152    fn consume(&mut self, amount: usize);
2153
2154    /// Checks if there is any data left to be `read`.
2155    ///
2156    /// This function may fill the buffer to check for data,
2157    /// so this function returns `Result<bool>`, not `bool`.
2158    ///
2159    /// The default implementation calls `fill_buf` and checks that the
2160    /// returned slice is empty (which means that there is no data left,
2161    /// since EOF is reached).
2162    ///
2163    /// # Errors
2164    ///
2165    /// This function will return an I/O error if a `Read` method was called, but returned an error.
2166    ///
2167    /// Examples
2168    ///
2169    /// ```
2170    /// #![feature(buf_read_has_data_left)]
2171    /// use std::io;
2172    /// use std::io::prelude::*;
2173    ///
2174    /// let stdin = io::stdin();
2175    /// let mut stdin = stdin.lock();
2176    ///
2177    /// while stdin.has_data_left()? {
2178    ///     let mut line = String::new();
2179    ///     stdin.read_line(&mut line)?;
2180    ///     // work with line
2181    ///     println!("{line:?}");
2182    /// }
2183    /// # std::io::Result::Ok(())
2184    /// ```
2185    #[unstable(feature = "buf_read_has_data_left", issue = "86423")]
2186    fn has_data_left(&mut self) -> Result<bool> {
2187        self.fill_buf().map(|b| !b.is_empty())
2188    }
2189
2190    /// Reads all bytes into `buf` until the delimiter `byte` or EOF is reached.
2191    ///
2192    /// This function will read bytes from the underlying stream until the
2193    /// delimiter or EOF is found. Once found, all bytes up to, and including,
2194    /// the delimiter (if found) will be appended to `buf`.
2195    ///
2196    /// If successful, this function will return the total number of bytes read.
2197    ///
2198    /// This function is blocking and should be used carefully: it is possible for
2199    /// an attacker to continuously send bytes without ever sending the delimiter
2200    /// or EOF.
2201    ///
2202    /// # Errors
2203    ///
2204    /// This function will ignore all instances of [`ErrorKind::Interrupted`] and
2205    /// will otherwise return any errors returned by [`fill_buf`].
2206    ///
2207    /// If an I/O error is encountered then all bytes read so far will be
2208    /// present in `buf` and its length will have been adjusted appropriately.
2209    ///
2210    /// [`fill_buf`]: BufRead::fill_buf
2211    ///
2212    /// # Examples
2213    ///
2214    /// [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In
2215    /// this example, we use [`Cursor`] to read all the bytes in a byte slice
2216    /// in hyphen delimited segments:
2217    ///
2218    /// ```
2219    /// use std::io::{self, BufRead};
2220    ///
2221    /// let mut cursor = io::Cursor::new(b"lorem-ipsum");
2222    /// let mut buf = vec![];
2223    ///
2224    /// // cursor is at 'l'
2225    /// let num_bytes = cursor.read_until(b'-', &mut buf)
2226    ///     .expect("reading from cursor won't fail");
2227    /// assert_eq!(num_bytes, 6);
2228    /// assert_eq!(buf, b"lorem-");
2229    /// buf.clear();
2230    ///
2231    /// // cursor is at 'i'
2232    /// let num_bytes = cursor.read_until(b'-', &mut buf)
2233    ///     .expect("reading from cursor won't fail");
2234    /// assert_eq!(num_bytes, 5);
2235    /// assert_eq!(buf, b"ipsum");
2236    /// buf.clear();
2237    ///
2238    /// // cursor is at EOF
2239    /// let num_bytes = cursor.read_until(b'-', &mut buf)
2240    ///     .expect("reading from cursor won't fail");
2241    /// assert_eq!(num_bytes, 0);
2242    /// assert_eq!(buf, b"");
2243    /// ```
2244    #[stable(feature = "rust1", since = "1.0.0")]
2245    fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> Result<usize> {
2246        read_until(self, byte, buf)
2247    }
2248
2249    /// Skips all bytes until the delimiter `byte` or EOF is reached.
2250    ///
2251    /// This function will read (and discard) bytes from the underlying stream until the
2252    /// delimiter or EOF is found.
2253    ///
2254    /// If successful, this function will return the total number of bytes read,
2255    /// including the delimiter byte if found.
2256    ///
2257    /// This is useful for efficiently skipping data such as NUL-terminated strings
2258    /// in binary file formats without buffering.
2259    ///
2260    /// This function is blocking and should be used carefully: it is possible for
2261    /// an attacker to continuously send bytes without ever sending the delimiter
2262    /// or EOF.
2263    ///
2264    /// # Errors
2265    ///
2266    /// This function will ignore all instances of [`ErrorKind::Interrupted`] and
2267    /// will otherwise return any errors returned by [`fill_buf`].
2268    ///
2269    /// If an I/O error is encountered then all bytes read so far will be
2270    /// present in `buf` and its length will have been adjusted appropriately.
2271    ///
2272    /// [`fill_buf`]: BufRead::fill_buf
2273    ///
2274    /// # Examples
2275    ///
2276    /// [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In
2277    /// this example, we use [`Cursor`] to read some NUL-terminated information
2278    /// about Ferris from a binary string, skipping the fun fact:
2279    ///
2280    /// ```
2281    /// use std::io::{self, BufRead};
2282    ///
2283    /// let mut cursor = io::Cursor::new(b"Ferris\0Likes long walks on the beach\0Crustacean\0!");
2284    ///
2285    /// // read name
2286    /// let mut name = Vec::new();
2287    /// let num_bytes = cursor.read_until(b'\0', &mut name)
2288    ///     .expect("reading from cursor won't fail");
2289    /// assert_eq!(num_bytes, 7);
2290    /// assert_eq!(name, b"Ferris\0");
2291    ///
2292    /// // skip fun fact
2293    /// let num_bytes = cursor.skip_until(b'\0')
2294    ///     .expect("reading from cursor won't fail");
2295    /// assert_eq!(num_bytes, 30);
2296    ///
2297    /// // read animal type
2298    /// let mut animal = Vec::new();
2299    /// let num_bytes = cursor.read_until(b'\0', &mut animal)
2300    ///     .expect("reading from cursor won't fail");
2301    /// assert_eq!(num_bytes, 11);
2302    /// assert_eq!(animal, b"Crustacean\0");
2303    ///
2304    /// // reach EOF
2305    /// let num_bytes = cursor.skip_until(b'\0')
2306    ///     .expect("reading from cursor won't fail");
2307    /// assert_eq!(num_bytes, 1);
2308    /// ```
2309    #[stable(feature = "bufread_skip_until", since = "1.83.0")]
2310    fn skip_until(&mut self, byte: u8) -> Result<usize> {
2311        skip_until(self, byte)
2312    }
2313
2314    /// Reads all bytes until a newline (the `0xA` byte) is reached, and append
2315    /// them to the provided `String` buffer.
2316    ///
2317    /// Previous content of the buffer will be preserved. To avoid appending to
2318    /// the buffer, you need to [`clear`] it first.
2319    ///
2320    /// This function will read bytes from the underlying stream until the
2321    /// newline delimiter (the `0xA` byte) or EOF is found. Once found, all bytes
2322    /// up to, and including, the delimiter (if found) will be appended to
2323    /// `buf`.
2324    ///
2325    /// If successful, this function will return the total number of bytes read.
2326    ///
2327    /// If this function returns [`Ok(0)`], the stream has reached EOF.
2328    ///
2329    /// This function is blocking and should be used carefully: it is possible for
2330    /// an attacker to continuously send bytes without ever sending a newline
2331    /// or EOF. You can use [`take`] to limit the maximum number of bytes read.
2332    ///
2333    /// [`Ok(0)`]: Ok
2334    /// [`clear`]: String::clear
2335    /// [`take`]: crate::io::Read::take
2336    ///
2337    /// # Errors
2338    ///
2339    /// This function has the same error semantics as [`read_until`] and will
2340    /// also return an error if the read bytes are not valid UTF-8. If an I/O
2341    /// error is encountered then `buf` may contain some bytes already read in
2342    /// the event that all data read so far was valid UTF-8.
2343    ///
2344    /// [`read_until`]: BufRead::read_until
2345    ///
2346    /// # Examples
2347    ///
2348    /// [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In
2349    /// this example, we use [`Cursor`] to read all the lines in a byte slice:
2350    ///
2351    /// ```
2352    /// use std::io::{self, BufRead};
2353    ///
2354    /// let mut cursor = io::Cursor::new(b"foo\nbar");
2355    /// let mut buf = String::new();
2356    ///
2357    /// // cursor is at 'f'
2358    /// let num_bytes = cursor.read_line(&mut buf)
2359    ///     .expect("reading from cursor won't fail");
2360    /// assert_eq!(num_bytes, 4);
2361    /// assert_eq!(buf, "foo\n");
2362    /// buf.clear();
2363    ///
2364    /// // cursor is at 'b'
2365    /// let num_bytes = cursor.read_line(&mut buf)
2366    ///     .expect("reading from cursor won't fail");
2367    /// assert_eq!(num_bytes, 3);
2368    /// assert_eq!(buf, "bar");
2369    /// buf.clear();
2370    ///
2371    /// // cursor is at EOF
2372    /// let num_bytes = cursor.read_line(&mut buf)
2373    ///     .expect("reading from cursor won't fail");
2374    /// assert_eq!(num_bytes, 0);
2375    /// assert_eq!(buf, "");
2376    /// ```
2377    #[stable(feature = "rust1", since = "1.0.0")]
2378    fn read_line(&mut self, buf: &mut String) -> Result<usize> {
2379        // Note that we are not calling the `.read_until` method here, but
2380        // rather our hardcoded implementation. For more details as to why, see
2381        // the comments in `default_read_to_string`.
2382        unsafe { append_to_string(buf, |b| read_until(self, b'\n', b)) }
2383    }
2384
2385    /// Returns an iterator over the contents of this reader split on the byte
2386    /// `byte`.
2387    ///
2388    /// The iterator returned from this function will return instances of
2389    /// <code>[io::Result]<[Vec]\<u8>></code>. Each vector returned will *not* have
2390    /// the delimiter byte at the end.
2391    ///
2392    /// This function will yield errors whenever [`read_until`] would have
2393    /// also yielded an error.
2394    ///
2395    /// [io::Result]: self::Result "io::Result"
2396    /// [`read_until`]: BufRead::read_until
2397    ///
2398    /// # Examples
2399    ///
2400    /// [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In
2401    /// this example, we use [`Cursor`] to iterate over all hyphen delimited
2402    /// segments in a byte slice
2403    ///
2404    /// ```
2405    /// use std::io::{self, BufRead};
2406    ///
2407    /// let cursor = io::Cursor::new(b"lorem-ipsum-dolor");
2408    ///
2409    /// let mut split_iter = cursor.split(b'-').map(|l| l.unwrap());
2410    /// assert_eq!(split_iter.next(), Some(b"lorem".to_vec()));
2411    /// assert_eq!(split_iter.next(), Some(b"ipsum".to_vec()));
2412    /// assert_eq!(split_iter.next(), Some(b"dolor".to_vec()));
2413    /// assert_eq!(split_iter.next(), None);
2414    /// ```
2415    #[stable(feature = "rust1", since = "1.0.0")]
2416    fn split(self, byte: u8) -> Split<Self>
2417    where
2418        Self: Sized,
2419    {
2420        Split { buf: self, delim: byte }
2421    }
2422
2423    /// Returns an iterator over the lines of this reader.
2424    ///
2425    /// The iterator returned from this function will yield instances of
2426    /// <code>[io::Result]<[String]></code>. Each string returned will *not* have a newline
2427    /// byte (the `0xA` byte) or `CRLF` (`0xD`, `0xA` bytes) at the end.
2428    ///
2429    /// [io::Result]: self::Result "io::Result"
2430    ///
2431    /// # Examples
2432    ///
2433    /// [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In
2434    /// this example, we use [`Cursor`] to iterate over all the lines in a byte
2435    /// slice.
2436    ///
2437    /// ```
2438    /// use std::io::{self, BufRead};
2439    ///
2440    /// let cursor = io::Cursor::new(b"lorem\nipsum\r\ndolor");
2441    ///
2442    /// let mut lines_iter = cursor.lines().map(|l| l.unwrap());
2443    /// assert_eq!(lines_iter.next(), Some(String::from("lorem")));
2444    /// assert_eq!(lines_iter.next(), Some(String::from("ipsum")));
2445    /// assert_eq!(lines_iter.next(), Some(String::from("dolor")));
2446    /// assert_eq!(lines_iter.next(), None);
2447    /// ```
2448    ///
2449    /// # Errors
2450    ///
2451    /// Each line of the iterator has the same error semantics as [`BufRead::read_line`].
2452    #[stable(feature = "rust1", since = "1.0.0")]
2453    fn lines(self) -> Lines<Self>
2454    where
2455        Self: Sized,
2456    {
2457        Lines { buf: self }
2458    }
2459}
2460
2461#[stable(feature = "rust1", since = "1.0.0")]
2462impl<T: Read, U: Read> Read for Chain<T, U> {
2463    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
2464        if !self.done_first {
2465            match self.first.read(buf)? {
2466                0 if !buf.is_empty() => self.done_first = true,
2467                n => return Ok(n),
2468            }
2469        }
2470        self.second.read(buf)
2471    }
2472
2473    fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize> {
2474        if !self.done_first {
2475            match self.first.read_vectored(bufs)? {
2476                0 if bufs.iter().any(|b| !b.is_empty()) => self.done_first = true,
2477                n => return Ok(n),
2478            }
2479        }
2480        self.second.read_vectored(bufs)
2481    }
2482
2483    #[inline]
2484    fn is_read_vectored(&self) -> bool {
2485        self.first.is_read_vectored() || self.second.is_read_vectored()
2486    }
2487
2488    fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
2489        let mut read = 0;
2490        if !self.done_first {
2491            read += self.first.read_to_end(buf)?;
2492            self.done_first = true;
2493        }
2494        read += self.second.read_to_end(buf)?;
2495        Ok(read)
2496    }
2497
2498    // We don't override `read_to_string` here because an UTF-8 sequence could
2499    // be split between the two parts of the chain
2500
2501    fn read_buf(&mut self, mut buf: BorrowedCursor<'_, u8>) -> Result<()> {
2502        if buf.capacity() == 0 {
2503            return Ok(());
2504        }
2505
2506        if !self.done_first {
2507            let old_len = buf.written();
2508            self.first.read_buf(buf.reborrow())?;
2509
2510            if buf.written() != old_len {
2511                return Ok(());
2512            } else {
2513                self.done_first = true;
2514            }
2515        }
2516        self.second.read_buf(buf)
2517    }
2518}
2519
2520#[stable(feature = "chain_bufread", since = "1.9.0")]
2521impl<T: BufRead, U: BufRead> BufRead for Chain<T, U> {
2522    fn fill_buf(&mut self) -> Result<&[u8]> {
2523        if !self.done_first {
2524            match self.first.fill_buf()? {
2525                buf if buf.is_empty() => self.done_first = true,
2526                buf => return Ok(buf),
2527            }
2528        }
2529        self.second.fill_buf()
2530    }
2531
2532    fn consume(&mut self, amt: usize) {
2533        if !self.done_first { self.first.consume(amt) } else { self.second.consume(amt) }
2534    }
2535
2536    fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> Result<usize> {
2537        let mut read = 0;
2538        if !self.done_first {
2539            let n = self.first.read_until(byte, buf)?;
2540            read += n;
2541
2542            match buf.last() {
2543                Some(b) if *b == byte && n != 0 => return Ok(read),
2544                _ => self.done_first = true,
2545            }
2546        }
2547        read += self.second.read_until(byte, buf)?;
2548        Ok(read)
2549    }
2550
2551    // We don't override `read_line` here because an UTF-8 sequence could be
2552    // split between the two parts of the chain
2553}
2554
2555impl<T, U> SizeHint for Chain<T, U> {
2556    #[inline]
2557    fn lower_bound(&self) -> usize {
2558        SizeHint::lower_bound(&self.first) + SizeHint::lower_bound(&self.second)
2559    }
2560
2561    #[inline]
2562    fn upper_bound(&self) -> Option<usize> {
2563        match (SizeHint::upper_bound(&self.first), SizeHint::upper_bound(&self.second)) {
2564            (Some(first), Some(second)) => first.checked_add(second),
2565            _ => None,
2566        }
2567    }
2568}
2569
2570#[stable(feature = "rust1", since = "1.0.0")]
2571impl<T: Read> Read for Take<T> {
2572    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
2573        // Don't call into inner reader at all at EOF because it may still block
2574        if self.limit == 0 {
2575            return Ok(0);
2576        }
2577
2578        let max = cmp::min(buf.len() as u64, self.limit) as usize;
2579        let n = self.inner.read(&mut buf[..max])?;
2580        assert!(n as u64 <= self.limit, "number of read bytes exceeds limit");
2581        self.limit -= n as u64;
2582        Ok(n)
2583    }
2584
2585    fn read_buf(&mut self, mut buf: BorrowedCursor<'_, u8>) -> Result<()> {
2586        // Don't call into inner reader at all at EOF because it may still block
2587        if self.limit == 0 {
2588            return Ok(());
2589        }
2590
2591        if self.limit < buf.capacity() as u64 {
2592            // The condition above guarantees that `self.limit` fits in `usize`.
2593            let limit = self.limit as usize;
2594
2595            let is_init = buf.is_init();
2596
2597            // SAFETY: no uninit data is written to ibuf
2598            let mut sliced_buf = BorrowedBuf::from(unsafe { &mut buf.as_mut()[..limit] });
2599
2600            if is_init {
2601                // SAFETY: `sliced_buf` is a subslice of `buf`, so if `buf` was initialized then
2602                // `sliced_buf` is.
2603                unsafe { sliced_buf.set_init() };
2604            }
2605
2606            let result = self.inner.read_buf(sliced_buf.unfilled());
2607
2608            let did_init_up_to_limit = sliced_buf.is_init();
2609            let filled = sliced_buf.len();
2610
2611            // sliced_buf must drop here
2612
2613            // Avoid accidentally quadratic behaviour by initializing the whole
2614            // cursor if only part of it was initialized.
2615            if did_init_up_to_limit && !is_init {
2616                // SAFETY: No uninit data will be written.
2617                let unfilled_before_advance = unsafe { buf.as_mut() };
2618
2619                unfilled_before_advance[limit..].write_filled(0);
2620
2621                // SAFETY: `unfilled_before_advance[..limit]` was initialized by `T::read_buf`, and
2622                // `unfilled_before_advance[limit..]` was just initialized.
2623                unsafe { buf.set_init() };
2624            }
2625
2626            unsafe {
2627                // SAFETY: filled bytes have been filled
2628                buf.advance(filled);
2629            }
2630
2631            self.limit -= filled as u64;
2632
2633            result
2634        } else {
2635            let written = buf.written();
2636            let result = self.inner.read_buf(buf.reborrow());
2637            self.limit -= (buf.written() - written) as u64;
2638            result
2639        }
2640    }
2641}
2642
2643#[stable(feature = "rust1", since = "1.0.0")]
2644impl<T: BufRead> BufRead for Take<T> {
2645    fn fill_buf(&mut self) -> Result<&[u8]> {
2646        // Don't call into inner reader at all at EOF because it may still block
2647        if self.limit == 0 {
2648            return Ok(&[]);
2649        }
2650
2651        let buf = self.inner.fill_buf()?;
2652        let cap = cmp::min(buf.len() as u64, self.limit) as usize;
2653        Ok(&buf[..cap])
2654    }
2655
2656    fn consume(&mut self, amt: usize) {
2657        // Don't let callers reset the limit by passing an overlarge value
2658        let amt = cmp::min(amt as u64, self.limit) as usize;
2659        self.limit -= amt as u64;
2660        self.inner.consume(amt);
2661    }
2662}
2663
2664impl<T> SizeHint for Take<T> {
2665    #[inline]
2666    fn lower_bound(&self) -> usize {
2667        cmp::min(SizeHint::lower_bound(&self.inner) as u64, self.limit) as usize
2668    }
2669
2670    #[inline]
2671    fn upper_bound(&self) -> Option<usize> {
2672        match SizeHint::upper_bound(&self.inner) {
2673            Some(upper_bound) => Some(cmp::min(upper_bound as u64, self.limit) as usize),
2674            None => self.limit.try_into().ok(),
2675        }
2676    }
2677}
2678
2679#[stable(feature = "seek_io_take", since = "1.89.0")]
2680impl<T: Seek> Seek for Take<T> {
2681    fn seek(&mut self, pos: SeekFrom) -> Result<u64> {
2682        let new_position = match pos {
2683            SeekFrom::Start(v) => Some(v),
2684            SeekFrom::Current(v) => self.position().checked_add_signed(v),
2685            SeekFrom::End(v) => self.len.checked_add_signed(v),
2686        };
2687        let new_position = match new_position {
2688            Some(v) if v <= self.len => v,
2689            _ => return Err(ErrorKind::InvalidInput.into()),
2690        };
2691        while new_position != self.position() {
2692            if let Some(offset) = new_position.checked_signed_diff(self.position()) {
2693                self.inner.seek_relative(offset)?;
2694                self.limit = self.limit.wrapping_sub(offset as u64);
2695                break;
2696            }
2697            let offset = if new_position > self.position() { i64::MAX } else { i64::MIN };
2698            self.inner.seek_relative(offset)?;
2699            self.limit = self.limit.wrapping_sub(offset as u64);
2700        }
2701        Ok(new_position)
2702    }
2703
2704    fn stream_len(&mut self) -> Result<u64> {
2705        Ok(self.len)
2706    }
2707
2708    fn stream_position(&mut self) -> Result<u64> {
2709        Ok(self.position())
2710    }
2711
2712    fn seek_relative(&mut self, offset: i64) -> Result<()> {
2713        if !self.position().checked_add_signed(offset).is_some_and(|p| p <= self.len) {
2714            return Err(ErrorKind::InvalidInput.into());
2715        }
2716        self.inner.seek_relative(offset)?;
2717        self.limit = self.limit.wrapping_sub(offset as u64);
2718        Ok(())
2719    }
2720}
2721
2722/// An iterator over `u8` values of a reader.
2723///
2724/// This struct is generally created by calling [`bytes`] on a reader.
2725/// Please see the documentation of [`bytes`] for more details.
2726///
2727/// [`bytes`]: Read::bytes
2728#[stable(feature = "rust1", since = "1.0.0")]
2729#[derive(Debug)]
2730pub struct Bytes<R> {
2731    inner: R,
2732}
2733
2734#[stable(feature = "rust1", since = "1.0.0")]
2735impl<R: Read> Iterator for Bytes<R> {
2736    type Item = Result<u8>;
2737
2738    // Not `#[inline]`. This function gets inlined even without it, but having
2739    // the inline annotation can result in worse code generation. See #116785.
2740    fn next(&mut self) -> Option<Result<u8>> {
2741        SpecReadByte::spec_read_byte(&mut self.inner)
2742    }
2743
2744    #[inline]
2745    fn size_hint(&self) -> (usize, Option<usize>) {
2746        SizeHint::size_hint(&self.inner)
2747    }
2748}
2749
2750/// For the specialization of `Bytes::next`.
2751trait SpecReadByte {
2752    fn spec_read_byte(&mut self) -> Option<Result<u8>>;
2753}
2754
2755impl<R> SpecReadByte for R
2756where
2757    Self: Read,
2758{
2759    #[inline]
2760    default fn spec_read_byte(&mut self) -> Option<Result<u8>> {
2761        inlined_slow_read_byte(self)
2762    }
2763}
2764
2765/// Reads a single byte in a slow, generic way. This is used by the default
2766/// `spec_read_byte`.
2767#[inline]
2768fn inlined_slow_read_byte<R: Read>(reader: &mut R) -> Option<Result<u8>> {
2769    let mut byte = 0;
2770    loop {
2771        return match reader.read(slice::from_mut(&mut byte)) {
2772            Ok(0) => None,
2773            Ok(..) => Some(Ok(byte)),
2774            Err(ref e) if e.is_interrupted() => continue,
2775            Err(e) => Some(Err(e)),
2776        };
2777    }
2778}
2779
2780// Used by `BufReader::spec_read_byte`, for which the `inline(never)` is
2781// important.
2782#[inline(never)]
2783fn uninlined_slow_read_byte<R: Read>(reader: &mut R) -> Option<Result<u8>> {
2784    inlined_slow_read_byte(reader)
2785}
2786
2787trait SizeHint {
2788    fn lower_bound(&self) -> usize;
2789
2790    fn upper_bound(&self) -> Option<usize>;
2791
2792    fn size_hint(&self) -> (usize, Option<usize>) {
2793        (self.lower_bound(), self.upper_bound())
2794    }
2795}
2796
2797impl<T: ?Sized> SizeHint for T {
2798    #[inline]
2799    default fn lower_bound(&self) -> usize {
2800        0
2801    }
2802
2803    #[inline]
2804    default fn upper_bound(&self) -> Option<usize> {
2805        None
2806    }
2807}
2808
2809impl<T> SizeHint for &mut T {
2810    #[inline]
2811    fn lower_bound(&self) -> usize {
2812        SizeHint::lower_bound(*self)
2813    }
2814
2815    #[inline]
2816    fn upper_bound(&self) -> Option<usize> {
2817        SizeHint::upper_bound(*self)
2818    }
2819}
2820
2821impl<T> SizeHint for Box<T> {
2822    #[inline]
2823    fn lower_bound(&self) -> usize {
2824        SizeHint::lower_bound(&**self)
2825    }
2826
2827    #[inline]
2828    fn upper_bound(&self) -> Option<usize> {
2829        SizeHint::upper_bound(&**self)
2830    }
2831}
2832
2833impl SizeHint for &[u8] {
2834    #[inline]
2835    fn lower_bound(&self) -> usize {
2836        self.len()
2837    }
2838
2839    #[inline]
2840    fn upper_bound(&self) -> Option<usize> {
2841        Some(self.len())
2842    }
2843}
2844
2845/// An iterator over the contents of an instance of `BufRead` split on a
2846/// particular byte.
2847///
2848/// This struct is generally created by calling [`split`] on a `BufRead`.
2849/// Please see the documentation of [`split`] for more details.
2850///
2851/// [`split`]: BufRead::split
2852#[stable(feature = "rust1", since = "1.0.0")]
2853#[derive(Debug)]
2854#[cfg_attr(not(test), rustc_diagnostic_item = "IoSplit")]
2855pub struct Split<B> {
2856    buf: B,
2857    delim: u8,
2858}
2859
2860#[stable(feature = "rust1", since = "1.0.0")]
2861impl<B: BufRead> Iterator for Split<B> {
2862    type Item = Result<Vec<u8>>;
2863
2864    fn next(&mut self) -> Option<Result<Vec<u8>>> {
2865        let mut buf = Vec::new();
2866        match self.buf.read_until(self.delim, &mut buf) {
2867            Ok(0) => None,
2868            Ok(_n) => {
2869                if buf[buf.len() - 1] == self.delim {
2870                    buf.pop();
2871                }
2872                Some(Ok(buf))
2873            }
2874            Err(e) => Some(Err(e)),
2875        }
2876    }
2877}
2878
2879/// An iterator over the lines of an instance of `BufRead`.
2880///
2881/// This struct is generally created by calling [`lines`] on a `BufRead`.
2882/// Please see the documentation of [`lines`] for more details.
2883///
2884/// [`lines`]: BufRead::lines
2885#[stable(feature = "rust1", since = "1.0.0")]
2886#[derive(Debug)]
2887#[cfg_attr(not(test), rustc_diagnostic_item = "IoLines")]
2888pub struct Lines<B> {
2889    buf: B,
2890}
2891
2892#[stable(feature = "rust1", since = "1.0.0")]
2893impl<B: BufRead> Iterator for Lines<B> {
2894    type Item = Result<String>;
2895
2896    fn next(&mut self) -> Option<Result<String>> {
2897        let mut buf = String::new();
2898        match self.buf.read_line(&mut buf) {
2899            Ok(0) => None,
2900            Ok(_n) => {
2901                if buf.ends_with('\n') {
2902                    buf.pop();
2903                    if buf.ends_with('\r') {
2904                        buf.pop();
2905                    }
2906                }
2907                Some(Ok(buf))
2908            }
2909            Err(e) => Some(Err(e)),
2910        }
2911    }
2912}
2913
2914/// Trait for types that can be converted from a fixed-size byte array with a specified endianness
2915#[unstable(feature = "read_le_be_internals", reason = "internals", issue = "none")]
2916// Once we can use associated consts in the types of method parameters, rewrite this to have
2917// `from_le_bytes` and `from_be_bytes` methods, move it to `core`, and make it public.
2918pub trait FromEndianBytes: crate::sealed::Sealed + Sized {
2919    #[doc(hidden)]
2920    fn read_le_from(r: &mut impl Read) -> Result<Self>;
2921
2922    #[doc(hidden)]
2923    fn read_be_from(r: &mut impl Read) -> Result<Self>;
2924}
2925
2926macro_rules! impl_from_endian_bytes {
2927    ($($t:ty),*$(,)?) => {$(
2928        #[unstable(feature = "read_le_be_internals", reason = "internals", issue = "none")]
2929        impl FromEndianBytes for $t {
2930            #[inline]
2931            fn read_le_from(r: &mut impl Read) -> Result<Self> {
2932                Ok(<$t>::from_le_bytes(r.read_array()?))
2933            }
2934
2935            #[inline]
2936            fn read_be_from(r: &mut impl Read) -> Result<Self> {
2937                Ok(<$t>::from_be_bytes(r.read_array()?))
2938            }
2939        }
2940    )*};
2941}
2942
2943impl_from_endian_bytes!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64);