ckb_rust_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
294mod cherry_picking;
295
296#[cfg(test)]
297mod tests;
298
299pub use self::buffered::WriterPanicked;
300pub use self::{
301 buffered::{BufReader, BufWriter, IntoInnerError, LineWriter},
302 copy::copy,
303 cursor::Cursor,
304 error::{Error, ErrorKind, Result},
305 util::{empty, repeat, sink, Empty, Repeat, Sink},
306};
307pub use crate::io::cherry_picking::borrowed_buf::{BorrowedBuf, BorrowedCursor};
308use alloc::boxed::Box;
309use alloc::fmt;
310use alloc::str;
311use alloc::string::String;
312use alloc::vec::Vec;
313use cherry_picking::memchr;
314use core::{cmp, slice};
315mod buffered;
316pub(crate) mod copy;
317mod cursor;
318pub mod error;
319mod impls;
320pub mod prelude;
321mod util;
322pub(crate) use crate::const_io_error;
323
324const DEFAULT_BUF_SIZE: usize = 1024;
325
326struct Guard<'a> {
327 buf: &'a mut Vec<u8>,
328 len: usize,
329}
330impl Drop for Guard<'_> {
331 fn drop(&mut self) {
332 unsafe {
333 self.buf.set_len(self.len);
334 }
335 }
336}
337
338// Several `read_to_string` and `read_line` methods in the standard library will
339// append data into a `String` buffer, but we need to be pretty careful when
340// doing this. The implementation will just call `.as_mut_vec()` and then
341// delegate to a byte-oriented reading method, but we must ensure that when
342// returning we never leave `buf` in a state such that it contains invalid UTF-8
343// in its bounds.
344//
345// To this end, we use an RAII guard (to protect against panics) which updates
346// the length of the string when it is dropped. This guard initially truncates
347// the string to the prior length and only after we've validated that the
348// new contents are valid UTF-8 do we allow it to set a longer length.
349//
350// The unsafety in this function is twofold:
351//
352// 1. We're looking at the raw bytes of `buf`, so we take on the burden of UTF-8
353// checks.
354// 2. We're passing a raw buffer to the function `f`, and it is expected that
355// the function only *appends* bytes to the buffer. We'll get undefined
356// behavior if existing bytes are overwritten to have non-UTF-8 data.
357pub(crate) unsafe fn append_to_string<F>(buf: &mut String, f: F) -> Result<usize>
358where
359 F: FnOnce(&mut Vec<u8>) -> Result<usize>,
360{
361 let mut g = Guard {
362 len: buf.len(),
363 buf: unsafe { buf.as_mut_vec() },
364 };
365 let ret = f(g.buf);
366
367 // SAFETY: the caller promises to only append data to `buf`
368 let appended = unsafe { g.buf.get_unchecked(g.len..) };
369 if str::from_utf8(appended).is_err() {
370 ret.and(Err(Error::INVALID_UTF8))
371 } else {
372 g.len = g.buf.len();
373 ret
374 }
375}
376
377// Here we must serve many masters with conflicting goals:
378//
379// - avoid allocating unless necessary
380// - avoid overallocating if we know the exact size (#89165)
381// - avoid passing large buffers to readers that always initialize the free capacity if they perform short reads (#23815, #23820)
382// - pass large buffers to readers that do not initialize the spare capacity. this can amortize per-call overheads
383// - and finally pass not-too-small and not-too-large buffers to Windows read APIs because they manage to suffer from both problems
384// at the same time, i.e. small reads suffer from syscall overhead, all reads incur initialization cost
385// proportional to buffer size (#110650)
386//
387pub(crate) fn default_read_to_end<R: Read + ?Sized>(
388 r: &mut R,
389 buf: &mut Vec<u8>,
390 size_hint: Option<usize>,
391) -> Result<usize> {
392 let start_len = buf.len();
393 let start_cap = buf.capacity();
394 // Optionally limit the maximum bytes read on each iteration.
395 // This adds an arbitrary fiddle factor to allow for more data than we expect.
396 let mut max_read_size = size_hint
397 .and_then(|s| {
398 s.checked_add(1024)?
399 .checked_next_multiple_of(DEFAULT_BUF_SIZE)
400 })
401 .unwrap_or(DEFAULT_BUF_SIZE);
402
403 let mut initialized = 0; // Extra initialized bytes from previous loop iteration
404
405 const PROBE_SIZE: usize = 32;
406
407 fn small_probe_read<R: Read + ?Sized>(r: &mut R, buf: &mut Vec<u8>) -> Result<usize> {
408 let mut probe = [0u8; PROBE_SIZE];
409
410 loop {
411 match r.read(&mut probe) {
412 Ok(n) => {
413 // there is no way to recover from allocation failure here
414 // because the data has already been read.
415 buf.extend_from_slice(&probe[..n]);
416 return Ok(n);
417 }
418 Err(ref e) if e.is_interrupted() => continue,
419 Err(e) => return Err(e),
420 }
421 }
422 }
423
424 // avoid inflating empty/small vecs before we have determined that there's anything to read
425 if (size_hint.is_none() || size_hint == Some(0)) && buf.capacity() - buf.len() < PROBE_SIZE {
426 let read = small_probe_read(r, buf)?;
427
428 if read == 0 {
429 return Ok(0);
430 }
431 }
432
433 loop {
434 if buf.len() == buf.capacity() && buf.capacity() == start_cap {
435 // The buffer might be an exact fit. Let's read into a probe buffer
436 // and see if it returns `Ok(0)`. If so, we've avoided an
437 // unnecessary doubling of the capacity. But if not, append the
438 // probe buffer to the primary buffer and let its capacity grow.
439 let read = small_probe_read(r, buf)?;
440
441 if read == 0 {
442 return Ok(buf.len() - start_len);
443 }
444 }
445
446 if buf.len() == buf.capacity() {
447 // buf is full, need more space
448 buf.try_reserve(PROBE_SIZE)?;
449 }
450 let mut spare = buf.spare_capacity_mut();
451 let buf_len = cmp::min(spare.len(), max_read_size);
452 spare = &mut spare[..buf_len];
453 let mut read_buf: BorrowedBuf<'_> = spare.into();
454
455 // SAFETY: These bytes were initialized but not filled in the previous loop
456 unsafe {
457 read_buf.set_init(initialized);
458 }
459
460 let mut cursor = read_buf.unfilled();
461 loop {
462 match r.read_buf(cursor.reborrow()) {
463 Ok(()) => break,
464 Err(e) if e.is_interrupted() => continue,
465 Err(e) => return Err(e),
466 }
467 }
468
469 let unfilled_but_initialized = cursor.init_ref().len();
470 let bytes_read = cursor.written();
471 let was_fully_initialized = read_buf.init_len() == buf_len;
472
473 if bytes_read == 0 {
474 return Ok(buf.len() - start_len);
475 }
476
477 // store how much was initialized but not filled
478 initialized = unfilled_but_initialized;
479
480 // SAFETY: BorrowedBuf's invariants mean this much memory is initialized.
481 unsafe {
482 let new_len = bytes_read + buf.len();
483 buf.set_len(new_len);
484 }
485
486 // Use heuristics to determine the max read size if no initial size hint was provided
487 if size_hint.is_none() {
488 // The reader is returning short reads but it doesn't call ensure_init().
489 // In that case we no longer need to restrict read sizes to avoid
490 // initialization costs.
491 if !was_fully_initialized {
492 max_read_size = usize::MAX;
493 }
494
495 // we have passed a larger buffer than previously and the
496 // reader still hasn't returned a short read
497 if buf_len >= max_read_size && bytes_read == buf_len {
498 max_read_size = max_read_size.saturating_mul(2);
499 }
500 }
501 }
502}
503
504pub(crate) fn default_read_to_string<R: Read + ?Sized>(
505 r: &mut R,
506 buf: &mut String,
507 size_hint: Option<usize>,
508) -> Result<usize> {
509 // Note that we do *not* call `r.read_to_end()` here. We are passing
510 // `&mut Vec<u8>` (the raw contents of `buf`) into the `read_to_end`
511 // method to fill it up. An arbitrary implementation could overwrite the
512 // entire contents of the vector, not just append to it (which is what
513 // we are expecting).
514 //
515 // To prevent extraneously checking the UTF-8-ness of the entire buffer
516 // we pass it to our hardcoded `default_read_to_end` implementation which
517 // we know is guaranteed to only read data into the end of the buffer.
518 unsafe { append_to_string(buf, |b| default_read_to_end(r, b, size_hint)) }
519}
520
521pub(crate) fn default_read_exact<R: Read + ?Sized>(this: &mut R, mut buf: &mut [u8]) -> Result<()> {
522 while !buf.is_empty() {
523 match this.read(buf) {
524 Ok(0) => break,
525 Ok(n) => {
526 buf = &mut buf[n..];
527 }
528 Err(ref e) if e.is_interrupted() => {}
529 Err(e) => return Err(e),
530 }
531 }
532 if !buf.is_empty() {
533 Err(Error::READ_EXACT_EOF)
534 } else {
535 Ok(())
536 }
537}
538
539pub(crate) fn default_read_buf<F>(read: F, mut cursor: BorrowedCursor<'_>) -> Result<()>
540where
541 F: FnOnce(&mut [u8]) -> Result<usize>,
542{
543 let n = read(cursor.ensure_init().init_mut())?;
544 cursor.advance(n);
545 Ok(())
546}
547
548pub(crate) fn default_read_buf_exact<R: Read + ?Sized>(
549 this: &mut R,
550 mut cursor: BorrowedCursor<'_>,
551) -> Result<()> {
552 while cursor.capacity() > 0 {
553 let prev_written = cursor.written();
554 match this.read_buf(cursor.reborrow()) {
555 Ok(()) => {}
556 Err(e) if e.is_interrupted() => continue,
557 Err(e) => return Err(e),
558 }
559
560 if cursor.written() == prev_written {
561 return Err(Error::READ_EXACT_EOF);
562 }
563 }
564
565 Ok(())
566}
567
568/// The `Read` trait allows for reading bytes from a source.
569///
570/// Implementors of the `Read` trait are called 'readers'.
571///
572/// Readers are defined by one required method, [`read()`]. Each call to [`read()`]
573/// will attempt to pull bytes from this source into a provided buffer. A
574/// number of other methods are implemented in terms of [`read()`], giving
575/// implementors a number of ways to read bytes while only needing to implement
576/// a single method.
577///
578/// Readers are intended to be composable with one another. Many implementors
579/// throughout [`std::io`] take and provide types which implement the `Read`
580/// trait.
581///
582/// Please note that each call to [`read()`] may involve a system call, and
583/// therefore, using something that implements [`BufRead`], such as
584/// [`BufReader`], will be more efficient.
585///
586/// Repeated calls to the reader use the same cursor, so for example
587/// calling `read_to_end` twice on a [`File`] will only return the file's
588/// contents once. It's recommended to first call `rewind()` in that case.
589///
590/// # Examples
591///
592/// [`File`]s implement `Read`:
593///
594/// ```no_run
595/// use std::io;
596/// use std::io::prelude::*;
597/// use std::fs::File;
598///
599/// fn main() -> io::Result<()> {
600/// let mut f = File::open("foo.txt")?;
601/// let mut buffer = [0; 10];
602///
603/// // read up to 10 bytes
604/// f.read(&mut buffer)?;
605///
606/// let mut buffer = Vec::new();
607/// // read the whole file
608/// f.read_to_end(&mut buffer)?;
609///
610/// // read into a String, so that you don't need to do the conversion.
611/// let mut buffer = String::new();
612/// f.read_to_string(&mut buffer)?;
613///
614/// // and more! See the other methods for more details.
615/// Ok(())
616/// }
617/// ```
618///
619/// Read from [`&str`] because [`&[u8]`][prim@slice] implements `Read`:
620///
621/// ```no_run
622/// # use std::io;
623/// use std::io::prelude::*;
624///
625/// fn main() -> io::Result<()> {
626/// let mut b = "This string will be read".as_bytes();
627/// let mut buffer = [0; 10];
628///
629/// // read up to 10 bytes
630/// b.read(&mut buffer)?;
631///
632/// // etc... it works exactly as a File does!
633/// Ok(())
634/// }
635/// ```
636///
637/// [`read()`]: Read::read
638/// [`&str`]: prim@str
639/// [`std::io`]: self
640/// [`File`]: crate::fs::File
641pub trait Read {
642 /// Pull some bytes from this source into the specified buffer, returning
643 /// how many bytes were read.
644 ///
645 /// This function does not provide any guarantees about whether it blocks
646 /// waiting for data, but if an object needs to block for a read and cannot,
647 /// it will typically signal this via an [`Err`] return value.
648 ///
649 /// If the return value of this method is [`Ok(n)`], then implementations must
650 /// guarantee that `0 <= n <= buf.len()`. A nonzero `n` value indicates
651 /// that the buffer `buf` has been filled in with `n` bytes of data from this
652 /// source. If `n` is `0`, then it can indicate one of two scenarios:
653 ///
654 /// 1. This reader has reached its "end of file" and will likely no longer
655 /// be able to produce bytes. Note that this does not mean that the
656 /// reader will *always* no longer be able to produce bytes. As an example,
657 /// on Linux, this method will call the `recv` syscall for a [`TcpStream`],
658 /// where returning zero indicates the connection was shut down correctly. While
659 /// for [`File`], it is possible to reach the end of file and get zero as result,
660 /// but if more data is appended to the file, future calls to `read` will return
661 /// more data.
662 /// 2. The buffer specified was 0 bytes in length.
663 ///
664 /// It is not an error if the returned value `n` is smaller than the buffer size,
665 /// even when the reader is not at the end of the stream yet.
666 /// This may happen for example because fewer bytes are actually available right now
667 /// (e. g. being close to end-of-file) or because read() was interrupted by a signal.
668 ///
669 /// As this trait is safe to implement, callers in unsafe code cannot rely on
670 /// `n <= buf.len()` for safety.
671 /// Extra care needs to be taken when `unsafe` functions are used to access the read bytes.
672 /// Callers have to ensure that no unchecked out-of-bounds accesses are possible even if
673 /// `n > buf.len()`.
674 ///
675 /// *Implementations* of this method can make no assumptions about the contents of `buf` when
676 /// this function is called. It is recommended that implementations only write data to `buf`
677 /// instead of reading its contents.
678 ///
679 /// Correspondingly, however, *callers* of this method in unsafe code must not assume
680 /// any guarantees about how the implementation uses `buf`. The trait is safe to implement,
681 /// so it is possible that the code that's supposed to write to the buffer might also read
682 /// from it. It is your responsibility to make sure that `buf` is initialized
683 /// before calling `read`. Calling `read` with an uninitialized `buf` (of the kind one
684 /// obtains via [`MaybeUninit<T>`]) is not safe, and can lead to undefined behavior.
685 ///
686 /// [`MaybeUninit<T>`]: crate::mem::MaybeUninit
687 ///
688 /// # Errors
689 ///
690 /// If this function encounters any form of I/O or other error, an error
691 /// variant will be returned. If an error is returned then it must be
692 /// guaranteed that no bytes were read.
693 ///
694 /// An error of the [`ErrorKind::Interrupted`] kind is non-fatal and the read
695 /// operation should be retried if there is nothing else to do.
696 ///
697 /// # Examples
698 ///
699 /// [`File`]s implement `Read`:
700 ///
701 /// [`Ok(n)`]: Ok
702 /// [`File`]: crate::fs::File
703 /// [`TcpStream`]: crate::net::TcpStream
704 ///
705 /// ```no_run
706 /// use std::io;
707 /// use std::io::prelude::*;
708 /// use std::fs::File;
709 ///
710 /// fn main() -> io::Result<()> {
711 /// let mut f = File::open("foo.txt")?;
712 /// let mut buffer = [0; 10];
713 ///
714 /// // read up to 10 bytes
715 /// let n = f.read(&mut buffer[..])?;
716 ///
717 /// println!("The bytes: {:?}", &buffer[..n]);
718 /// Ok(())
719 /// }
720 /// ```
721 fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
722
723 /// Read all bytes until EOF in this source, placing them into `buf`.
724 ///
725 /// All bytes read from this source will be appended to the specified buffer
726 /// `buf`. This function will continuously call [`read()`] to append more data to
727 /// `buf` until [`read()`] returns either [`Ok(0)`] or an error of
728 /// non-[`ErrorKind::Interrupted`] kind.
729 ///
730 /// If successful, this function will return the total number of bytes read.
731 ///
732 /// # Errors
733 ///
734 /// If this function encounters an error of the kind
735 /// [`ErrorKind::Interrupted`] then the error is ignored and the operation
736 /// will continue.
737 ///
738 /// If any other read error is encountered then this function immediately
739 /// returns. Any bytes which have already been read will be appended to
740 /// `buf`.
741 ///
742 /// # Examples
743 ///
744 /// [`File`]s implement `Read`:
745 ///
746 /// [`read()`]: Read::read
747 /// [`Ok(0)`]: Ok
748 /// [`File`]: crate::fs::File
749 ///
750 /// ```no_run
751 /// use std::io;
752 /// use std::io::prelude::*;
753 /// use std::fs::File;
754 ///
755 /// fn main() -> io::Result<()> {
756 /// let mut f = File::open("foo.txt")?;
757 /// let mut buffer = Vec::new();
758 ///
759 /// // read the whole file
760 /// f.read_to_end(&mut buffer)?;
761 /// Ok(())
762 /// }
763 /// ```
764 ///
765 /// (See also the [`std::fs::read`] convenience function for reading from a
766 /// file.)
767 ///
768 /// [`std::fs::read`]: crate::fs::read
769 ///
770 /// ## Implementing `read_to_end`
771 ///
772 /// When implementing the `io::Read` trait, it is recommended to allocate
773 /// memory using [`Vec::try_reserve`]. However, this behavior is not guaranteed
774 /// by all implementations, and `read_to_end` may not handle out-of-memory
775 /// situations gracefully.
776 ///
777 /// ```no_run
778 /// # use std::io::{self, BufRead};
779 /// # struct Example { example_datasource: io::Empty } impl Example {
780 /// # fn get_some_data_for_the_example(&self) -> &'static [u8] { &[] }
781 /// fn read_to_end(&mut self, dest_vec: &mut Vec<u8>) -> io::Result<usize> {
782 /// let initial_vec_len = dest_vec.len();
783 /// loop {
784 /// let src_buf = self.example_datasource.fill_buf()?;
785 /// if src_buf.is_empty() {
786 /// break;
787 /// }
788 /// dest_vec.try_reserve(src_buf.len())?;
789 /// dest_vec.extend_from_slice(src_buf);
790 ///
791 /// // Any irreversible side effects should happen after `try_reserve` succeeds,
792 /// // to avoid losing data on allocation error.
793 /// let read = src_buf.len();
794 /// self.example_datasource.consume(read);
795 /// }
796 /// Ok(dest_vec.len() - initial_vec_len)
797 /// }
798 /// # }
799 /// ```
800 ///
801 /// [`Vec::try_reserve`]: crate::vec::Vec::try_reserve
802 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
803 default_read_to_end(self, buf, None)
804 }
805
806 /// Read all bytes until EOF in this source, appending them to `buf`.
807 ///
808 /// If successful, this function returns the number of bytes which were read
809 /// and appended to `buf`.
810 ///
811 /// # Errors
812 ///
813 /// If the data in this stream is *not* valid UTF-8 then an error is
814 /// returned and `buf` is unchanged.
815 ///
816 /// See [`read_to_end`] for other error semantics.
817 ///
818 /// [`read_to_end`]: Read::read_to_end
819 ///
820 /// # Examples
821 ///
822 /// [`File`]s implement `Read`:
823 ///
824 /// [`File`]: crate::fs::File
825 ///
826 /// ```no_run
827 /// use std::io;
828 /// use std::io::prelude::*;
829 /// use std::fs::File;
830 ///
831 /// fn main() -> io::Result<()> {
832 /// let mut f = File::open("foo.txt")?;
833 /// let mut buffer = String::new();
834 ///
835 /// f.read_to_string(&mut buffer)?;
836 /// Ok(())
837 /// }
838 /// ```
839 ///
840 /// (See also the [`std::fs::read_to_string`] convenience function for
841 /// reading from a file.)
842 ///
843 /// [`std::fs::read_to_string`]: crate::fs::read_to_string
844 fn read_to_string(&mut self, buf: &mut String) -> Result<usize> {
845 default_read_to_string(self, buf, None)
846 }
847
848 /// Read the exact number of bytes required to fill `buf`.
849 ///
850 /// This function reads as many bytes as necessary to completely fill the
851 /// specified buffer `buf`.
852 ///
853 /// *Implementations* of this method can make no assumptions about the contents of `buf` when
854 /// this function is called. It is recommended that implementations only write data to `buf`
855 /// instead of reading its contents. The documentation on [`read`] has a more detailed
856 /// explanation of this subject.
857 ///
858 /// # Errors
859 ///
860 /// If this function encounters an error of the kind
861 /// [`ErrorKind::Interrupted`] then the error is ignored and the operation
862 /// will continue.
863 ///
864 /// If this function encounters an "end of file" before completely filling
865 /// the buffer, it returns an error of the kind [`ErrorKind::UnexpectedEof`].
866 /// The contents of `buf` are unspecified in this case.
867 ///
868 /// If any other read error is encountered then this function immediately
869 /// returns. The contents of `buf` are unspecified in this case.
870 ///
871 /// If this function returns an error, it is unspecified how many bytes it
872 /// has read, but it will never read more than would be necessary to
873 /// completely fill the buffer.
874 ///
875 /// # Examples
876 ///
877 /// [`File`]s implement `Read`:
878 ///
879 /// [`read`]: Read::read
880 /// [`File`]: crate::fs::File
881 ///
882 /// ```no_run
883 /// use std::io;
884 /// use std::io::prelude::*;
885 /// use std::fs::File;
886 ///
887 /// fn main() -> io::Result<()> {
888 /// let mut f = File::open("foo.txt")?;
889 /// let mut buffer = [0; 10];
890 ///
891 /// // read exactly 10 bytes
892 /// f.read_exact(&mut buffer)?;
893 /// Ok(())
894 /// }
895 /// ```
896 fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
897 default_read_exact(self, buf)
898 }
899
900 /// Pull some bytes from this source into the specified buffer.
901 ///
902 /// This is equivalent to the [`read`](Read::read) method, except that it is passed a [`BorrowedCursor`] rather than `[u8]` to allow use
903 /// with uninitialized buffers. The new data will be appended to any existing contents of `buf`.
904 ///
905 /// The default implementation delegates to `read`.
906 fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<()> {
907 default_read_buf(|b| self.read(b), buf)
908 }
909
910 /// Read the exact number of bytes required to fill `cursor`.
911 ///
912 /// This is similar to the [`read_exact`](Read::read_exact) method, except
913 /// that it is passed a [`BorrowedCursor`] rather than `[u8]` to allow use
914 /// with uninitialized buffers.
915 ///
916 /// # Errors
917 ///
918 /// If this function encounters an error of the kind [`ErrorKind::Interrupted`]
919 /// then the error is ignored and the operation will continue.
920 ///
921 /// If this function encounters an "end of file" before completely filling
922 /// the buffer, it returns an error of the kind [`ErrorKind::UnexpectedEof`].
923 ///
924 /// If any other read error is encountered then this function immediately
925 /// returns.
926 ///
927 /// If this function returns an error, all bytes read will be appended to `cursor`.
928 fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<()> {
929 default_read_buf_exact(self, cursor)
930 }
931
932 /// Creates a "by reference" adaptor for this instance of `Read`.
933 ///
934 /// The returned adapter also implements `Read` and will simply borrow this
935 /// current reader.
936 ///
937 /// # Examples
938 ///
939 /// [`File`]s implement `Read`:
940 ///
941 /// [`File`]: crate::fs::File
942 ///
943 /// ```no_run
944 /// use std::io;
945 /// use std::io::Read;
946 /// use std::fs::File;
947 ///
948 /// fn main() -> io::Result<()> {
949 /// let mut f = File::open("foo.txt")?;
950 /// let mut buffer = Vec::new();
951 /// let mut other_buffer = Vec::new();
952 ///
953 /// {
954 /// let reference = f.by_ref();
955 ///
956 /// // read at most 5 bytes
957 /// reference.take(5).read_to_end(&mut buffer)?;
958 ///
959 /// } // drop our &mut reference so we can use f again
960 ///
961 /// // original file still usable, read the rest
962 /// f.read_to_end(&mut other_buffer)?;
963 /// Ok(())
964 /// }
965 /// ```
966 fn by_ref(&mut self) -> &mut Self
967 where
968 Self: Sized,
969 {
970 self
971 }
972
973 /// Transforms this `Read` instance to an [`Iterator`] over its bytes.
974 ///
975 /// The returned type implements [`Iterator`] where the [`Item`] is
976 /// <code>[Result]<[u8], [io::Error]></code>.
977 /// The yielded item is [`Ok`] if a byte was successfully read and [`Err`]
978 /// otherwise. EOF is mapped to returning [`None`] from this iterator.
979 ///
980 /// The default implementation calls `read` for each byte,
981 /// which can be very inefficient for data that's not in memory,
982 /// such as [`File`]. Consider using a [`BufReader`] in such cases.
983 ///
984 /// # Examples
985 ///
986 /// [`File`]s implement `Read`:
987 ///
988 /// [`Item`]: Iterator::Item
989 /// [`File`]: crate::fs::File "fs::File"
990 /// [Result]: crate::result::Result "Result"
991 /// [io::Error]: self::Error "io::Error"
992 ///
993 /// ```no_run
994 /// use std::io;
995 /// use std::io::prelude::*;
996 /// use std::io::BufReader;
997 /// use std::fs::File;
998 ///
999 /// fn main() -> io::Result<()> {
1000 /// let f = BufReader::new(File::open("foo.txt")?);
1001 ///
1002 /// for byte in f.bytes() {
1003 /// println!("{}", byte.unwrap());
1004 /// }
1005 /// Ok(())
1006 /// }
1007 /// ```
1008 fn bytes(self) -> Bytes<Self>
1009 where
1010 Self: Sized,
1011 {
1012 Bytes { inner: self }
1013 }
1014
1015 /// Creates an adapter which will chain this stream with another.
1016 ///
1017 /// The returned `Read` instance will first read all bytes from this object
1018 /// until EOF is encountered. Afterwards the output is equivalent to the
1019 /// output of `next`.
1020 ///
1021 /// # Examples
1022 ///
1023 /// [`File`]s implement `Read`:
1024 ///
1025 /// [`File`]: crate::fs::File
1026 ///
1027 /// ```no_run
1028 /// use std::io;
1029 /// use std::io::prelude::*;
1030 /// use std::fs::File;
1031 ///
1032 /// fn main() -> io::Result<()> {
1033 /// let f1 = File::open("foo.txt")?;
1034 /// let f2 = File::open("bar.txt")?;
1035 ///
1036 /// let mut handle = f1.chain(f2);
1037 /// let mut buffer = String::new();
1038 ///
1039 /// // read the value into a String. We could use any Read method here,
1040 /// // this is just one example.
1041 /// handle.read_to_string(&mut buffer)?;
1042 /// Ok(())
1043 /// }
1044 /// ```
1045 fn chain<R: Read>(self, next: R) -> Chain<Self, R>
1046 where
1047 Self: Sized,
1048 {
1049 Chain {
1050 first: self,
1051 second: next,
1052 done_first: false,
1053 }
1054 }
1055
1056 /// Creates an adapter which will read at most `limit` bytes from it.
1057 ///
1058 /// This function returns a new instance of `Read` which will read at most
1059 /// `limit` bytes, after which it will always return EOF ([`Ok(0)`]). Any
1060 /// read errors will not count towards the number of bytes read and future
1061 /// calls to [`read()`] may succeed.
1062 ///
1063 /// # Examples
1064 ///
1065 /// [`File`]s implement `Read`:
1066 ///
1067 /// [`File`]: crate::fs::File
1068 /// [`Ok(0)`]: Ok
1069 /// [`read()`]: Read::read
1070 ///
1071 /// ```no_run
1072 /// use std::io;
1073 /// use std::io::prelude::*;
1074 /// use std::fs::File;
1075 ///
1076 /// fn main() -> io::Result<()> {
1077 /// let f = File::open("foo.txt")?;
1078 /// let mut buffer = [0; 5];
1079 ///
1080 /// // read at most five bytes
1081 /// let mut handle = f.take(5);
1082 ///
1083 /// handle.read(&mut buffer)?;
1084 /// Ok(())
1085 /// }
1086 /// ```
1087 fn take(self, limit: u64) -> Take<Self>
1088 where
1089 Self: Sized,
1090 {
1091 Take { inner: self, limit }
1092 }
1093}
1094
1095/// Read all bytes from a [reader][Read] into a new [`String`].
1096///
1097/// This is a convenience function for [`Read::read_to_string`]. Using this
1098/// function avoids having to create a variable first and provides more type
1099/// safety since you can only get the buffer out if there were no errors. (If you
1100/// use [`Read::read_to_string`] you have to remember to check whether the read
1101/// succeeded because otherwise your buffer will be empty or only partially full.)
1102///
1103/// # Performance
1104///
1105/// The downside of this function's increased ease of use and type safety is
1106/// that it gives you less control over performance. For example, you can't
1107/// pre-allocate memory like you can using [`String::with_capacity`] and
1108/// [`Read::read_to_string`]. Also, you can't re-use the buffer if an error
1109/// occurs while reading.
1110///
1111/// In many cases, this function's performance will be adequate and the ease of use
1112/// and type safety tradeoffs will be worth it. However, there are cases where you
1113/// need more control over performance, and in those cases you should definitely use
1114/// [`Read::read_to_string`] directly.
1115///
1116/// Note that in some special cases, such as when reading files, this function will
1117/// pre-allocate memory based on the size of the input it is reading. In those
1118/// cases, the performance should be as good as if you had used
1119/// [`Read::read_to_string`] with a manually pre-allocated buffer.
1120///
1121/// # Errors
1122///
1123/// This function forces you to handle errors because the output (the `String`)
1124/// is wrapped in a [`Result`]. See [`Read::read_to_string`] for the errors
1125/// that can occur. If any error occurs, you will get an [`Err`], so you
1126/// don't have to worry about your buffer being empty or partially full.
1127///
1128/// # Examples
1129///
1130/// ```no_run
1131/// # use std::io;
1132/// fn main() -> io::Result<()> {
1133/// let stdin = io::read_to_string(io::stdin())?;
1134/// println!("Stdin was:");
1135/// println!("{stdin}");
1136/// Ok(())
1137/// }
1138/// ```
1139pub fn read_to_string<R: Read>(mut reader: R) -> Result<String> {
1140 let mut buf = String::new();
1141 reader.read_to_string(&mut buf)?;
1142 Ok(buf)
1143}
1144
1145/// A trait for objects which are byte-oriented sinks.
1146///
1147/// Implementors of the `Write` trait are sometimes called 'writers'.
1148///
1149/// Writers are defined by two required methods, [`write`] and [`flush`]:
1150///
1151/// * The [`write`] method will attempt to write some data into the object,
1152/// returning how many bytes were successfully written.
1153///
1154/// * The [`flush`] method is useful for adapters and explicit buffers
1155/// themselves for ensuring that all buffered data has been pushed out to the
1156/// 'true sink'.
1157///
1158/// Writers are intended to be composable with one another. Many implementors
1159/// throughout [`std::io`] take and provide types which implement the `Write`
1160/// trait.
1161///
1162/// [`write`]: Write::write
1163/// [`flush`]: Write::flush
1164/// [`std::io`]: self
1165///
1166/// # Examples
1167///
1168/// ```no_run
1169/// use std::io::prelude::*;
1170/// use std::fs::File;
1171///
1172/// fn main() -> std::io::Result<()> {
1173/// let data = b"some bytes";
1174///
1175/// let mut pos = 0;
1176/// let mut buffer = File::create("foo.txt")?;
1177///
1178/// while pos < data.len() {
1179/// let bytes_written = buffer.write(&data[pos..])?;
1180/// pos += bytes_written;
1181/// }
1182/// Ok(())
1183/// }
1184/// ```
1185///
1186/// The trait also provides convenience methods like [`write_all`], which calls
1187/// `write` in a loop until its entire input has been written.
1188///
1189/// [`write_all`]: Write::write_all
1190pub trait Write {
1191 /// Write a buffer into this writer, returning how many bytes were written.
1192 ///
1193 /// This function will attempt to write the entire contents of `buf`, but
1194 /// the entire write might not succeed, or the write may also generate an
1195 /// error. Typically, a call to `write` represents one attempt to write to
1196 /// any wrapped object.
1197 ///
1198 /// Calls to `write` are not guaranteed to block waiting for data to be
1199 /// written, and a write which would otherwise block can be indicated through
1200 /// an [`Err`] variant.
1201 ///
1202 /// If this method consumed `n > 0` bytes of `buf` it must return [`Ok(n)`].
1203 /// If the return value is `Ok(n)` then `n` must satisfy `n <= buf.len()`.
1204 /// A return value of `Ok(0)` typically means that the underlying object is
1205 /// no longer able to accept bytes and will likely not be able to in the
1206 /// future as well, or that the buffer provided is empty.
1207 ///
1208 /// # Errors
1209 ///
1210 /// Each call to `write` may generate an I/O error indicating that the
1211 /// operation could not be completed. If an error is returned then no bytes
1212 /// in the buffer were written to this writer.
1213 ///
1214 /// It is **not** considered an error if the entire buffer could not be
1215 /// written to this writer.
1216 ///
1217 /// An error of the [`ErrorKind::Interrupted`] kind is non-fatal and the
1218 /// write operation should be retried if there is nothing else to do.
1219 ///
1220 /// # Examples
1221 ///
1222 /// ```no_run
1223 /// use std::io::prelude::*;
1224 /// use std::fs::File;
1225 ///
1226 /// fn main() -> std::io::Result<()> {
1227 /// let mut buffer = File::create("foo.txt")?;
1228 ///
1229 /// // Writes some prefix of the byte string, not necessarily all of it.
1230 /// buffer.write(b"some bytes")?;
1231 /// Ok(())
1232 /// }
1233 /// ```
1234 ///
1235 /// [`Ok(n)`]: Ok
1236 fn write(&mut self, buf: &[u8]) -> Result<usize>;
1237 fn is_write_vectored(&self) -> bool {
1238 false
1239 }
1240
1241 /// Flush this output stream, ensuring that all intermediately buffered
1242 /// contents reach their destination.
1243 ///
1244 /// # Errors
1245 ///
1246 /// It is considered an error if not all bytes could be written due to
1247 /// I/O errors or EOF being reached.
1248 ///
1249 /// # Examples
1250 ///
1251 /// ```no_run
1252 /// use std::io::prelude::*;
1253 /// use std::io::BufWriter;
1254 /// use std::fs::File;
1255 ///
1256 /// fn main() -> std::io::Result<()> {
1257 /// let mut buffer = BufWriter::new(File::create("foo.txt")?);
1258 ///
1259 /// buffer.write_all(b"some bytes")?;
1260 /// buffer.flush()?;
1261 /// Ok(())
1262 /// }
1263 /// ```
1264 fn flush(&mut self) -> Result<()>;
1265
1266 /// Attempts to write an entire buffer into this writer.
1267 ///
1268 /// This method will continuously call [`write`] until there is no more data
1269 /// to be written or an error of non-[`ErrorKind::Interrupted`] kind is
1270 /// returned. This method will not return until the entire buffer has been
1271 /// successfully written or such an error occurs. The first error that is
1272 /// not of [`ErrorKind::Interrupted`] kind generated from this method will be
1273 /// returned.
1274 ///
1275 /// If the buffer contains no data, this will never call [`write`].
1276 ///
1277 /// # Errors
1278 ///
1279 /// This function will return the first error of
1280 /// non-[`ErrorKind::Interrupted`] kind that [`write`] returns.
1281 ///
1282 /// [`write`]: Write::write
1283 ///
1284 /// # Examples
1285 ///
1286 /// ```no_run
1287 /// use std::io::prelude::*;
1288 /// use std::fs::File;
1289 ///
1290 /// fn main() -> std::io::Result<()> {
1291 /// let mut buffer = File::create("foo.txt")?;
1292 ///
1293 /// buffer.write_all(b"some bytes")?;
1294 /// Ok(())
1295 /// }
1296 /// ```
1297 fn write_all(&mut self, mut buf: &[u8]) -> Result<()> {
1298 while !buf.is_empty() {
1299 match self.write(buf) {
1300 Ok(0) => {
1301 return Err(Error::WRITE_ALL_EOF);
1302 }
1303 Ok(n) => buf = &buf[n..],
1304 Err(ref e) if e.is_interrupted() => {}
1305 Err(e) => return Err(e),
1306 }
1307 }
1308 Ok(())
1309 }
1310
1311 /// Writes a formatted string into this writer, returning any error
1312 /// encountered.
1313 ///
1314 /// This method is primarily used to interface with the
1315 /// [`format_args!()`] macro, and it is rare that this should
1316 /// explicitly be called. The [`write!()`] macro should be favored to
1317 /// invoke this method instead.
1318 ///
1319 /// This function internally uses the [`write_all`] method on
1320 /// this trait and hence will continuously write data so long as no errors
1321 /// are received. This also means that partial writes are not indicated in
1322 /// this signature.
1323 ///
1324 /// [`write_all`]: Write::write_all
1325 ///
1326 /// # Errors
1327 ///
1328 /// This function will return any I/O error reported while formatting.
1329 ///
1330 /// # Examples
1331 ///
1332 /// ```no_run
1333 /// use std::io::prelude::*;
1334 /// use std::fs::File;
1335 ///
1336 /// fn main() -> std::io::Result<()> {
1337 /// let mut buffer = File::create("foo.txt")?;
1338 ///
1339 /// // this call
1340 /// write!(buffer, "{:.*}", 2, 1.234567)?;
1341 /// // turns into this:
1342 /// buffer.write_fmt(format_args!("{:.*}", 2, 1.234567))?;
1343 /// Ok(())
1344 /// }
1345 /// ```
1346 fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> Result<()> {
1347 // Create a shim which translates a Write to a fmt::Write and saves
1348 // off I/O errors. instead of discarding them
1349 struct Adapter<'a, T: ?Sized + 'a> {
1350 inner: &'a mut T,
1351 error: Result<()>,
1352 }
1353
1354 impl<T: Write + ?Sized> fmt::Write for Adapter<'_, T> {
1355 fn write_str(&mut self, s: &str) -> fmt::Result {
1356 match self.inner.write_all(s.as_bytes()) {
1357 Ok(()) => Ok(()),
1358 Err(e) => {
1359 self.error = Err(e);
1360 Err(fmt::Error)
1361 }
1362 }
1363 }
1364 }
1365
1366 let mut output = Adapter {
1367 inner: self,
1368 error: Ok(()),
1369 };
1370 match fmt::write(&mut output, fmt) {
1371 Ok(()) => Ok(()),
1372 Err(..) => {
1373 // check if the error came from the underlying `Write` or not
1374 if output.error.is_err() {
1375 output.error
1376 } else {
1377 // This shouldn't happen: the underlying stream did not error, but somehow
1378 // the formatter still errored?
1379 panic!(
1380 "a formatting trait implementation returned an error when the underlying stream did not"
1381 );
1382 }
1383 }
1384 }
1385 }
1386
1387 /// Creates a "by reference" adapter for this instance of `Write`.
1388 ///
1389 /// The returned adapter also implements `Write` and will simply borrow this
1390 /// current writer.
1391 ///
1392 /// # Examples
1393 ///
1394 /// ```no_run
1395 /// use std::io::Write;
1396 /// use std::fs::File;
1397 ///
1398 /// fn main() -> std::io::Result<()> {
1399 /// let mut buffer = File::create("foo.txt")?;
1400 ///
1401 /// let reference = buffer.by_ref();
1402 ///
1403 /// // we can use reference just like our original buffer
1404 /// reference.write_all(b"some bytes")?;
1405 /// Ok(())
1406 /// }
1407 /// ```
1408 fn by_ref(&mut self) -> &mut Self
1409 where
1410 Self: Sized,
1411 {
1412 self
1413 }
1414}
1415
1416/// The `Seek` trait provides a cursor which can be moved within a stream of
1417/// bytes.
1418///
1419/// The stream typically has a fixed size, allowing seeking relative to either
1420/// end or the current offset.
1421///
1422/// # Examples
1423///
1424/// [`File`]s implement `Seek`:
1425///
1426/// [`File`]: crate::fs::File
1427///
1428/// ```no_run
1429/// use std::io;
1430/// use std::io::prelude::*;
1431/// use std::fs::File;
1432/// use std::io::SeekFrom;
1433///
1434/// fn main() -> io::Result<()> {
1435/// let mut f = File::open("foo.txt")?;
1436///
1437/// // move the cursor 42 bytes from the start of the file
1438/// f.seek(SeekFrom::Start(42))?;
1439/// Ok(())
1440/// }
1441/// ```
1442pub trait Seek {
1443 /// Seek to an offset, in bytes, in a stream.
1444 ///
1445 /// A seek beyond the end of a stream is allowed, but behavior is defined
1446 /// by the implementation.
1447 ///
1448 /// If the seek operation completed successfully,
1449 /// this method returns the new position from the start of the stream.
1450 /// That position can be used later with [`SeekFrom::Start`].
1451 ///
1452 /// # Errors
1453 ///
1454 /// Seeking can fail, for example because it might involve flushing a buffer.
1455 ///
1456 /// Seeking to a negative offset is considered an error.
1457 fn seek(&mut self, pos: SeekFrom) -> Result<u64>;
1458
1459 /// Rewind to the beginning of a stream.
1460 ///
1461 /// This is a convenience method, equivalent to `seek(SeekFrom::Start(0))`.
1462 ///
1463 /// # Errors
1464 ///
1465 /// Rewinding can fail, for example because it might involve flushing a buffer.
1466 ///
1467 /// # Example
1468 ///
1469 /// ```no_run
1470 /// use std::io::{Read, Seek, Write};
1471 /// use std::fs::OpenOptions;
1472 ///
1473 /// let mut f = OpenOptions::new()
1474 /// .write(true)
1475 /// .read(true)
1476 /// .create(true)
1477 /// .open("foo.txt").unwrap();
1478 ///
1479 /// let hello = "Hello!\n";
1480 /// write!(f, "{hello}").unwrap();
1481 /// f.rewind().unwrap();
1482 ///
1483 /// let mut buf = String::new();
1484 /// f.read_to_string(&mut buf).unwrap();
1485 /// assert_eq!(&buf, hello);
1486 /// ```
1487 fn rewind(&mut self) -> Result<()> {
1488 self.seek(SeekFrom::Start(0))?;
1489 Ok(())
1490 }
1491
1492 /// Returns the length of this stream (in bytes).
1493 ///
1494 /// This method is implemented using up to three seek operations. If this
1495 /// method returns successfully, the seek position is unchanged (i.e. the
1496 /// position before calling this method is the same as afterwards).
1497 /// However, if this method returns an error, the seek position is
1498 /// unspecified.
1499 ///
1500 /// If you need to obtain the length of *many* streams and you don't care
1501 /// about the seek position afterwards, you can reduce the number of seek
1502 /// operations by simply calling `seek(SeekFrom::End(0))` and using its
1503 /// return value (it is also the stream length).
1504 ///
1505 /// Note that length of a stream can change over time (for example, when
1506 /// data is appended to a file). So calling this method multiple times does
1507 /// not necessarily return the same length each time.
1508 ///
1509 /// # Example
1510 ///
1511 /// ```no_run
1512 /// #![feature(seek_stream_len)]
1513 /// use std::{
1514 /// io::{self, Seek},
1515 /// fs::File,
1516 /// };
1517 ///
1518 /// fn main() -> io::Result<()> {
1519 /// let mut f = File::open("foo.txt")?;
1520 ///
1521 /// let len = f.stream_len()?;
1522 /// println!("The file is currently {len} bytes long");
1523 /// Ok(())
1524 /// }
1525 /// ```
1526 fn stream_len(&mut self) -> Result<u64> {
1527 let old_pos = self.stream_position()?;
1528 let len = self.seek(SeekFrom::End(0))?;
1529
1530 // Avoid seeking a third time when we were already at the end of the
1531 // stream. The branch is usually way cheaper than a seek operation.
1532 if old_pos != len {
1533 self.seek(SeekFrom::Start(old_pos))?;
1534 }
1535
1536 Ok(len)
1537 }
1538
1539 /// Returns the current seek position from the start of the stream.
1540 ///
1541 /// This is equivalent to `self.seek(SeekFrom::Current(0))`.
1542 ///
1543 /// # Example
1544 ///
1545 /// ```no_run
1546 /// use std::{
1547 /// io::{self, BufRead, BufReader, Seek},
1548 /// fs::File,
1549 /// };
1550 ///
1551 /// fn main() -> io::Result<()> {
1552 /// let mut f = BufReader::new(File::open("foo.txt")?);
1553 ///
1554 /// let before = f.stream_position()?;
1555 /// f.read_line(&mut String::new())?;
1556 /// let after = f.stream_position()?;
1557 ///
1558 /// println!("The first line was {} bytes long", after - before);
1559 /// Ok(())
1560 /// }
1561 /// ```
1562 fn stream_position(&mut self) -> Result<u64> {
1563 self.seek(SeekFrom::Current(0))
1564 }
1565
1566 /// Seeks relative to the current position.
1567 ///
1568 /// This is equivalent to `self.seek(SeekFrom::Current(offset))` but
1569 /// doesn't return the new position which can allow some implementations
1570 /// such as [`BufReader`] to perform more efficient seeks.
1571 ///
1572 /// # Example
1573 ///
1574 /// ```no_run
1575 /// use std::{
1576 /// io::{self, Seek},
1577 /// fs::File,
1578 /// };
1579 ///
1580 /// fn main() -> io::Result<()> {
1581 /// let mut f = File::open("foo.txt")?;
1582 /// f.seek_relative(10)?;
1583 /// assert_eq!(f.stream_position()?, 10);
1584 /// Ok(())
1585 /// }
1586 /// ```
1587 ///
1588 /// [`BufReader`]: crate::io::BufReader
1589 fn seek_relative(&mut self, offset: i64) -> Result<()> {
1590 self.seek(SeekFrom::Current(offset))?;
1591 Ok(())
1592 }
1593}
1594
1595/// Enumeration of possible methods to seek within an I/O object.
1596///
1597/// It is used by the [`Seek`] trait.
1598#[derive(Copy, PartialEq, Eq, Clone, Debug)]
1599pub enum SeekFrom {
1600 /// Sets the offset to the provided number of bytes.
1601 Start(u64),
1602
1603 /// Sets the offset to the size of this object plus the specified number of
1604 /// bytes.
1605 ///
1606 /// It is possible to seek beyond the end of an object, but it's an error to
1607 /// seek before byte 0.
1608 End(i64),
1609
1610 /// Sets the offset to the current position plus the specified number of
1611 /// bytes.
1612 ///
1613 /// It is possible to seek beyond the end of an object, but it's an error to
1614 /// seek before byte 0.
1615 Current(i64),
1616}
1617
1618fn read_until<R: BufRead + ?Sized>(r: &mut R, delim: u8, buf: &mut Vec<u8>) -> Result<usize> {
1619 let mut read = 0;
1620 loop {
1621 let (done, used) = {
1622 let available = match r.fill_buf() {
1623 Ok(n) => n,
1624 Err(ref e) if e.is_interrupted() => continue,
1625 Err(e) => return Err(e),
1626 };
1627 match memchr::memchr(delim, available) {
1628 Some(i) => {
1629 buf.extend_from_slice(&available[..=i]);
1630 (true, i + 1)
1631 }
1632 None => {
1633 buf.extend_from_slice(available);
1634 (false, available.len())
1635 }
1636 }
1637 };
1638 r.consume(used);
1639 read += used;
1640 if done || used == 0 {
1641 return Ok(read);
1642 }
1643 }
1644}
1645
1646fn skip_until<R: BufRead + ?Sized>(r: &mut R, delim: u8) -> Result<usize> {
1647 let mut read = 0;
1648 loop {
1649 let (done, used) = {
1650 let available = match r.fill_buf() {
1651 Ok(n) => n,
1652 Err(ref e) if e.is_interrupted() => continue,
1653 Err(e) => return Err(e),
1654 };
1655 match memchr::memchr(delim, available) {
1656 Some(i) => (true, i + 1),
1657 None => (false, available.len()),
1658 }
1659 };
1660 r.consume(used);
1661 read += used;
1662 if done || used == 0 {
1663 return Ok(read);
1664 }
1665 }
1666}
1667
1668/// A `BufRead` is a type of `Read`er which has an internal buffer, allowing it
1669/// to perform extra ways of reading.
1670///
1671/// For example, reading line-by-line is inefficient without using a buffer, so
1672/// if you want to read by line, you'll need `BufRead`, which includes a
1673/// [`read_line`] method as well as a [`lines`] iterator.
1674///
1675/// # Examples
1676///
1677/// A locked standard input implements `BufRead`:
1678///
1679/// ```no_run
1680/// use std::io;
1681/// use std::io::prelude::*;
1682///
1683/// let stdin = io::stdin();
1684/// for line in stdin.lock().lines() {
1685/// println!("{}", line.unwrap());
1686/// }
1687/// ```
1688///
1689/// If you have something that implements [`Read`], you can use the [`BufReader`
1690/// type][`BufReader`] to turn it into a `BufRead`.
1691///
1692/// For example, [`File`] implements [`Read`], but not `BufRead`.
1693/// [`BufReader`] to the rescue!
1694///
1695/// [`File`]: crate::fs::File
1696/// [`read_line`]: BufRead::read_line
1697/// [`lines`]: BufRead::lines
1698///
1699/// ```no_run
1700/// use std::io::{self, BufReader};
1701/// use std::io::prelude::*;
1702/// use std::fs::File;
1703///
1704/// fn main() -> io::Result<()> {
1705/// let f = File::open("foo.txt")?;
1706/// let f = BufReader::new(f);
1707///
1708/// for line in f.lines() {
1709/// println!("{}", line.unwrap());
1710/// }
1711///
1712/// Ok(())
1713/// }
1714/// ```
1715pub trait BufRead: Read {
1716 /// Returns the contents of the internal buffer, filling it with more data
1717 /// from the inner reader if it is empty.
1718 ///
1719 /// This function is a lower-level call. It needs to be paired with the
1720 /// [`consume`] method to function properly. When calling this
1721 /// method, none of the contents will be "read" in the sense that later
1722 /// calling `read` may return the same contents. As such, [`consume`] must
1723 /// be called with the number of bytes that are consumed from this buffer to
1724 /// ensure that the bytes are never returned twice.
1725 ///
1726 /// [`consume`]: BufRead::consume
1727 ///
1728 /// An empty buffer returned indicates that the stream has reached EOF.
1729 ///
1730 /// # Errors
1731 ///
1732 /// This function will return an I/O error if the underlying reader was
1733 /// read, but returned an error.
1734 ///
1735 /// # Examples
1736 ///
1737 /// A locked standard input implements `BufRead`:
1738 ///
1739 /// ```no_run
1740 /// use std::io;
1741 /// use std::io::prelude::*;
1742 ///
1743 /// let stdin = io::stdin();
1744 /// let mut stdin = stdin.lock();
1745 ///
1746 /// let buffer = stdin.fill_buf().unwrap();
1747 ///
1748 /// // work with buffer
1749 /// println!("{buffer:?}");
1750 ///
1751 /// // ensure the bytes we worked with aren't returned again later
1752 /// let length = buffer.len();
1753 /// stdin.consume(length);
1754 /// ```
1755 fn fill_buf(&mut self) -> Result<&[u8]>;
1756
1757 /// Tells this buffer that `amt` bytes have been consumed from the buffer,
1758 /// so they should no longer be returned in calls to `read`.
1759 ///
1760 /// This function is a lower-level call. It needs to be paired with the
1761 /// [`fill_buf`] method to function properly. This function does
1762 /// not perform any I/O, it simply informs this object that some amount of
1763 /// its buffer, returned from [`fill_buf`], has been consumed and should
1764 /// no longer be returned. As such, this function may do odd things if
1765 /// [`fill_buf`] isn't called before calling it.
1766 ///
1767 /// The `amt` must be `<=` the number of bytes in the buffer returned by
1768 /// [`fill_buf`].
1769 ///
1770 /// # Examples
1771 ///
1772 /// Since `consume()` is meant to be used with [`fill_buf`],
1773 /// that method's example includes an example of `consume()`.
1774 ///
1775 /// [`fill_buf`]: BufRead::fill_buf
1776 fn consume(&mut self, amt: usize);
1777
1778 /// Check if the underlying `Read` has any data left to be read.
1779 ///
1780 /// This function may fill the buffer to check for data,
1781 /// so this functions returns `Result<bool>`, not `bool`.
1782 ///
1783 /// Default implementation calls `fill_buf` and checks that
1784 /// returned slice is empty (which means that there is no data left,
1785 /// since EOF is reached).
1786 ///
1787 /// Examples
1788 ///
1789 /// ```
1790 /// #![feature(buf_read_has_data_left)]
1791 /// use std::io;
1792 /// use std::io::prelude::*;
1793 ///
1794 /// let stdin = io::stdin();
1795 /// let mut stdin = stdin.lock();
1796 ///
1797 /// while stdin.has_data_left().unwrap() {
1798 /// let mut line = String::new();
1799 /// stdin.read_line(&mut line).unwrap();
1800 /// // work with line
1801 /// println!("{line:?}");
1802 /// }
1803 /// ```
1804 fn has_data_left(&mut self) -> Result<bool> {
1805 self.fill_buf().map(|b| !b.is_empty())
1806 }
1807
1808 /// Read all bytes into `buf` until the delimiter `byte` or EOF is reached.
1809 ///
1810 /// This function will read bytes from the underlying stream until the
1811 /// delimiter or EOF is found. Once found, all bytes up to, and including,
1812 /// the delimiter (if found) will be appended to `buf`.
1813 ///
1814 /// If successful, this function will return the total number of bytes read.
1815 ///
1816 /// This function is blocking and should be used carefully: it is possible for
1817 /// an attacker to continuously send bytes without ever sending the delimiter
1818 /// or EOF.
1819 ///
1820 /// # Errors
1821 ///
1822 /// This function will ignore all instances of [`ErrorKind::Interrupted`] and
1823 /// will otherwise return any errors returned by [`fill_buf`].
1824 ///
1825 /// If an I/O error is encountered then all bytes read so far will be
1826 /// present in `buf` and its length will have been adjusted appropriately.
1827 ///
1828 /// [`fill_buf`]: BufRead::fill_buf
1829 ///
1830 /// # Examples
1831 ///
1832 /// [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In
1833 /// this example, we use [`Cursor`] to read all the bytes in a byte slice
1834 /// in hyphen delimited segments:
1835 ///
1836 /// ```
1837 /// use std::io::{self, BufRead};
1838 ///
1839 /// let mut cursor = io::Cursor::new(b"lorem-ipsum");
1840 /// let mut buf = vec![];
1841 ///
1842 /// // cursor is at 'l'
1843 /// let num_bytes = cursor.read_until(b'-', &mut buf)
1844 /// .expect("reading from cursor won't fail");
1845 /// assert_eq!(num_bytes, 6);
1846 /// assert_eq!(buf, b"lorem-");
1847 /// buf.clear();
1848 ///
1849 /// // cursor is at 'i'
1850 /// let num_bytes = cursor.read_until(b'-', &mut buf)
1851 /// .expect("reading from cursor won't fail");
1852 /// assert_eq!(num_bytes, 5);
1853 /// assert_eq!(buf, b"ipsum");
1854 /// buf.clear();
1855 ///
1856 /// // cursor is at EOF
1857 /// let num_bytes = cursor.read_until(b'-', &mut buf)
1858 /// .expect("reading from cursor won't fail");
1859 /// assert_eq!(num_bytes, 0);
1860 /// assert_eq!(buf, b"");
1861 /// ```
1862 fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> Result<usize> {
1863 read_until(self, byte, buf)
1864 }
1865
1866 /// Skip all bytes until the delimiter `byte` or EOF is reached.
1867 ///
1868 /// This function will read (and discard) bytes from the underlying stream until the
1869 /// delimiter or EOF is found.
1870 ///
1871 /// If successful, this function will return the total number of bytes read,
1872 /// including the delimiter byte.
1873 ///
1874 /// This is useful for efficiently skipping data such as NUL-terminated strings
1875 /// in binary file formats without buffering.
1876 ///
1877 /// This function is blocking and should be used carefully: it is possible for
1878 /// an attacker to continuously send bytes without ever sending the delimiter
1879 /// or EOF.
1880 ///
1881 /// # Errors
1882 ///
1883 /// This function will ignore all instances of [`ErrorKind::Interrupted`] and
1884 /// will otherwise return any errors returned by [`fill_buf`].
1885 ///
1886 /// If an I/O error is encountered then all bytes read so far will be
1887 /// present in `buf` and its length will have been adjusted appropriately.
1888 ///
1889 /// [`fill_buf`]: BufRead::fill_buf
1890 ///
1891 /// # Examples
1892 ///
1893 /// [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In
1894 /// this example, we use [`Cursor`] to read some NUL-terminated information
1895 /// about Ferris from a binary string, skipping the fun fact:
1896 ///
1897 /// ```
1898 /// #![feature(bufread_skip_until)]
1899 ///
1900 /// use std::io::{self, BufRead};
1901 ///
1902 /// let mut cursor = io::Cursor::new(b"Ferris\0Likes long walks on the beach\0Crustacean\0");
1903 ///
1904 /// // read name
1905 /// let mut name = Vec::new();
1906 /// let num_bytes = cursor.read_until(b'\0', &mut name)
1907 /// .expect("reading from cursor won't fail");
1908 /// assert_eq!(num_bytes, 7);
1909 /// assert_eq!(name, b"Ferris\0");
1910 ///
1911 /// // skip fun fact
1912 /// let num_bytes = cursor.skip_until(b'\0')
1913 /// .expect("reading from cursor won't fail");
1914 /// assert_eq!(num_bytes, 30);
1915 ///
1916 /// // read animal type
1917 /// let mut animal = Vec::new();
1918 /// let num_bytes = cursor.read_until(b'\0', &mut animal)
1919 /// .expect("reading from cursor won't fail");
1920 /// assert_eq!(num_bytes, 11);
1921 /// assert_eq!(animal, b"Crustacean\0");
1922 /// ```
1923 fn skip_until(&mut self, byte: u8) -> Result<usize> {
1924 skip_until(self, byte)
1925 }
1926
1927 /// Read all bytes until a newline (the `0xA` byte) is reached, and append
1928 /// them to the provided `String` buffer.
1929 ///
1930 /// Previous content of the buffer will be preserved. To avoid appending to
1931 /// the buffer, you need to [`clear`] it first.
1932 ///
1933 /// This function will read bytes from the underlying stream until the
1934 /// newline delimiter (the `0xA` byte) or EOF is found. Once found, all bytes
1935 /// up to, and including, the delimiter (if found) will be appended to
1936 /// `buf`.
1937 ///
1938 /// If successful, this function will return the total number of bytes read.
1939 ///
1940 /// If this function returns [`Ok(0)`], the stream has reached EOF.
1941 ///
1942 /// This function is blocking and should be used carefully: it is possible for
1943 /// an attacker to continuously send bytes without ever sending a newline
1944 /// or EOF. You can use [`take`] to limit the maximum number of bytes read.
1945 ///
1946 /// [`Ok(0)`]: Ok
1947 /// [`clear`]: String::clear
1948 /// [`take`]: crate::io::Read::take
1949 ///
1950 /// # Errors
1951 ///
1952 /// This function has the same error semantics as [`read_until`] and will
1953 /// also return an error if the read bytes are not valid UTF-8. If an I/O
1954 /// error is encountered then `buf` may contain some bytes already read in
1955 /// the event that all data read so far was valid UTF-8.
1956 ///
1957 /// [`read_until`]: BufRead::read_until
1958 ///
1959 /// # Examples
1960 ///
1961 /// [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In
1962 /// this example, we use [`Cursor`] to read all the lines in a byte slice:
1963 ///
1964 /// ```
1965 /// use std::io::{self, BufRead};
1966 ///
1967 /// let mut cursor = io::Cursor::new(b"foo\nbar");
1968 /// let mut buf = String::new();
1969 ///
1970 /// // cursor is at 'f'
1971 /// let num_bytes = cursor.read_line(&mut buf)
1972 /// .expect("reading from cursor won't fail");
1973 /// assert_eq!(num_bytes, 4);
1974 /// assert_eq!(buf, "foo\n");
1975 /// buf.clear();
1976 ///
1977 /// // cursor is at 'b'
1978 /// let num_bytes = cursor.read_line(&mut buf)
1979 /// .expect("reading from cursor won't fail");
1980 /// assert_eq!(num_bytes, 3);
1981 /// assert_eq!(buf, "bar");
1982 /// buf.clear();
1983 ///
1984 /// // cursor is at EOF
1985 /// let num_bytes = cursor.read_line(&mut buf)
1986 /// .expect("reading from cursor won't fail");
1987 /// assert_eq!(num_bytes, 0);
1988 /// assert_eq!(buf, "");
1989 /// ```
1990 fn read_line(&mut self, buf: &mut String) -> Result<usize> {
1991 // Note that we are not calling the `.read_until` method here, but
1992 // rather our hardcoded implementation. For more details as to why, see
1993 // the comments in `read_to_end`.
1994 unsafe { append_to_string(buf, |b| read_until(self, b'\n', b)) }
1995 }
1996
1997 /// Returns an iterator over the contents of this reader split on the byte
1998 /// `byte`.
1999 ///
2000 /// The iterator returned from this function will return instances of
2001 /// <code>[io::Result]<[Vec]\<u8>></code>. Each vector returned will *not* have
2002 /// the delimiter byte at the end.
2003 ///
2004 /// This function will yield errors whenever [`read_until`] would have
2005 /// also yielded an error.
2006 ///
2007 /// [io::Result]: self::Result "io::Result"
2008 /// [`read_until`]: BufRead::read_until
2009 ///
2010 /// # Examples
2011 ///
2012 /// [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In
2013 /// this example, we use [`Cursor`] to iterate over all hyphen delimited
2014 /// segments in a byte slice
2015 ///
2016 /// ```
2017 /// use std::io::{self, BufRead};
2018 ///
2019 /// let cursor = io::Cursor::new(b"lorem-ipsum-dolor");
2020 ///
2021 /// let mut split_iter = cursor.split(b'-').map(|l| l.unwrap());
2022 /// assert_eq!(split_iter.next(), Some(b"lorem".to_vec()));
2023 /// assert_eq!(split_iter.next(), Some(b"ipsum".to_vec()));
2024 /// assert_eq!(split_iter.next(), Some(b"dolor".to_vec()));
2025 /// assert_eq!(split_iter.next(), None);
2026 /// ```
2027 fn split(self, byte: u8) -> Split<Self>
2028 where
2029 Self: Sized,
2030 {
2031 Split {
2032 buf: self,
2033 delim: byte,
2034 }
2035 }
2036
2037 /// Returns an iterator over the lines of this reader.
2038 ///
2039 /// The iterator returned from this function will yield instances of
2040 /// <code>[io::Result]<[String]></code>. Each string returned will *not* have a newline
2041 /// byte (the `0xA` byte) or `CRLF` (`0xD`, `0xA` bytes) at the end.
2042 ///
2043 /// [io::Result]: self::Result "io::Result"
2044 ///
2045 /// # Examples
2046 ///
2047 /// [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In
2048 /// this example, we use [`Cursor`] to iterate over all the lines in a byte
2049 /// slice.
2050 ///
2051 /// ```
2052 /// use std::io::{self, BufRead};
2053 ///
2054 /// let cursor = io::Cursor::new(b"lorem\nipsum\r\ndolor");
2055 ///
2056 /// let mut lines_iter = cursor.lines().map(|l| l.unwrap());
2057 /// assert_eq!(lines_iter.next(), Some(String::from("lorem")));
2058 /// assert_eq!(lines_iter.next(), Some(String::from("ipsum")));
2059 /// assert_eq!(lines_iter.next(), Some(String::from("dolor")));
2060 /// assert_eq!(lines_iter.next(), None);
2061 /// ```
2062 ///
2063 /// # Errors
2064 ///
2065 /// Each line of the iterator has the same error semantics as [`BufRead::read_line`].
2066 fn lines(self) -> Lines<Self>
2067 where
2068 Self: Sized,
2069 {
2070 Lines { buf: self }
2071 }
2072}
2073
2074/// Adapter to chain together two readers.
2075///
2076/// This struct is generally created by calling [`chain`] on a reader.
2077/// Please see the documentation of [`chain`] for more details.
2078///
2079/// [`chain`]: Read::chain
2080#[derive(Debug)]
2081pub struct Chain<T, U> {
2082 first: T,
2083 second: U,
2084 done_first: bool,
2085}
2086
2087impl<T, U> Chain<T, U> {
2088 /// Consumes the `Chain`, returning the wrapped readers.
2089 ///
2090 /// # Examples
2091 ///
2092 /// ```no_run
2093 /// use std::io;
2094 /// use std::io::prelude::*;
2095 /// use std::fs::File;
2096 ///
2097 /// fn main() -> io::Result<()> {
2098 /// let mut foo_file = File::open("foo.txt")?;
2099 /// let mut bar_file = File::open("bar.txt")?;
2100 ///
2101 /// let chain = foo_file.chain(bar_file);
2102 /// let (foo_file, bar_file) = chain.into_inner();
2103 /// Ok(())
2104 /// }
2105 /// ```
2106 pub fn into_inner(self) -> (T, U) {
2107 (self.first, self.second)
2108 }
2109
2110 /// Gets references to the underlying readers in this `Chain`.
2111 ///
2112 /// # Examples
2113 ///
2114 /// ```no_run
2115 /// use std::io;
2116 /// use std::io::prelude::*;
2117 /// use std::fs::File;
2118 ///
2119 /// fn main() -> io::Result<()> {
2120 /// let mut foo_file = File::open("foo.txt")?;
2121 /// let mut bar_file = File::open("bar.txt")?;
2122 ///
2123 /// let chain = foo_file.chain(bar_file);
2124 /// let (foo_file, bar_file) = chain.get_ref();
2125 /// Ok(())
2126 /// }
2127 /// ```
2128 pub fn get_ref(&self) -> (&T, &U) {
2129 (&self.first, &self.second)
2130 }
2131
2132 /// Gets mutable references to the underlying readers in this `Chain`.
2133 ///
2134 /// Care should be taken to avoid modifying the internal I/O state of the
2135 /// underlying readers as doing so may corrupt the internal state of this
2136 /// `Chain`.
2137 ///
2138 /// # Examples
2139 ///
2140 /// ```no_run
2141 /// use std::io;
2142 /// use std::io::prelude::*;
2143 /// use std::fs::File;
2144 ///
2145 /// fn main() -> io::Result<()> {
2146 /// let mut foo_file = File::open("foo.txt")?;
2147 /// let mut bar_file = File::open("bar.txt")?;
2148 ///
2149 /// let mut chain = foo_file.chain(bar_file);
2150 /// let (foo_file, bar_file) = chain.get_mut();
2151 /// Ok(())
2152 /// }
2153 /// ```
2154 pub fn get_mut(&mut self) -> (&mut T, &mut U) {
2155 (&mut self.first, &mut self.second)
2156 }
2157}
2158impl<T: Read, U: Read> Read for Chain<T, U> {
2159 fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
2160 if !self.done_first {
2161 match self.first.read(buf)? {
2162 0 if !buf.is_empty() => self.done_first = true,
2163 n => return Ok(n),
2164 }
2165 }
2166 self.second.read(buf)
2167 }
2168 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
2169 let mut read = 0;
2170 if !self.done_first {
2171 read += self.first.read_to_end(buf)?;
2172 self.done_first = true;
2173 }
2174 read += self.second.read_to_end(buf)?;
2175 Ok(read)
2176 }
2177
2178 // We don't override `read_to_string` here because an UTF-8 sequence could
2179 // be split between the two parts of the chain
2180
2181 fn read_buf(&mut self, mut buf: BorrowedCursor<'_>) -> Result<()> {
2182 if buf.capacity() == 0 {
2183 return Ok(());
2184 }
2185
2186 if !self.done_first {
2187 let old_len = buf.written();
2188 self.first.read_buf(buf.reborrow())?;
2189
2190 if buf.written() != old_len {
2191 return Ok(());
2192 } else {
2193 self.done_first = true;
2194 }
2195 }
2196 self.second.read_buf(buf)
2197 }
2198}
2199impl<T: BufRead, U: BufRead> BufRead for Chain<T, U> {
2200 fn fill_buf(&mut self) -> Result<&[u8]> {
2201 if !self.done_first {
2202 match self.first.fill_buf()? {
2203 [] => self.done_first = true,
2204 buf => return Ok(buf),
2205 }
2206 }
2207 self.second.fill_buf()
2208 }
2209
2210 fn consume(&mut self, amt: usize) {
2211 if !self.done_first {
2212 self.first.consume(amt)
2213 } else {
2214 self.second.consume(amt)
2215 }
2216 }
2217
2218 fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> Result<usize> {
2219 let mut read = 0;
2220 if !self.done_first {
2221 let n = self.first.read_until(byte, buf)?;
2222 read += n;
2223
2224 match buf.last() {
2225 Some(b) if *b == byte && n != 0 => return Ok(read),
2226 _ => self.done_first = true,
2227 }
2228 }
2229 read += self.second.read_until(byte, buf)?;
2230 Ok(read)
2231 }
2232
2233 // We don't override `read_line` here because an UTF-8 sequence could be
2234 // split between the two parts of the chain
2235}
2236impl<T: SizeHint, U: SizeHint> SizeHint for Chain<T, U> {
2237 #[inline]
2238 fn lower_bound(&self) -> usize {
2239 SizeHint::lower_bound(&self.first) + SizeHint::lower_bound(&self.second)
2240 }
2241
2242 #[inline]
2243 fn upper_bound(&self) -> Option<usize> {
2244 match (
2245 SizeHint::upper_bound(&self.first),
2246 SizeHint::upper_bound(&self.second),
2247 ) {
2248 (Some(first), Some(second)) => first.checked_add(second),
2249 _ => None,
2250 }
2251 }
2252}
2253
2254/// Reader adapter which limits the bytes read from an underlying reader.
2255///
2256/// This struct is generally created by calling [`take`] on a reader.
2257/// Please see the documentation of [`take`] for more details.
2258///
2259/// [`take`]: Read::take
2260#[derive(Debug)]
2261pub struct Take<T> {
2262 inner: T,
2263 limit: u64,
2264}
2265
2266impl<T> Take<T> {
2267 /// Returns the number of bytes that can be read before this instance will
2268 /// return EOF.
2269 ///
2270 /// # Note
2271 ///
2272 /// This instance may reach `EOF` after reading fewer bytes than indicated by
2273 /// this method if the underlying [`Read`] instance reaches EOF.
2274 ///
2275 /// # Examples
2276 ///
2277 /// ```no_run
2278 /// use std::io;
2279 /// use std::io::prelude::*;
2280 /// use std::fs::File;
2281 ///
2282 /// fn main() -> io::Result<()> {
2283 /// let f = File::open("foo.txt")?;
2284 ///
2285 /// // read at most five bytes
2286 /// let handle = f.take(5);
2287 ///
2288 /// println!("limit: {}", handle.limit());
2289 /// Ok(())
2290 /// }
2291 /// ```
2292 pub fn limit(&self) -> u64 {
2293 self.limit
2294 }
2295
2296 /// Sets the number of bytes that can be read before this instance will
2297 /// return EOF. This is the same as constructing a new `Take` instance, so
2298 /// the amount of bytes read and the previous limit value don't matter when
2299 /// calling this method.
2300 ///
2301 /// # Examples
2302 ///
2303 /// ```no_run
2304 /// use std::io;
2305 /// use std::io::prelude::*;
2306 /// use std::fs::File;
2307 ///
2308 /// fn main() -> io::Result<()> {
2309 /// let f = File::open("foo.txt")?;
2310 ///
2311 /// // read at most five bytes
2312 /// let mut handle = f.take(5);
2313 /// handle.set_limit(10);
2314 ///
2315 /// assert_eq!(handle.limit(), 10);
2316 /// Ok(())
2317 /// }
2318 /// ```
2319 pub fn set_limit(&mut self, limit: u64) {
2320 self.limit = limit;
2321 }
2322
2323 /// Consumes the `Take`, returning the wrapped reader.
2324 ///
2325 /// # Examples
2326 ///
2327 /// ```no_run
2328 /// use std::io;
2329 /// use std::io::prelude::*;
2330 /// use std::fs::File;
2331 ///
2332 /// fn main() -> io::Result<()> {
2333 /// let mut file = File::open("foo.txt")?;
2334 ///
2335 /// let mut buffer = [0; 5];
2336 /// let mut handle = file.take(5);
2337 /// handle.read(&mut buffer)?;
2338 ///
2339 /// let file = handle.into_inner();
2340 /// Ok(())
2341 /// }
2342 /// ```
2343 pub fn into_inner(self) -> T {
2344 self.inner
2345 }
2346
2347 /// Gets a reference to the underlying reader.
2348 ///
2349 /// # Examples
2350 ///
2351 /// ```no_run
2352 /// use std::io;
2353 /// use std::io::prelude::*;
2354 /// use std::fs::File;
2355 ///
2356 /// fn main() -> io::Result<()> {
2357 /// let mut file = File::open("foo.txt")?;
2358 ///
2359 /// let mut buffer = [0; 5];
2360 /// let mut handle = file.take(5);
2361 /// handle.read(&mut buffer)?;
2362 ///
2363 /// let file = handle.get_ref();
2364 /// Ok(())
2365 /// }
2366 /// ```
2367 pub fn get_ref(&self) -> &T {
2368 &self.inner
2369 }
2370
2371 /// Gets a mutable reference to the underlying reader.
2372 ///
2373 /// Care should be taken to avoid modifying the internal I/O state of the
2374 /// underlying reader as doing so may corrupt the internal limit of this
2375 /// `Take`.
2376 ///
2377 /// # Examples
2378 ///
2379 /// ```no_run
2380 /// use std::io;
2381 /// use std::io::prelude::*;
2382 /// use std::fs::File;
2383 ///
2384 /// fn main() -> io::Result<()> {
2385 /// let mut file = File::open("foo.txt")?;
2386 ///
2387 /// let mut buffer = [0; 5];
2388 /// let mut handle = file.take(5);
2389 /// handle.read(&mut buffer)?;
2390 ///
2391 /// let file = handle.get_mut();
2392 /// Ok(())
2393 /// }
2394 /// ```
2395 pub fn get_mut(&mut self) -> &mut T {
2396 &mut self.inner
2397 }
2398}
2399impl<T: Read> Read for Take<T> {
2400 fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
2401 // Don't call into inner reader at all at EOF because it may still block
2402 if self.limit == 0 {
2403 return Ok(0);
2404 }
2405
2406 let max = cmp::min(buf.len() as u64, self.limit) as usize;
2407 let n = self.inner.read(&mut buf[..max])?;
2408 assert!(n as u64 <= self.limit, "number of read bytes exceeds limit");
2409 self.limit -= n as u64;
2410 Ok(n)
2411 }
2412
2413 fn read_buf(&mut self, mut buf: BorrowedCursor<'_>) -> Result<()> {
2414 // Don't call into inner reader at all at EOF because it may still block
2415 if self.limit == 0 {
2416 return Ok(());
2417 }
2418
2419 if self.limit <= buf.capacity() as u64 {
2420 // if we just use an as cast to convert, limit may wrap around on a 32 bit target
2421 let limit = cmp::min(self.limit, usize::MAX as u64) as usize;
2422
2423 let extra_init = cmp::min(limit as usize, buf.init_ref().len());
2424
2425 // SAFETY: no uninit data is written to ibuf
2426 let ibuf = unsafe { &mut buf.as_mut()[..limit] };
2427
2428 let mut sliced_buf: BorrowedBuf<'_> = ibuf.into();
2429
2430 // SAFETY: extra_init bytes of ibuf are known to be initialized
2431 unsafe {
2432 sliced_buf.set_init(extra_init);
2433 }
2434
2435 let mut cursor = sliced_buf.unfilled();
2436 self.inner.read_buf(cursor.reborrow())?;
2437
2438 let new_init = cursor.init_ref().len();
2439 let filled = sliced_buf.len();
2440
2441 // cursor / sliced_buf / ibuf must drop here
2442
2443 unsafe {
2444 // SAFETY: filled bytes have been filled and therefore initialized
2445 buf.advance_unchecked(filled);
2446 // SAFETY: new_init bytes of buf's unfilled buffer have been initialized
2447 buf.set_init(new_init);
2448 }
2449
2450 self.limit -= filled as u64;
2451 } else {
2452 let written = buf.written();
2453 self.inner.read_buf(buf.reborrow())?;
2454 self.limit -= (buf.written() - written) as u64;
2455 }
2456
2457 Ok(())
2458 }
2459}
2460impl<T: BufRead> BufRead for Take<T> {
2461 fn fill_buf(&mut self) -> Result<&[u8]> {
2462 // Don't call into inner reader at all at EOF because it may still block
2463 if self.limit == 0 {
2464 return Ok(&[]);
2465 }
2466
2467 let buf = self.inner.fill_buf()?;
2468 let cap = cmp::min(buf.len() as u64, self.limit) as usize;
2469 Ok(&buf[..cap])
2470 }
2471
2472 fn consume(&mut self, amt: usize) {
2473 // Don't let callers reset the limit by passing an overlarge value
2474 let amt = cmp::min(amt as u64, self.limit) as usize;
2475 self.limit -= amt as u64;
2476 self.inner.consume(amt);
2477 }
2478}
2479impl<T: SizeHint> SizeHint for Take<T> {
2480 #[inline]
2481 fn lower_bound(&self) -> usize {
2482 cmp::min(SizeHint::lower_bound(&self.inner) as u64, self.limit) as usize
2483 }
2484
2485 #[inline]
2486 fn upper_bound(&self) -> Option<usize> {
2487 match SizeHint::upper_bound(&self.inner) {
2488 Some(upper_bound) => Some(cmp::min(upper_bound as u64, self.limit) as usize),
2489 None => self.limit.try_into().ok(),
2490 }
2491 }
2492}
2493
2494/// An iterator over `u8` values of a reader.
2495///
2496/// This struct is generally created by calling [`bytes`] on a reader.
2497/// Please see the documentation of [`bytes`] for more details.
2498///
2499/// [`bytes`]: Read::bytes
2500#[derive(Debug)]
2501pub struct Bytes<R> {
2502 #[allow(unused)]
2503 inner: R,
2504}
2505
2506impl<R: Read> Iterator for Bytes<R> {
2507 type Item = Result<u8>;
2508
2509 fn next(&mut self) -> Option<Result<u8>> {
2510 inlined_slow_read_byte(&mut self.inner)
2511 }
2512
2513 #[inline]
2514 fn size_hint(&self) -> (usize, Option<usize>) {
2515 (0, None)
2516 }
2517}
2518
2519#[allow(unused)]
2520trait SpecReadByte {
2521 fn spec_read_byte(&mut self) -> Option<Result<u8>>;
2522}
2523
2524#[inline]
2525fn inlined_slow_read_byte<R: Read>(reader: &mut R) -> Option<Result<u8>> {
2526 let mut byte = 0;
2527 loop {
2528 return match reader.read(slice::from_mut(&mut byte)) {
2529 Ok(0) => None,
2530 Ok(..) => Some(Ok(byte)),
2531 Err(ref e) if e.is_interrupted() => continue,
2532 Err(e) => Some(Err(e)),
2533 };
2534 }
2535}
2536
2537// Used by `BufReader::spec_read_byte`, for which the `inline(ever)` is
2538// important.
2539#[inline(never)]
2540fn uninlined_slow_read_byte<R: Read>(reader: &mut R) -> Option<Result<u8>> {
2541 inlined_slow_read_byte(reader)
2542}
2543#[allow(unused)]
2544pub(crate) trait SizeHint {
2545 fn lower_bound(&self) -> usize;
2546
2547 fn upper_bound(&self) -> Option<usize>;
2548
2549 fn size_hint(&self) -> (usize, Option<usize>) {
2550 (self.lower_bound(), self.upper_bound())
2551 }
2552}
2553
2554impl<T: SizeHint> SizeHint for &mut T {
2555 #[inline]
2556 fn lower_bound(&self) -> usize {
2557 SizeHint::lower_bound(*self)
2558 }
2559
2560 #[inline]
2561 fn upper_bound(&self) -> Option<usize> {
2562 SizeHint::upper_bound(*self)
2563 }
2564}
2565impl<T: SizeHint> SizeHint for Box<T> {
2566 #[inline]
2567 fn lower_bound(&self) -> usize {
2568 SizeHint::lower_bound(&**self)
2569 }
2570
2571 #[inline]
2572 fn upper_bound(&self) -> Option<usize> {
2573 SizeHint::upper_bound(&**self)
2574 }
2575}
2576
2577impl SizeHint for &[u8] {
2578 #[inline]
2579 fn lower_bound(&self) -> usize {
2580 self.len()
2581 }
2582
2583 #[inline]
2584 fn upper_bound(&self) -> Option<usize> {
2585 Some(self.len())
2586 }
2587}
2588
2589/// An iterator over the contents of an instance of `BufRead` split on a
2590/// particular byte.
2591///
2592/// This struct is generally created by calling [`split`] on a `BufRead`.
2593/// Please see the documentation of [`split`] for more details.
2594///
2595/// [`split`]: BufRead::split
2596#[derive(Debug)]
2597pub struct Split<B> {
2598 buf: B,
2599 delim: u8,
2600}
2601impl<B: BufRead> Iterator for Split<B> {
2602 type Item = Result<Vec<u8>>;
2603
2604 fn next(&mut self) -> Option<Result<Vec<u8>>> {
2605 let mut buf = Vec::new();
2606 match self.buf.read_until(self.delim, &mut buf) {
2607 Ok(0) => None,
2608 Ok(_n) => {
2609 if buf[buf.len() - 1] == self.delim {
2610 buf.pop();
2611 }
2612 Some(Ok(buf))
2613 }
2614 Err(e) => Some(Err(e)),
2615 }
2616 }
2617}
2618
2619/// An iterator over the lines of an instance of `BufRead`.
2620///
2621/// This struct is generally created by calling [`lines`] on a `BufRead`.
2622/// Please see the documentation of [`lines`] for more details.
2623///
2624/// [`lines`]: BufRead::lines
2625#[derive(Debug)]
2626pub struct Lines<B> {
2627 buf: B,
2628}
2629impl<B: BufRead> Iterator for Lines<B> {
2630 type Item = Result<String>;
2631
2632 fn next(&mut self) -> Option<Result<String>> {
2633 let mut buf = String::new();
2634 match self.buf.read_line(&mut buf) {
2635 Ok(0) => None,
2636 Ok(_n) => {
2637 if buf.ends_with('\n') {
2638 buf.pop();
2639 if buf.ends_with('\r') {
2640 buf.pop();
2641 }
2642 }
2643 Some(Ok(buf))
2644 }
2645 Err(e) => Some(Err(e)),
2646 }
2647 }
2648}