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