Skip to main content

alloc/io/
read.rs

1use core::mem::{DropGuard, MaybeUninit};
2
3use crate::io::{
4    BorrowedBuf, BorrowedCursor, Bytes, Chain, Error, IoSliceMut, Result, Take, bytes, chain, take,
5};
6use crate::string::String;
7use crate::vec::Vec;
8
9/// The `Read` trait allows for reading bytes from a source.
10///
11/// Implementors of the `Read` trait are called 'readers'.
12///
13/// Readers are defined by one required method, [`read()`]. Each call to [`read()`]
14/// will attempt to pull bytes from this source into a provided buffer. A
15/// number of other methods are implemented in terms of [`read()`], giving
16/// implementors a number of ways to read bytes while only needing to implement
17/// a single method.
18///
19/// Readers are intended to be composable with one another. Many implementors
20/// throughout [`std::io`] take and provide types which implement the `Read`
21/// trait.
22///
23/// Please note that each call to [`read()`] may involve a system call, and
24/// therefore, using something that implements [`BufRead`], such as
25/// `BufReader`, will be more efficient.
26///
27/// [`BufRead`]: crate::io::BufRead
28///
29/// Repeated calls to the reader use the same cursor, so for example
30/// calling `read_to_end` twice on a `File` will only return the file's
31/// contents once. It's recommended to first call `rewind()` in that case.
32///
33/// # Examples
34///
35/// `File`s implement `Read`:
36///
37/// ```no_run
38/// use std::io;
39/// use std::io::prelude::*;
40/// use std::fs::File;
41///
42/// fn main() -> io::Result<()> {
43///     let mut f = File::open("foo.txt")?;
44///     let mut buffer = [0; 10];
45///
46///     // read up to 10 bytes
47///     f.read(&mut buffer)?;
48///
49///     let mut buffer = Vec::new();
50///     // read the whole file
51///     f.read_to_end(&mut buffer)?;
52///
53///     // read into a String, so that you don't need to do the conversion.
54///     let mut buffer = String::new();
55///     f.read_to_string(&mut buffer)?;
56///
57///     // and more! See the other methods for more details.
58///     Ok(())
59/// }
60/// ```
61///
62/// Read from [`&str`] because [`&[u8]`][prim@slice] implements `Read`:
63///
64/// ```no_run
65/// # use std::io;
66/// use std::io::prelude::*;
67///
68/// fn main() -> io::Result<()> {
69///     let mut b = "This string will be read".as_bytes();
70///     let mut buffer = [0; 10];
71///
72///     // read up to 10 bytes
73///     b.read(&mut buffer)?;
74///
75///     // etc... it works exactly as a File does!
76///     Ok(())
77/// }
78/// ```
79///
80/// [`read()`]: Read::read
81/// [`&str`]: prim@str
82/// [`std::io`]: crate::io
83#[stable(feature = "rust1", since = "1.0.0")]
84#[doc(notable_trait)]
85#[cfg_attr(not(test), rustc_diagnostic_item = "IoRead")]
86#[rustc_must_implement_one_of(read_buf, read)] // Keep this order, it's important for rust-analyzer (the preferred-to-implement method should come first).
87pub trait Read {
88    /// Pull some bytes from this source into the specified buffer, returning
89    /// how many bytes were read.
90    ///
91    /// This function does not provide any guarantees about whether it blocks
92    /// waiting for data, but if an object needs to block for a read and cannot,
93    /// it will typically signal this via an [`Err`] return value.
94    ///
95    /// If the return value of this method is [`Ok(n)`], then implementations must
96    /// guarantee that `0 <= n <= buf.len()`. A nonzero `n` value indicates
97    /// that the buffer `buf` has been filled in with `n` bytes of data from this
98    /// source. If `n` is `0`, then it can indicate one of two scenarios:
99    ///
100    /// 1. This reader has reached its "end of file" and will likely no longer
101    ///    be able to produce bytes. Note that this does not mean that the
102    ///    reader will *always* no longer be able to produce bytes. As an example,
103    ///    on Linux, this method will call the `recv` syscall for a `TcpStream`,
104    ///    where returning zero indicates the connection was shut down correctly. While
105    ///    for `File`, it is possible to reach the end of file and get zero as result,
106    ///    but if more data is appended to the file, future calls to `read` will return
107    ///    more data.
108    /// 2. The buffer specified was 0 bytes in length.
109    ///
110    /// It is not an error if the returned value `n` is smaller than the buffer size,
111    /// even when the reader is not at the end of the stream yet.
112    /// This may happen for example because fewer bytes are actually available right now
113    /// (e. g. being close to end-of-file) or because read() was interrupted by a signal.
114    ///
115    /// As this trait is safe to implement, callers in unsafe code cannot rely on
116    /// `n <= buf.len()` for safety.
117    /// Extra care needs to be taken when `unsafe` functions are used to access the read bytes.
118    /// Callers have to ensure that no unchecked out-of-bounds accesses are possible even if
119    /// `n > buf.len()`.
120    ///
121    /// *Implementations* of this method can make no assumptions about the contents of `buf` when
122    /// this function is called. It is recommended that implementations only write data to `buf`
123    /// instead of reading its contents.
124    ///
125    /// Correspondingly, however, *callers* of this method in unsafe code must not assume
126    /// any guarantees about how the implementation uses `buf`. The trait is safe to implement,
127    /// so it is possible that the code that's supposed to write to the buffer might also read
128    /// from it. It is your responsibility to make sure that `buf` is initialized
129    /// before calling `read`. Calling `read` with an uninitialized `buf` (of the kind one
130    /// obtains via [`MaybeUninit<T>`]) is not safe, and can lead to undefined behavior.
131    ///
132    /// [`MaybeUninit<T>`]: core::mem::MaybeUninit
133    ///
134    /// # Errors
135    ///
136    /// If this function encounters any form of I/O or other error, an error
137    /// variant will be returned. If an error is returned then it must be
138    /// guaranteed that no bytes were read.
139    ///
140    /// An error of the [`ErrorKind::Interrupted`] kind is non-fatal and the read
141    /// operation should be retried if there is nothing else to do.
142    ///
143    /// # Examples
144    ///
145    /// `File`s implement `Read`:
146    ///
147    /// [`Ok(n)`]: Ok
148    /// [`ErrorKind::Interrupted`]: crate::io::ErrorKind::Interrupted
149    ///
150    /// ```no_run
151    /// use std::io;
152    /// use std::io::prelude::*;
153    /// use std::fs::File;
154    ///
155    /// fn main() -> io::Result<()> {
156    ///     let mut f = File::open("foo.txt")?;
157    ///     let mut buffer = [0; 10];
158    ///
159    ///     // read up to 10 bytes
160    ///     let n = f.read(&mut buffer[..])?;
161    ///
162    ///     println!("The bytes: {:?}", &buffer[..n]);
163    ///     Ok(())
164    /// }
165    /// ```
166    #[stable(feature = "rust1", since = "1.0.0")]
167    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
168        let mut buf = BorrowedBuf::from(buf);
169        self.read_buf(buf.unfilled()).map(|()| buf.len())
170    }
171
172    /// Like `read`, except that it reads into a slice of buffers.
173    ///
174    /// Data is copied to fill each buffer in order, with the final buffer
175    /// written to possibly being only partially filled. This method must
176    /// behave equivalently to a single call to `read` with concatenated
177    /// buffers.
178    ///
179    /// The default implementation calls `read` with either the first nonempty
180    /// buffer provided, or an empty one if none exists.
181    #[stable(feature = "iovec", since = "1.36.0")]
182    fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize> {
183        default_read_vectored(|b| self.read(b), bufs)
184    }
185
186    /// Determines if this `Read`er has an efficient `read_vectored`
187    /// implementation.
188    ///
189    /// If a `Read`er does not override the default `read_vectored`
190    /// implementation, code using it may want to avoid the method all together
191    /// and coalesce writes into a single buffer for higher performance.
192    ///
193    /// The default implementation returns `false`.
194    #[unstable(feature = "can_vector", issue = "69941")]
195    fn is_read_vectored(&self) -> bool {
196        false
197    }
198
199    /// Reads all bytes until EOF in this source, placing them into `buf`.
200    ///
201    /// All bytes read from this source will be appended to the specified buffer
202    /// `buf`. This function will continuously call [`read()`] to append more data to
203    /// `buf` until [`read()`] returns either [`Ok(0)`] or an error of
204    /// non-[`ErrorKind::Interrupted`] kind.
205    ///
206    /// If successful, this function will return the total number of bytes read.
207    ///
208    /// # Errors
209    ///
210    /// If this function encounters an error of the kind
211    /// [`ErrorKind::Interrupted`] then the error is ignored and the operation
212    /// will continue.
213    ///
214    /// If any other read error is encountered then this function immediately
215    /// returns. Any bytes which have already been read will be appended to
216    /// `buf`.
217    ///
218    /// # Examples
219    ///
220    /// `File`s implement `Read`:
221    ///
222    /// [`Ok(0)`]: Ok
223    /// [`ErrorKind::Interrupted`]: crate::io::ErrorKind::Interrupted
224    /// [`read()`]: Read::read
225    ///
226    /// ```no_run
227    /// use std::io;
228    /// use std::io::prelude::*;
229    /// use std::fs::File;
230    ///
231    /// fn main() -> io::Result<()> {
232    ///     let mut f = File::open("foo.txt")?;
233    ///     let mut buffer = Vec::new();
234    ///
235    ///     // read the whole file
236    ///     f.read_to_end(&mut buffer)?;
237    ///     Ok(())
238    /// }
239    /// ```
240    ///
241    /// (See also the `std::fs::read` convenience function for reading from a
242    /// file.)
243    ///
244    /// ## Implementing `read_to_end`
245    ///
246    /// When implementing the `io::Read` trait, it is recommended to allocate
247    /// memory using [`Vec::try_reserve`]. However, this behavior is not guaranteed
248    /// by all implementations, and `read_to_end` may not handle out-of-memory
249    /// situations gracefully.
250    ///
251    /// ```no_run
252    /// # #![expect(dead_code)]
253    /// # use std::io::{self, BufRead};
254    /// # struct Example { example_datasource: io::Empty } impl Example {
255    /// # fn get_some_data_for_the_example(&self) -> &'static [u8] { &[] }
256    /// fn read_to_end(&mut self, dest_vec: &mut Vec<u8>) -> io::Result<usize> {
257    ///     let initial_vec_len = dest_vec.len();
258    ///     loop {
259    ///         let src_buf = self.example_datasource.fill_buf()?;
260    ///         if src_buf.is_empty() {
261    ///             break;
262    ///         }
263    ///         dest_vec.try_reserve(src_buf.len())?;
264    ///         dest_vec.extend_from_slice(src_buf);
265    ///
266    ///         // Any irreversible side effects should happen after `try_reserve` succeeds,
267    ///         // to avoid losing data on allocation error.
268    ///         let read = src_buf.len();
269    ///         self.example_datasource.consume(read);
270    ///     }
271    ///     Ok(dest_vec.len() - initial_vec_len)
272    /// }
273    /// # }
274    /// ```
275    ///
276    /// # Usage Notes
277    ///
278    /// `read_to_end` attempts to read a source until EOF, but many sources are continuous streams
279    /// that do not send EOF. In these cases, `read_to_end` will block indefinitely. Standard input
280    /// is one such stream which may be finite if piped, but is typically continuous. For example,
281    /// `cat file | my-rust-program` will correctly terminate with an `EOF` upon closure of cat.
282    /// Reading user input or running programs that remain open indefinitely will never terminate
283    /// the stream with `EOF` (e.g. `yes | my-rust-program`).
284    ///
285    /// Using `.lines()` with a `BufReader` or using [`read`] can provide a better solution
286    ///
287    /// [`read`]: Read::read
288    /// [`Vec::try_reserve`]: crate::vec::Vec::try_reserve
289    #[stable(feature = "rust1", since = "1.0.0")]
290    fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
291        default_read_to_end(self, buf, None)
292    }
293
294    /// Reads all bytes until EOF in this source, appending them to `buf`.
295    ///
296    /// If successful, this function returns the number of bytes which were read
297    /// and appended to `buf`.
298    ///
299    /// # Errors
300    ///
301    /// If the data in this stream is *not* valid UTF-8 then an error is
302    /// returned and `buf` is unchanged.
303    ///
304    /// See [`read_to_end`] for other error semantics.
305    ///
306    /// [`read_to_end`]: Read::read_to_end
307    ///
308    /// # Examples
309    ///
310    /// `File`s implement `Read`:
311    ///
312    /// ```no_run
313    /// use std::io;
314    /// use std::io::prelude::*;
315    /// use std::fs::File;
316    ///
317    /// fn main() -> io::Result<()> {
318    ///     let mut f = File::open("foo.txt")?;
319    ///     let mut buffer = String::new();
320    ///
321    ///     f.read_to_string(&mut buffer)?;
322    ///     Ok(())
323    /// }
324    /// ```
325    ///
326    /// (See also the `std::fs::read_to_string` convenience function for
327    /// reading from a file.)
328    ///
329    /// # Usage Notes
330    ///
331    /// `read_to_string` attempts to read a source until EOF, but many sources are continuous streams
332    /// that do not send EOF. In these cases, `read_to_string` will block indefinitely. Standard input
333    /// is one such stream which may be finite if piped, but is typically continuous. For example,
334    /// `cat file | my-rust-program` will correctly terminate with an `EOF` upon closure of cat.
335    /// Reading user input or running programs that remain open indefinitely will never terminate
336    /// the stream with `EOF` (e.g. `yes | my-rust-program`).
337    ///
338    /// Using `.lines()` with a `BufReader` or using [`read`] can provide a better solution
339    ///
340    /// [`read`]: Read::read
341    #[stable(feature = "rust1", since = "1.0.0")]
342    fn read_to_string(&mut self, buf: &mut String) -> Result<usize> {
343        default_read_to_string(self, buf, None)
344    }
345
346    /// Reads the exact number of bytes required to fill `buf`.
347    ///
348    /// This function reads as many bytes as necessary to completely fill the
349    /// specified buffer `buf`.
350    ///
351    /// *Implementations* of this method can make no assumptions about the contents of `buf` when
352    /// this function is called. It is recommended that implementations only write data to `buf`
353    /// instead of reading its contents. The documentation on [`read`] has a more detailed
354    /// explanation of this subject.
355    ///
356    /// # Errors
357    ///
358    /// If this function encounters an error of the kind
359    /// [`ErrorKind::Interrupted`] then the error is ignored and the operation
360    /// will continue.
361    ///
362    /// If this function encounters an "end of file" before completely filling
363    /// the buffer, it returns an error of the kind [`ErrorKind::UnexpectedEof`].
364    /// The contents of `buf` are unspecified in this case.
365    ///
366    /// If any other read error is encountered then this function immediately
367    /// returns. The contents of `buf` are unspecified in this case.
368    ///
369    /// If this function returns an error, it is unspecified how many bytes it
370    /// has read, but it will never read more than would be necessary to
371    /// completely fill the buffer.
372    ///
373    /// # Examples
374    ///
375    /// `File`s implement `Read`:
376    ///
377    /// [`ErrorKind::Interrupted`]: crate::io::ErrorKind::Interrupted
378    /// [`ErrorKind::UnexpectedEof`]: crate::io::ErrorKind::UnexpectedEof
379    /// [`read`]: Read::read
380    ///
381    /// ```no_run
382    /// use std::io;
383    /// use std::io::prelude::*;
384    /// use std::fs::File;
385    ///
386    /// fn main() -> io::Result<()> {
387    ///     let mut f = File::open("foo.txt")?;
388    ///     let mut buffer = [0; 10];
389    ///
390    ///     // read exactly 10 bytes
391    ///     f.read_exact(&mut buffer)?;
392    ///     Ok(())
393    /// }
394    /// ```
395    #[stable(feature = "read_exact", since = "1.6.0")]
396    fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
397        default_read_exact(self, buf)
398    }
399
400    /// Pull some bytes from this source into the specified buffer.
401    ///
402    /// This is equivalent to the [`read`](Read::read) method, except that it is passed a [`BorrowedCursor`] rather than `[u8]` to allow use
403    /// with uninitialized buffers. The new data will be appended to any existing contents of `buf`.
404    ///
405    /// The default implementation delegates to `read`.
406    ///
407    /// This method makes it possible to return both data and an error but it is advised against.
408    #[unstable(feature = "read_buf", issue = "78485")]
409    fn read_buf(&mut self, buf: BorrowedCursor<'_, u8>) -> Result<()> {
410        default_read_buf(|b| self.read(b), buf)
411    }
412
413    /// Reads the exact number of bytes required to fill `cursor`.
414    ///
415    /// This is similar to the [`read_exact`](Read::read_exact) method, except
416    /// that it is passed a [`BorrowedCursor`] rather than `[u8]` to allow use
417    /// with uninitialized buffers.
418    ///
419    /// # Errors
420    ///
421    /// If this function encounters an error of the kind [`ErrorKind::Interrupted`]
422    /// then the error is ignored and the operation will continue.
423    ///
424    /// If this function encounters an "end of file" before completely filling
425    /// the buffer, it returns an error of the kind [`ErrorKind::UnexpectedEof`].
426    ///
427    /// If any other read error is encountered then this function immediately
428    /// returns.
429    ///
430    /// If this function returns an error, all bytes read will be appended to `cursor`.
431    ///
432    /// [`ErrorKind::Interrupted`]: crate::io::ErrorKind::Interrupted
433    /// [`ErrorKind::UnexpectedEof`]: crate::io::ErrorKind::UnexpectedEof
434    #[unstable(feature = "read_buf", issue = "78485")]
435    #[doc(alias("read_exact_buf"))]
436    fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_, u8>) -> Result<()> {
437        default_read_buf_exact(self, cursor)
438    }
439
440    /// Creates a "by reference" adapter for this instance of `Read`.
441    ///
442    /// The returned adapter also implements `Read` and will simply borrow this
443    /// current reader.
444    ///
445    /// # Examples
446    ///
447    /// `File`s implement `Read`:
448    ///
449    /// ```no_run
450    /// use std::io;
451    /// use std::io::Read;
452    /// use std::fs::File;
453    ///
454    /// fn main() -> io::Result<()> {
455    ///     let mut f = File::open("foo.txt")?;
456    ///     let mut buffer = Vec::new();
457    ///     let mut other_buffer = Vec::new();
458    ///
459    ///     {
460    ///         let reference = f.by_ref();
461    ///
462    ///         // read at most 5 bytes
463    ///         reference.take(5).read_to_end(&mut buffer)?;
464    ///
465    ///     } // drop our &mut reference so we can use f again
466    ///
467    ///     // original file still usable, read the rest
468    ///     f.read_to_end(&mut other_buffer)?;
469    ///     Ok(())
470    /// }
471    /// ```
472    #[stable(feature = "rust1", since = "1.0.0")]
473    fn by_ref(&mut self) -> &mut Self
474    where
475        Self: Sized,
476    {
477        self
478    }
479
480    /// Transforms this `Read` instance to an [`Iterator`] over its bytes.
481    ///
482    /// The returned type implements [`Iterator`] where the [`Item`] is
483    /// <code>[Result]<[u8], [io::Error]></code>.
484    /// The yielded item is [`Ok`] if a byte was successfully read and [`Err`]
485    /// otherwise. EOF is mapped to returning [`None`] from this iterator.
486    ///
487    /// The default implementation calls `read` for each byte,
488    /// which can be very inefficient for data that's not in memory,
489    /// such as `File`. Consider using a `BufReader` in such cases.
490    ///
491    /// # Examples
492    ///
493    /// `File`s implement `Read`:
494    ///
495    /// [`Item`]: Iterator::Item
496    /// [Result]: core::result::Result "Result"
497    /// [io::Error]: crate::io::Error "io::Error"
498    ///
499    /// ```no_run
500    /// use std::io;
501    /// use std::io::prelude::*;
502    /// use std::io::BufReader;
503    /// use std::fs::File;
504    ///
505    /// fn main() -> io::Result<()> {
506    ///     let f = BufReader::new(File::open("foo.txt")?);
507    ///
508    ///     for byte in f.bytes() {
509    ///         println!("{}", byte?);
510    ///     }
511    ///     Ok(())
512    /// }
513    /// ```
514    #[stable(feature = "rust1", since = "1.0.0")]
515    fn bytes(self) -> Bytes<Self>
516    where
517        Self: Sized,
518    {
519        bytes(self)
520    }
521
522    /// Creates an adapter which will chain this stream with another.
523    ///
524    /// The returned `Read` instance will first read all bytes from this object
525    /// until EOF is encountered. Afterwards the output is equivalent to the
526    /// output of `next`.
527    ///
528    /// # Examples
529    ///
530    /// `File`s implement `Read`:
531    ///
532    /// ```no_run
533    /// use std::io;
534    /// use std::io::prelude::*;
535    /// use std::fs::File;
536    ///
537    /// fn main() -> io::Result<()> {
538    ///     let f1 = File::open("foo.txt")?;
539    ///     let f2 = File::open("bar.txt")?;
540    ///
541    ///     let mut handle = f1.chain(f2);
542    ///     let mut buffer = String::new();
543    ///
544    ///     // read the value into a String. We could use any Read method here,
545    ///     // this is just one example.
546    ///     handle.read_to_string(&mut buffer)?;
547    ///     Ok(())
548    /// }
549    /// ```
550    #[stable(feature = "rust1", since = "1.0.0")]
551    fn chain<R: Read>(self, next: R) -> Chain<Self, R>
552    where
553        Self: Sized,
554    {
555        chain(self, next)
556    }
557
558    /// Creates an adapter which will read at most `limit` bytes from it.
559    ///
560    /// This function returns a new instance of `Read` which will read at most
561    /// `limit` bytes, after which it will always return EOF ([`Ok(0)`]). Any
562    /// read errors will not count towards the number of bytes read and future
563    /// calls to [`read()`] may succeed.
564    ///
565    /// # Examples
566    ///
567    /// `File`s implement `Read`:
568    ///
569    /// [`Ok(0)`]: Ok
570    /// [`read()`]: Read::read
571    ///
572    /// ```no_run
573    /// use std::io;
574    /// use std::io::prelude::*;
575    /// use std::fs::File;
576    ///
577    /// fn main() -> io::Result<()> {
578    ///     let f = File::open("foo.txt")?;
579    ///     let mut buffer = [0; 5];
580    ///
581    ///     // read at most five bytes
582    ///     let mut handle = f.take(5);
583    ///
584    ///     handle.read(&mut buffer)?;
585    ///     Ok(())
586    /// }
587    /// ```
588    #[stable(feature = "rust1", since = "1.0.0")]
589    fn take(self, limit: u64) -> Take<Self>
590    where
591        Self: Sized,
592    {
593        take(self, limit)
594    }
595
596    /// Read and return a fixed array of bytes from this source.
597    ///
598    /// This function uses an array sized based on a const generic size known at compile time. You
599    /// can specify the size with turbofish (`reader.read_array::<8>()`), or let type inference
600    /// determine the number of bytes needed based on how the return value gets used. For instance,
601    /// this function works well with functions like [`u64::from_le_bytes`] to turn an array of
602    /// bytes into an integer of the same size.
603    ///
604    /// Like `read_exact`, if this function encounters an "end of file" before reading the desired
605    /// number of bytes, it returns an error of the kind [`ErrorKind::UnexpectedEof`].
606    ///
607    /// [`ErrorKind::UnexpectedEof`]: crate::io::ErrorKind::UnexpectedEof
608    ///
609    /// ```
610    /// #![feature(read_array)]
611    /// use std::io::Cursor;
612    /// use std::io::prelude::*;
613    ///
614    /// fn main() -> std::io::Result<()> {
615    ///     let mut buf = Cursor::new([1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2]);
616    ///     let x = u64::from_le_bytes(buf.read_array()?);
617    ///     let y = u32::from_be_bytes(buf.read_array()?);
618    ///     let z = u16::from_be_bytes(buf.read_array()?);
619    ///     assert_eq!(x, 0x807060504030201);
620    ///     assert_eq!(y, 0x9080706);
621    ///     assert_eq!(z, 0x504);
622    ///     Ok(())
623    /// }
624    /// ```
625    #[unstable(feature = "read_array", issue = "148848")]
626    fn read_array<const N: usize>(&mut self) -> Result<[u8; N]>
627    where
628        Self: Sized,
629    {
630        let mut buf = [MaybeUninit::uninit(); N];
631        let mut borrowed_buf = BorrowedBuf::from(buf.as_mut_slice());
632        self.read_buf_exact(borrowed_buf.unfilled())?;
633        // Guard against incorrect `read_buf_exact` implementations.
634        assert_eq!(borrowed_buf.len(), N);
635        Ok(unsafe { MaybeUninit::array_assume_init(buf) })
636    }
637
638    /// Read and return a type (e.g. an integer) in little-endian order.
639    ///
640    /// You can specify the type with turbofish (`reader.read_le::<u64>()`), or let type inference
641    /// determine the type based on how the return value gets used.
642    ///
643    /// Like `read_exact`, if this function encounters an "end of file" before reading the desired
644    /// number of bytes, it returns an error of the kind [`ErrorKind::UnexpectedEof`].
645    ///
646    /// [`ErrorKind::UnexpectedEof`]: crate::io::ErrorKind::UnexpectedEof
647    ///
648    /// ```
649    /// #![feature(read_le)]
650    /// use std::io::Cursor;
651    /// use std::io::prelude::*;
652    ///
653    /// fn main() -> std::io::Result<()> {
654    ///     let mut buf = Cursor::new([1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2]);
655    ///     let x: u64 = buf.read_le()?;
656    ///     let y: u32 = buf.read_le()?;
657    ///     let z = buf.read_le::<u16>()?;
658    ///     assert_eq!(x, 0x807060504030201);
659    ///     assert_eq!(y, 0x6070809);
660    ///     assert_eq!(z, 0x405);
661    ///     Ok(())
662    /// }
663    /// ```
664    #[unstable(feature = "read_le", issue = "156984")]
665    #[inline]
666    fn read_le<T: FromEndianBytes>(&mut self) -> Result<T>
667    where
668        Self: Sized,
669    {
670        T::read_le_from(self)
671    }
672
673    /// Read and return a type (e.g. an integer) in big-endian order.
674    ///
675    /// You can specify the type with turbofish (`reader.read_be::<u64>()`), or let type inference
676    /// determine the type based on how the return value gets used.
677    ///
678    /// Like `read_exact`, if this function encounters an "end of file" before reading the desired
679    /// number of bytes, it returns an error of the kind [`ErrorKind::UnexpectedEof`].
680    ///
681    /// [`ErrorKind::UnexpectedEof`]: crate::io::ErrorKind::UnexpectedEof
682    ///
683    /// ```
684    /// #![feature(read_le)]
685    /// use std::io::Cursor;
686    /// use std::io::prelude::*;
687    ///
688    /// fn main() -> std::io::Result<()> {
689    ///     let mut buf = Cursor::new([1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2]);
690    ///     let x: u64 = buf.read_be()?;
691    ///     let y: u32 = buf.read_be()?;
692    ///     let z = buf.read_be::<u16>()?;
693    ///     assert_eq!(x, 0x102030405060708);
694    ///     assert_eq!(y, 0x9080706);
695    ///     assert_eq!(z, 0x504);
696    ///     Ok(())
697    /// }
698    /// ```
699    #[unstable(feature = "read_le", issue = "156984")]
700    #[inline]
701    fn read_be<T: FromEndianBytes>(&mut self) -> Result<T>
702    where
703        Self: Sized,
704    {
705        T::read_be_from(self)
706    }
707}
708
709/// Reads all bytes from a [reader][Read] into a new [`String`].
710///
711/// This is a convenience function for [`Read::read_to_string`]. Using this
712/// function avoids having to create a variable first and provides more type
713/// safety since you can only get the buffer out if there were no errors. (If you
714/// use [`Read::read_to_string`] you have to remember to check whether the read
715/// succeeded because otherwise your buffer will be empty or only partially full.)
716///
717/// # Performance
718///
719/// The downside of this function's increased ease of use and type safety is
720/// that it gives you less control over performance. For example, you can't
721/// pre-allocate memory like you can using [`String::with_capacity`] and
722/// [`Read::read_to_string`]. Also, you can't re-use the buffer if an error
723/// occurs while reading.
724///
725/// In many cases, this function's performance will be adequate and the ease of use
726/// and type safety tradeoffs will be worth it. However, there are cases where you
727/// need more control over performance, and in those cases you should definitely use
728/// [`Read::read_to_string`] directly.
729///
730/// Note that in some special cases, such as when reading files, this function will
731/// pre-allocate memory based on the size of the input it is reading. In those
732/// cases, the performance should be as good as if you had used
733/// [`Read::read_to_string`] with a manually pre-allocated buffer.
734///
735/// # Errors
736///
737/// This function forces you to handle errors because the output (the `String`)
738/// is wrapped in a [`Result`]. See [`Read::read_to_string`] for the errors
739/// that can occur. If any error occurs, you will get an [`Err`], so you
740/// don't have to worry about your buffer being empty or partially full.
741///
742/// # Examples
743///
744/// ```no_run
745/// # use std::io;
746/// fn main() -> io::Result<()> {
747///     let stdin = io::read_to_string(io::stdin())?;
748///     println!("Stdin was:");
749///     println!("{stdin}");
750///     Ok(())
751/// }
752/// ```
753///
754/// # Usage Notes
755///
756/// `read_to_string` attempts to read a source until EOF, but many sources are continuous streams
757/// that do not send EOF. In these cases, `read_to_string` will block indefinitely. Standard input
758/// is one such stream which may be finite if piped, but is typically continuous. For example,
759/// `cat file | my-rust-program` will correctly terminate with an `EOF` upon closure of cat.
760/// Reading user input or running programs that remain open indefinitely will never terminate
761/// the stream with `EOF` (e.g. `yes | my-rust-program`).
762///
763/// Using `.lines()` with a `BufReader` or using [`read`] can provide a better solution
764///
765/// [`read`]: Read::read
766///
767#[stable(feature = "io_read_to_string", since = "1.65.0")]
768pub fn read_to_string<R: Read>(mut reader: R) -> Result<String> {
769    let mut buf = String::new();
770    reader.read_to_string(&mut buf)?;
771    Ok(buf)
772}
773
774/// Bare metal platforms usually have very small amounts of RAM
775/// (in the order of hundreds of KB)
776#[doc(hidden)]
777#[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
778pub const DEFAULT_BUF_SIZE: usize = cfg_select! {
779    target_os = "espidf" => { 512 },
780    _ => { 8 * 1024 }
781};
782
783/// Several `read_to_string` and `read_line` methods in the standard library will
784/// append data into a `String` buffer, but we need to be pretty careful when
785/// doing this. The implementation will just call `.as_mut_vec()` and then
786/// delegate to a byte-oriented reading method, but we must ensure that when
787/// returning we never leave `buf` in a state such that it contains invalid UTF-8
788/// in its bounds.
789///
790/// To this end, we use an RAII guard (to protect against panics) which updates
791/// the length of the string when it is dropped. This guard initially truncates
792/// the string to the prior length and only after we've validated that the
793/// new contents are valid UTF-8 do we allow it to set a longer length.
794///
795/// The unsafety in this function is twofold:
796///
797/// 1. We're looking at the raw bytes of `buf`, so we take on the burden of UTF-8
798///    checks.
799/// 2. We're passing a raw buffer to the function `f`, and it is expected that
800///    the function only *appends* bytes to the buffer. We'll get undefined
801///    behavior if existing bytes are overwritten to have non-UTF-8 data.
802pub(super) unsafe fn append_to_string<F>(buf: &mut String, f: F) -> Result<usize>
803where
804    F: FnOnce(&mut Vec<u8>) -> Result<usize>,
805{
806    let len_original = buf.len();
807    // SAFETY: invalid UTF-8 discarded before return or unwind
808    let buf_vec = unsafe { buf.as_mut_vec() };
809    let mut g = DropGuard::new((len_original, buf_vec), |(len, buf)| unsafe {
810        buf.set_len(len);
811    });
812    let ret = f(g.1);
813
814    // SAFETY: the caller promises to only append data to `buf`
815    let appended = unsafe { g.1.get_unchecked(g.0..) };
816    if str::from_utf8(appended).is_err() {
817        ret.and_then(|_| Err(Error::INVALID_UTF8))
818    } else {
819        g.0 = g.1.len();
820        ret
821    }
822}
823
824/// Here we must serve many masters with conflicting goals:
825///
826/// - avoid allocating unless necessary
827/// - avoid overallocating if we know the exact size (#89165)
828/// - avoid passing large buffers to readers that always initialize the free capacity if they perform short reads (#23815, #23820)
829/// - avoid re-initializing unfilled bytes into the spare buffer if we initialized >PROBE_SIZE unfilled bytes in a previous loop (#158008)
830/// - pass large buffers to readers that do not initialize the spare capacity. this can amortize per-call overheads
831/// - pass not-too-small and not-too-large buffers to Windows read APIs because they manage to suffer from both problems
832///   at the same time, i.e. small reads suffer from syscall overhead, all reads incur costs proportional to buffer size (#110650)
833/// - also avoid <4 byte reads as this may split UTF-8 code points, which can be a problem for Windows console reads (#142847)
834#[doc(hidden)]
835#[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
836pub fn default_read_to_end<R: Read + ?Sized>(
837    r: &mut R,
838    buf: &mut Vec<u8>,
839    size_hint: Option<usize>,
840) -> Result<usize> {
841    let start_len = buf.len();
842    let start_cap = buf.capacity();
843    // Optionally limit the maximum bytes read on each iteration.
844    // This adds an arbitrary fiddle factor to allow for more data than we expect.
845    let mut max_read_size = size_hint
846        .and_then(|s| s.checked_add(1024)?.checked_next_multiple_of(DEFAULT_BUF_SIZE))
847        .unwrap_or(DEFAULT_BUF_SIZE);
848
849    // Tracks how many bytes are initialized in the buffer
850    let mut init_until = buf.len();
851
852    const PROBE_SIZE: usize = 32;
853
854    fn small_probe_read<R: Read + ?Sized>(r: &mut R, buf: &mut Vec<u8>) -> Result<usize> {
855        let mut probe = [0u8; PROBE_SIZE];
856
857        loop {
858            cfg_select! {
859                no_global_oom_handling => {
860                    // Without global OOM handling we must proactively allocate the buffer
861                    // to avoid failing after already reading data.
862                    buf.try_reserve(PROBE_SIZE)?;
863                }
864                _ => {}
865            }
866
867            match r.read(&mut probe) {
868                Ok(n) => {
869                    cfg_select! {
870                        no_global_oom_handling => {
871                            // there is no way to recover from allocation failure here
872                            // because the data has already been read.
873                            buf.try_extend_from_slice_of_bytes(&probe[..n])?;
874                        }
875                        _ => {
876                            // there is no way to recover from allocation failure here
877                            // because the data has already been read.
878                            buf.extend_from_slice(&probe[..n]);
879                        }
880                    }
881                    return Ok(n);
882                }
883                Err(ref e) if e.is_interrupted() => continue,
884                Err(e) => return Err(e),
885            }
886        }
887    }
888
889    // avoid inflating empty/small vecs before we have determined that there's anything to read
890    if (size_hint.is_none() || size_hint == Some(0)) && buf.capacity() - buf.len() < PROBE_SIZE {
891        let read = small_probe_read(r, buf)?;
892
893        if read == 0 {
894            return Ok(0);
895        }
896    }
897
898    loop {
899        if buf.spare_capacity_mut().len() < PROBE_SIZE && buf.capacity() == start_cap {
900            // The buffer might be an exact fit. Let's read into a probe buffer
901            // and see if it returns `Ok(0)`. If so, we've avoided an
902            // unnecessary doubling of the capacity. But if not, append the
903            // probe buffer to the primary buffer and let its capacity grow.
904            let read = small_probe_read(r, buf)?;
905
906            if read == 0 {
907                return Ok(buf.len() - start_len);
908            }
909
910            init_until = buf.len();
911            // In the case of very short reads, continue to use the stack buffer
912            // until either we reach the end or we need to reallocate.
913            continue;
914        }
915
916        // Avoid unnecessarily short reads by ensuring there's at least PROBE_SIZE space available.
917        // And assert that PROBE_SIZE is always at least large enough to fit any UTF-8 encoded code point.
918        const { assert!(PROBE_SIZE >= char::MAX_LEN_UTF8) }
919        if buf.spare_capacity_mut().len() < PROBE_SIZE {
920            buf.try_reserve(PROBE_SIZE)?;
921            // When reallocation occurs, we have to update init_until accordingly
922            // to re-calibrate how many bytes are actually initialized in the buffer
923            init_until = buf.len();
924        }
925
926        // We set a threshold of >PROBE_SIZE initialized yet unfilled bytes left in the
927        // spare buffer before determining that we need to initialize more bytes into
928        // the spare buffer
929        let buf_len = if init_until > buf.len() + PROBE_SIZE {
930            init_until - buf.len()
931        } else {
932            usize::min(max_read_size, buf.capacity() - buf.len())
933        };
934        let was_init = init_until >= buf.len() + buf_len;
935
936        let mut spare = buf.spare_capacity_mut();
937        spare = &mut spare[..buf_len];
938        let mut read_buf: BorrowedBuf<'_, u8> = spare.into();
939
940        if was_init {
941            // SAFETY: These bytes were initialized but not filled in the previous loop
942            unsafe { read_buf.set_init() };
943        }
944
945        let mut cursor = read_buf.unfilled();
946        let result = loop {
947            match r.read_buf(cursor.reborrow()) {
948                Err(e) if e.is_interrupted() => continue,
949                // Do not stop now in case of error: we might have received both data
950                // and an error
951                res => break res,
952            }
953        };
954
955        let bytes_read = cursor.written();
956        let is_init = read_buf.is_init();
957
958        if is_init {
959            init_until = buf.len() + buf_len;
960        }
961
962        // SAFETY: BorrowedBuf's invariants mean this much memory is initialized.
963        unsafe {
964            let new_len = bytes_read + buf.len();
965            buf.set_len(new_len);
966        }
967
968        // Now that all data is pushed to the vector, we can fail without data loss
969        result?;
970
971        if bytes_read == 0 {
972            return Ok(buf.len() - start_len);
973        }
974
975        // Use heuristics to determine the max read size if no initial size hint was provided
976        if size_hint.is_none() {
977            // The reader is returning short reads but it doesn't call ensure_init().
978            // In that case we no longer need to restrict read sizes to avoid
979            // initialization costs.
980            // When reading from disk we usually don't get any short reads except at EOF.
981            // So we wait for at least 2 short reads before uncapping the read buffer;
982            // this helps with the Windows issue.
983            if !is_init {
984                max_read_size = usize::MAX;
985            }
986            // the spare buffer has initialized and read in `max_read_size` bytes.
987            // it's possible that we have more than `max_read_size` bytes to read
988            // left, so a larger buffer may be necessary to minimize the number of
989            // iterations of reading in bytes to the buffer
990            else if bytes_read == max_read_size {
991                max_read_size = max_read_size.saturating_mul(2);
992            }
993        }
994    }
995}
996
997#[doc(hidden)]
998#[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
999pub fn default_read_to_string<R: Read + ?Sized>(
1000    r: &mut R,
1001    buf: &mut String,
1002    size_hint: Option<usize>,
1003) -> Result<usize> {
1004    // Note that we do *not* call `r.read_to_end()` here. We are passing
1005    // `&mut Vec<u8>` (the raw contents of `buf`) into the `read_to_end`
1006    // method to fill it up. An arbitrary implementation could overwrite the
1007    // entire contents of the vector, not just append to it (which is what
1008    // we are expecting).
1009    //
1010    // To prevent extraneously checking the UTF-8-ness of the entire buffer
1011    // we pass it to our hardcoded `default_read_to_end` implementation which
1012    // we know is guaranteed to only read data into the end of the buffer.
1013    unsafe { append_to_string(buf, |b| default_read_to_end(r, b, size_hint)) }
1014}
1015
1016#[doc(hidden)]
1017#[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
1018pub fn default_read_vectored<F>(read: F, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
1019where
1020    F: FnOnce(&mut [u8]) -> Result<usize>,
1021{
1022    let buf = bufs.iter_mut().find(|b| !b.is_empty()).map_or(&mut [][..], |b| &mut **b);
1023    read(buf)
1024}
1025
1026pub(super) fn default_read_exact<R: Read + ?Sized>(this: &mut R, mut buf: &mut [u8]) -> Result<()> {
1027    while !buf.is_empty() {
1028        match this.read(buf) {
1029            Ok(0) => break,
1030            Ok(n) => {
1031                buf = &mut buf[n..];
1032            }
1033            Err(ref e) if e.is_interrupted() => {}
1034            Err(e) => return Err(e),
1035        }
1036    }
1037    if !buf.is_empty() { Err(Error::READ_EXACT_EOF) } else { Ok(()) }
1038}
1039
1040#[doc(hidden)]
1041#[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
1042pub fn default_read_buf<F>(read: F, mut cursor: BorrowedCursor<'_, u8>) -> Result<()>
1043where
1044    F: FnOnce(&mut [u8]) -> Result<usize>,
1045{
1046    let n = read(cursor.ensure_init())?;
1047    cursor.advance_checked(n);
1048    Ok(())
1049}
1050
1051pub(super) fn default_read_buf_exact<R: Read + ?Sized>(
1052    this: &mut R,
1053    mut cursor: BorrowedCursor<'_, u8>,
1054) -> Result<()> {
1055    while cursor.capacity() > 0 {
1056        let prev_written = cursor.written();
1057        match this.read_buf(cursor.reborrow()) {
1058            Ok(()) => {}
1059            Err(e) if e.is_interrupted() => continue,
1060            Err(e) => return Err(e),
1061        }
1062
1063        if cursor.written() == prev_written {
1064            return Err(Error::READ_EXACT_EOF);
1065        }
1066    }
1067
1068    Ok(())
1069}
1070
1071/// Trait for types that can be converted from a fixed-size byte array with a specified endianness
1072#[unstable(feature = "read_le_be_internals", reason = "internals", issue = "none")]
1073// Once we can use associated consts in the types of method parameters, rewrite this to have
1074// `from_le_bytes` and `from_be_bytes` methods, move it to `core`, and make it public.
1075pub impl(self) trait FromEndianBytes: Sized {
1076    #[doc(hidden)]
1077    fn read_le_from(r: &mut impl Read) -> Result<Self>;
1078
1079    #[doc(hidden)]
1080    fn read_be_from(r: &mut impl Read) -> Result<Self>;
1081}
1082
1083macro_rules! impl_from_endian_bytes {
1084    ($($t:ty),*$(,)?) => {$(
1085        #[unstable(feature = "read_le_be_internals", reason = "internals", issue = "none")]
1086        impl FromEndianBytes for $t {
1087            #[inline]
1088            fn read_le_from(r: &mut impl Read) -> Result<Self> {
1089                Ok(<$t>::from_le_bytes(r.read_array()?))
1090            }
1091
1092            #[inline]
1093            fn read_be_from(r: &mut impl Read) -> Result<Self> {
1094                Ok(<$t>::from_be_bytes(r.read_array()?))
1095            }
1096        }
1097    )*};
1098}
1099
1100impl_from_endian_bytes!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64);