mycelium_util/io.rs
1//! A modified version of the Rust standard library's `std::io` module, suitable
2//! for use in Mycelium and other kernels.
3//!
4//! The traits and functions here are mostly identical to (and copied from!) the
5//! standard library's `std::io` module, with the following differences:
6//!
7//! - `Read::read_to_end` and `Read::read_to_string` require the "alloc" feature
8//! flag on `mycelium_util` (which is enabled by default). Bootloaders & other
9//! code which cannot allocate may disable this feature flag.
10//! - `Read::read_vectored` and `Write::write_vectored` do not exist.
11//! - Most of the `BufRead` utility functions (`lines`, `split`, `read_until`)
12//! require the "alloc" feature
13//! - `BufReader` and `BufWriter` require the "alloc" feature (and are not yet implemented).
14//! - The `io::Error` type takes an `ErrorKind` and an optional `&'static str`,
15//! and does not wrap another error. This is because we need to be able to
16//! construct I/O errors even when we cannot allocate.
17mod cursor;
18mod error;
19mod impls;
20mod initializer;
21pub mod prelude;
22mod util;
23pub use self::cursor::Cursor;
24pub use self::error::{Error, ErrorKind, Result};
25pub use self::initializer::Initializer;
26pub use self::util::{copy, empty, repeat, sink, Empty, Repeat, Sink};
27use core::{cmp, fmt, slice, str};
28
29#[cfg(feature = "alloc")]
30use alloc::{string::String, vec::Vec};
31// FIXME(eliza): calculate based on arch?
32const DEFAULT_BUF_SIZE: usize = 256;
33
34/// The `Read` trait allows for reading bytes from a source.
35///
36/// Implementors of the `Read` trait are called 'readers'.
37///
38/// Readers are defined by one required method, [`read()`]. Each call to [`read()`]
39/// will attempt to pull bytes from this source into a provided buffer. A
40/// number of other methods are implemented in terms of [`read()`], giving
41/// implementors a number of ways to read bytes while only needing to implement
42/// a single method.
43///
44/// This is essentially a vendored version of the [`std::io::Read`] trait from
45/// the Rust standard library, modified to work without `std`. See the
46/// module-level docs for [`mycelium_util::io`] for more information on how
47/// `mycelium_util`'s `io` module differs from `std`'s.
48///
49/// [`read()`]: Self::read
50/// [`mycelium_util::io`]: crate::io
51pub trait Read {
52 /// Pull some bytes from this source into the specified buffer, returning
53 /// how many bytes were read.
54 ///
55 /// This function does not provide any guarantees about whether it blocks
56 /// waiting for data, but if an object needs to block for a read but cannot
57 /// it will typically signal this via an [`Err`] return value.
58 ///
59 /// If the return value of this method is [`Ok(n)`], then it must be
60 /// guaranteed that `0 <= n <= buf.len()`. A nonzero `n` value indicates
61 /// that the buffer `buf` has been filled in with `n` bytes of data from this
62 /// source. If `n` is `0`, then it can indicate one of two scenarios:
63 ///
64 /// 1. This reader has reached its "end of file" and will likely no longer
65 /// be able to produce bytes. Note that this does not mean that the
66 /// reader will *always* no longer be able to produce bytes.
67 /// 2. The buffer specified was 0 bytes in length.
68 ///
69 /// No guarantees are provided about the contents of `buf` when this
70 /// function is called, implementations cannot rely on any property of the
71 /// contents of `buf` being true. It is recommended that *implementations*
72 /// only write data to `buf` instead of reading its contents.
73 ///
74 /// Correspondingly, however, *callers* of this method may not assume any guarantees
75 /// about how the implementation uses `buf`. The trait is safe to implement,
76 /// so it is possible that the code that's supposed to write to the buffer might also read
77 /// from it. It is your responsibility to make sure that `buf` is initialized
78 /// before calling `read`. Calling `read` with an uninitialized `buf` (of the kind one
79 /// obtains via [`MaybeUninit<T>`]) is not safe, and can lead to undefined behavior.
80 ///
81 /// [`MaybeUninit<T>`]: ../mem/union.MaybeUninit.html
82 ///
83 /// # Errors
84 ///
85 /// If this function encounters any form of I/O or other error, an error
86 /// variant will be returned. If an error is returned then it must be
87 /// guaranteed that no bytes were read.
88 ///
89 /// An error of the [`ErrorKind::Interrupted`] kind is non-fatal and the read
90 /// operation should be retried if there is nothing else to do.
91 fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
92
93 /// Determines if this `Read`er can work with buffers of uninitialized
94 /// memory.
95 ///
96 /// The default implementation returns an initializer which will zero
97 /// buffers.
98 ///
99 /// If a `Read`er guarantees that it can work properly with uninitialized
100 /// memory, it should call [`Initializer::nop()`]. See the documentation for
101 /// [`Initializer`] for details.
102 ///
103 /// The behavior of this method must be independent of the state of the
104 /// `Read`er - the method only takes `&self` so that it can be used through
105 /// trait objects.
106 ///
107 /// # Safety
108 ///
109 /// This method is unsafe because a `Read`er could otherwise return a
110 /// non-zeroing `Initializer` from another `Read` type without an `unsafe`
111 /// block.
112 ///
113 /// [`Initializer::nop()`]: struct.Initializer.html#method.nop
114 /// [`Initializer`]: struct.Initializer.html
115 #[inline]
116 unsafe fn initializer(&self) -> Initializer {
117 Initializer::zeroing()
118 }
119
120 /// Read all bytes until EOF in this source, placing them into `buf`.
121 ///
122 /// All bytes read from this source will be appended to the specified buffer
123 /// `buf`. This function will continuously call [`read()`] to append more data to
124 /// `buf` until [`read()`] returns either [`Ok(0)`] or an error of
125 /// non-[`ErrorKind::Interrupted`] kind.
126 ///
127 /// If successful, this function will return the total number of bytes read.
128 ///
129 /// # Errors
130 ///
131 /// If this function encounters an error of the kind
132 /// [`ErrorKind::Interrupted`] then the error is ignored and the operation
133 /// will continue.
134 ///
135 /// If any other read error is encountered then this function immediately
136 /// returns. Any bytes which have already been read will be appended to
137 /// `buf`.
138 ///
139 /// [`read()`]: trait.Read.html#tymethod.read
140 /// [`ErrorKind::Interrupted`]: enum.ErrorKind.html#variant.Interrupted
141 #[cfg(feature = "alloc")]
142 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
143 read_to_end(self, buf)
144 }
145
146 /// Read all bytes until EOF in this source, appending them to `buf`.
147 ///
148 /// If successful, this function returns the number of bytes which were read
149 /// and appended to `buf`.
150 ///
151 /// # Errors
152 ///
153 /// If the data in this stream is *not* valid UTF-8 then an error is
154 /// returned and `buf` is unchanged.
155 ///
156 /// See [`read_to_end`][readtoend] for other error semantics.
157 ///
158 /// [readtoend]: #method.read_to_end
159 #[cfg(feature = "alloc")]
160 fn read_to_string(&mut self, buf: &mut String) -> Result<usize> {
161 // Note that we do *not* call `.read_to_end()` here. We are passing
162 // `&mut Vec<u8>` (the raw contents of `buf`) into the `read_to_end`
163 // method to fill it up. An arbitrary implementation could overwrite the
164 // entire contents of the vector, not just append to it (which is what
165 // we are expecting).
166 //
167 // To prevent extraneously checking the UTF-8-ness of the entire buffer
168 // we pass it to our hardcoded `read_to_end` implementation which we
169 // know is guaranteed to only read data into the end of the buffer.
170 append_to_string(buf, |b| read_to_end(self, b))
171 }
172
173 /// Read the exact number of bytes required to fill `buf`.
174 ///
175 /// This function reads as many bytes as necessary to completely fill the
176 /// specified buffer `buf`.
177 ///
178 /// No guarantees are provided about the contents of `buf` when this
179 /// function is called, implementations cannot rely on any property of the
180 /// contents of `buf` being true. It is recommended that implementations
181 /// only write data to `buf` instead of reading its contents.
182 ///
183 /// # Errors
184 ///
185 /// If this function encounters an error of the kind
186 /// [`ErrorKind::Interrupted`] then the error is ignored and the operation
187 /// will continue.
188 ///
189 /// If this function encounters an "end of file" before completely filling
190 /// the buffer, it returns an error of the kind [`ErrorKind::UnexpectedEof`].
191 /// The contents of `buf` are unspecified in this case.
192 ///
193 /// If any other read error is encountered then this function immediately
194 /// returns. The contents of `buf` are unspecified in this case.
195 ///
196 /// If this function returns an error, it is unspecified how many bytes it
197 /// has read, but it will never read more than would be necessary to
198 /// completely fill the buffer.
199 ///
200 /// [`ErrorKind::Interrupted`]: enum.ErrorKind.html#variant.Interrupted
201 /// [`ErrorKind::UnexpectedEof`]: enum.ErrorKind.html#variant.UnexpectedEof
202 fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<()> {
203 while !buf.is_empty() {
204 match self.read(buf) {
205 Ok(0) => break,
206 Ok(n) => {
207 let tmp = buf;
208 buf = &mut tmp[n..];
209 }
210 Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
211 Err(e) => return Err(e),
212 }
213 }
214 if !buf.is_empty() {
215 Err(Error::new(
216 ErrorKind::UnexpectedEof,
217 "failed to fill whole buffer",
218 ))
219 } else {
220 Ok(())
221 }
222 }
223
224 /// Creates a "by reference" adaptor for this instance of `Read`.
225 ///
226 /// The returned adaptor also implements `Read` and will simply borrow this
227 /// current reader.
228 fn by_ref(&mut self) -> &mut Self
229 where
230 Self: Sized,
231 {
232 self
233 }
234
235 /// Transforms this `Read` instance to an `Iterator` over its bytes.
236 ///
237 /// The returned type implements [`Iterator`] where the `Item` is
238 /// [`Result`]`<`[`u8`]`, `[`io::Error`]`>`.
239 /// The yielded item is [`Ok`] if a byte was successfully read and [`Err`]
240 /// otherwise. EOF is mapped to returning [`None`] from this iterator.
241 ///
242 /// [`Iterator`]: ../../std/iter/trait.Iterator.html
243 /// [`Result`]: ../../std/result/enum.Result.html
244 /// [`io::Error`]: ../../std/io/struct.Error.html
245 /// [`u8`]: ../../std/primitive.u8.html
246 /// [`Ok`]: ../../std/result/enum.Result.html#variant.Ok
247 /// [`Err`]: ../../std/result/enum.Result.html#variant.Err
248 /// [`None`]: ../../std/option/enum.Option.html#variant.None
249 fn bytes(self) -> Bytes<Self>
250 where
251 Self: Sized,
252 {
253 Bytes { inner: self }
254 }
255
256 /// Creates an adaptor which will chain this stream with another.
257 ///
258 /// The returned `Read` instance will first read all bytes from this object
259 /// until EOF is encountered. Afterwards the output is equivalent to the
260 /// output of `next`.
261 ///
262 /// # Examples
263 ///
264 /// [`File`][file]s implement `Read`:
265 ///
266 /// [file]: ../fs/struct.File.html
267 ///
268 /// ```no_run
269 /// use std::io;
270 /// use std::io::prelude::*;
271 /// use std::fs::File;
272 ///
273 /// fn main() -> io::Result<()> {
274 /// let mut f1 = File::open("foo.txt")?;
275 /// let mut f2 = File::open("bar.txt")?;
276 ///
277 /// let mut handle = f1.chain(f2);
278 /// let mut buffer = String::new();
279 ///
280 /// // read the value into a String. We could use any Read method here,
281 /// // this is just one example.
282 /// handle.read_to_string(&mut buffer)?;
283 /// Ok(())
284 /// }
285 /// ```
286 fn chain<R: Read>(self, next: R) -> Chain<Self, R>
287 where
288 Self: Sized,
289 {
290 Chain {
291 first: self,
292 second: next,
293 done_first: false,
294 }
295 }
296
297 /// Creates an adaptor which will read at most `limit` bytes from it.
298 ///
299 /// This function returns a new instance of `Read` which will read at most
300 /// `limit` bytes, after which it will always return EOF ([`Ok(0)`]). Any
301 /// read errors will not count towards the number of bytes read and future
302 /// calls to [`read()`](Self::read) may succeed.
303 fn take(self, limit: u64) -> Take<Self>
304 where
305 Self: Sized,
306 {
307 Take { inner: self, limit }
308 }
309}
310
311/// A trait for objects which are byte-oriented sinks.
312///
313/// Implementors of the `Write` trait are sometimes called 'writers'.
314///
315/// Writers are defined by two required methods, [`write`] and [`flush`]:
316///
317/// * The [`write`] method will attempt to write some data into the object,
318/// returning how many bytes were successfully written.
319///
320/// * The [`flush`] method is useful for adapters and explicit buffers
321/// themselves for ensuring that all buffered data has been pushed out to the
322/// 'true sink'.
323///
324/// This is essentially a vendored version of the [`std::io::Write`] trait from
325/// the Rust standard library, modified to work without `std`. See the
326/// module-level docs for [`mycelium_util::io`] for more information on how
327/// `mycelium_util`'s `io` module differs from `std`'s.
328///
329/// [`write`]: Write::write
330/// [`flush`]: Write::flush
331/// [`mycelium_util::io`]: crate::io
332pub trait Write {
333 /// Write a buffer into this writer, returning how many bytes were written.
334 ///
335 /// This function will attempt to write the entire contents of `buf`, but
336 /// the entire write may not succeed, or the write may also generate an
337 /// error. A call to `write` represents *at most one* attempt to write to
338 /// any wrapped object.
339 ///
340 /// Calls to `write` are not guaranteed to block waiting for data to be
341 /// written, and a write which would otherwise block can be indicated through
342 /// an [`Err`] variant.
343 ///
344 /// If the return value is [`Ok(n)`] then it must be guaranteed that
345 /// `n <= buf.len()`. A return value of `0` typically means that the
346 /// underlying object is no longer able to accept bytes and will likely not
347 /// be able to in the future as well, or that the buffer provided is empty.
348 ///
349 /// # Errors
350 ///
351 /// Each call to `write` may generate an I/O error indicating that the
352 /// operation could not be completed. If an error is returned then no bytes
353 /// in the buffer were written to this writer.
354 ///
355 /// It is **not** considered an error if the entire buffer could not be
356 /// written to this writer.
357 ///
358 /// An error of the [`ErrorKind::Interrupted`] kind is non-fatal and the
359 /// write operation should be retried if there is nothing else to do.
360 ///
361 /// [`Err`]: ../../std/result/enum.Result.html#variant.Err
362 /// [`Ok(n)`]: ../../std/result/enum.Result.html#variant.Ok
363 fn write(&mut self, buf: &[u8]) -> Result<usize>;
364
365 /// Flush this output stream, ensuring that all intermediately buffered
366 /// contents reach their destination.
367 fn flush(&mut self) -> Result<()>;
368
369 /// Attempts to write an entire buffer into this writer.
370 ///
371 /// This method will continuously call [`write`] until there is no more data
372 /// to be written or an error of non-[`ErrorKind::Interrupted`] kind is
373 /// returned. This method will not return until the entire buffer has been
374 /// successfully written or such an error occurs. The first error that is
375 /// not of [`ErrorKind::Interrupted`] kind generated from this method will be
376 /// returned.
377 ///
378 /// # Errors
379 ///
380 /// This function will return the first error of
381 /// non-[`ErrorKind::Interrupted`] kind that [`write`] returns.
382 ///
383 /// [`write`]: #tymethod.write
384 fn write_all(&mut self, mut buf: &[u8]) -> Result<()> {
385 while !buf.is_empty() {
386 match self.write(buf) {
387 Ok(0) => {
388 return Err(Error::new(
389 ErrorKind::WriteZero,
390 "failed to write whole buffer",
391 ))
392 }
393 Ok(n) => buf = &buf[n..],
394 Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
395 Err(e) => return Err(e),
396 }
397 }
398 Ok(())
399 }
400
401 /// Writes a formatted string into this writer, returning any error
402 /// encountered.
403 ///
404 /// This method is primarily used to interface with the
405 /// [`format_args!`][formatargs] macro, but it is rare that this should
406 /// explicitly be called. The [`write!`][write] macro should be favored to
407 /// invoke this method instead.
408 ///
409 /// [formatargs]: ../macro.format_args.html
410 /// [write]: ../macro.write.html
411 ///
412 /// This function internally uses the [`write_all`][writeall] method on
413 /// this trait and hence will continuously write data so long as no errors
414 /// are received. This also means that partial writes are not indicated in
415 /// this signature.
416 ///
417 /// [writeall]: #method.write_all
418 ///
419 /// # Errors
420 ///
421 /// This function will return any I/O error reported while formatting.
422 fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> Result<()> {
423 let mut output = WriteFmtAdaptor::new(self);
424 match fmt::write(&mut output, fmt) {
425 Ok(()) => Ok(()),
426 Err(..) => {
427 // check if the error came from the underlying `Write` or not
428 if output.error.is_err() {
429 output.error
430 } else {
431 Err(Error::new(ErrorKind::Other, "formatter error"))
432 }
433 }
434 }
435 }
436
437 /// Creates a "by reference" adaptor for this instance of `Write`.
438 ///
439 /// The returned adaptor also implements `Write` and will simply borrow this
440 /// current writer.
441 fn by_ref(&mut self) -> &mut Self
442 where
443 Self: Sized,
444 {
445 self
446 }
447}
448
449/// The `Seek` trait provides a cursor which can be moved within a stream of
450/// bytes.
451///
452/// The stream typically has a fixed size, allowing seeking relative to either
453/// end or the current offset.
454pub trait Seek {
455 /// Seek to an offset, in bytes, in a stream.
456 ///
457 /// A seek beyond the end of a stream is allowed, but behavior is defined
458 /// by the implementation.
459 ///
460 /// If the seek operation completed successfully,
461 /// this method returns the new position from the start of the stream.
462 /// That position can be used later with [`SeekFrom::Start`].
463 ///
464 /// # Errors
465 ///
466 /// Seeking to a negative offset is considered an error.
467 ///
468 /// [`SeekFrom::Start`]: enum.SeekFrom.html#variant.Start
469 fn seek(&mut self, pos: SeekFrom) -> Result<u64>;
470
471 /// Returns the length of this stream (in bytes).
472 ///
473 /// This method is implemented using up to three seek operations. If this
474 /// method returns successfully, the seek position is unchanged (i.e. the
475 /// position before calling this method is the same as afterwards).
476 /// However, if this method returns an error, the seek position is
477 /// unspecified.
478 ///
479 /// If you need to obtain the length of *many* streams and you don't care
480 /// about the seek position afterwards, you can reduce the number of seek
481 /// operations by simply calling `seek(SeekFrom::End(0))` and using its
482 /// return value (it is also the stream length).
483 ///
484 /// Note that length of a stream can change over time (for example, when
485 /// data is appended to a file). So calling this method multiple times does
486 /// not necessarily return the same length each time.
487 fn stream_len(&mut self) -> Result<u64> {
488 let old_pos = self.stream_position()?;
489 let len = self.seek(SeekFrom::End(0))?;
490
491 // Avoid seeking a third time when we were already at the end of the
492 // stream. The branch is usually way cheaper than a seek operation.
493 if old_pos != len {
494 self.seek(SeekFrom::Start(old_pos))?;
495 }
496
497 Ok(len)
498 }
499
500 /// Returns the current seek position from the start of the stream.
501 ///
502 /// This is equivalent to `self.seek(SeekFrom::Current(0))`.
503 fn stream_position(&mut self) -> Result<u64> {
504 self.seek(SeekFrom::Current(0))
505 }
506}
507
508/// A `BufRead` is a type of `Read`er which has an internal buffer, allowing it
509/// to perform extra ways of reading.
510///
511/// For example, reading line-by-line is inefficient without using a buffer, so
512/// if you want to read by line, you'll need `BufRead`, which includes a
513/// [`read_line`] method as well as a [`lines`] iterator.
514///
515/// [`BufReader`]: struct.BufReader.html
516/// [`read_line`]: #method.read_line
517/// [`lines`]: #method.lines
518/// [`Read`]: trait.Read.html
519pub trait BufRead: Read {
520 /// Returns the contents of the internal buffer, filling it with more data
521 /// from the inner reader if it is empty.
522 ///
523 /// This function is a lower-level call. It needs to be paired with the
524 /// [`consume`] method to function properly. When calling this
525 /// method, none of the contents will be "read" in the sense that later
526 /// calling `read` may return the same contents. As such, [`consume`] must
527 /// be called with the number of bytes that are consumed from this buffer to
528 /// ensure that the bytes are never returned twice.
529 ///
530 /// [`consume`]: Self::consume
531 ///
532 /// An empty buffer returned indicates that the stream has reached EOF.
533 ///
534 /// # Errors
535 ///
536 /// This function will return an I/O error if the underlying reader was
537 /// read, but returned an error.
538 fn fill_buf(&mut self) -> Result<&[u8]>;
539
540 /// Tells this buffer that `amt` bytes have been consumed from the buffer,
541 /// so they should no longer be returned in calls to `read`.
542 ///
543 /// This function is a lower-level call. It needs to be paired with the
544 /// [`fill_buf`] method to function properly. This function does
545 /// not perform any I/O, it simply informs this object that some amount of
546 /// its buffer, returned from [`fill_buf`], has been consumed and should
547 /// no longer be returned. As such, this function may do odd things if
548 /// [`fill_buf`] isn't called before calling it.
549 ///
550 /// The `amt` must be `<=` the number of bytes in the buffer returned by
551 /// [`fill_buf`].
552 ///
553 /// [`fill_buf`]: Self::fill_buf
554 fn consume(&mut self, amt: usize);
555
556 /// Read all bytes into `buf` until the delimiter `byte` or EOF is reached.
557 ///
558 /// This function will read bytes from the underlying stream until the
559 /// delimiter or EOF is found. Once found, all bytes up to, and including,
560 /// the delimiter (if found) will be appended to `buf`.
561 ///
562 /// If successful, this function will return the total number of bytes read.
563 ///
564 /// # Errors
565 ///
566 /// This function will ignore all instances of [`ErrorKind::Interrupted`] and
567 /// will otherwise return any errors returned by [`fill_buf`].
568 ///
569 /// If an I/O error is encountered then all bytes read so far will be
570 /// present in `buf` and its length will have been adjusted appropriately.
571 ///
572 /// [`fill_buf`]: Self::fill_buf
573 #[cfg(feature = "alloc")]
574 fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> Result<usize> {
575 read_until(self, byte, buf)
576 }
577
578 /// Read all bytes until a newline (the 0xA byte) is reached, and append
579 /// them to the provided buffer.
580 ///
581 /// This function will read bytes from the underlying stream until the
582 /// newline delimiter (the 0xA byte) or EOF is found. Once found, all bytes
583 /// up to, and including, the delimiter (if found) will be appended to
584 /// `buf`.
585 ///
586 /// If successful, this function will return the total number of bytes read.
587 ///
588 /// If this function returns `Ok(0)`, the stream has reached EOF.
589 ///
590 /// # Errors
591 ///
592 /// This function has the same error semantics as [`read_until`] and will
593 /// also return an error if the read bytes are not valid UTF-8. If an I/O
594 /// error is encountered then `buf` may contain some bytes already read in
595 /// the event that all data read so far was valid UTF-8.
596 ///
597 /// [`read_until`]: Self::read_until
598 #[cfg(feature = "alloc")]
599 fn read_line(&mut self, buf: &mut String) -> Result<usize> {
600 // Note that we are not calling the `.read_until` method here, but
601 // rather our hardcoded implementation. For more details as to why, see
602 // the comments in `read_to_end`.
603 append_to_string(buf, |b| read_until(self, b'\n', b))
604 }
605
606 /// Returns an iterator over the contents of this reader split on the byte
607 /// `byte`.
608 ///
609 /// The iterator returned from this function will return instances of
610 /// [`io::Result`]`<`[`Vec<u8>`]`>`. Each vector returned will *not* have
611 /// the delimiter byte at the end.
612 ///
613 /// This function will yield errors whenever [`read_until`] would have
614 /// also yielded an error.
615 ///
616 /// [`io::Result`]: type.Result.html
617 /// [`Vec<u8>`]: ...alloc/vec/struct.Vec.html
618 /// [`read_until`]: #method.read_until
619 #[cfg(feature = "alloc")]
620 fn split(self, byte: u8) -> Split<Self>
621 where
622 Self: Sized,
623 {
624 Split {
625 buf: self,
626 delim: byte,
627 }
628 }
629
630 /// Returns an iterator over the lines of this reader.
631 ///
632 /// The iterator returned from this function will yield instances of
633 /// [`io::Result`]`<`[`String`]`>`. Each string returned will *not* have a newline
634 /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end.
635 ///
636 /// [`io::Result`]: type.Result.html
637 /// [`String`]: ../string/struct.String.html
638 ///
639 /// # Errors
640 ///
641 /// Each line of the iterator has the same error semantics as [`BufRead::read_line`].
642 ///
643 /// [`BufRead::read_line`]: trait.BufRead.html#method.read_line
644 #[cfg(feature = "alloc")]
645 fn lines(self) -> Lines<Self>
646 where
647 Self: Sized,
648 {
649 Lines { buf: self }
650 }
651}
652
653/// Enumeration of possible methods to seek within an I/O object.
654///
655/// It is used by the [`Seek`] trait.
656///
657/// [`Seek`]: trait.Seek.html
658#[derive(Copy, PartialEq, Eq, Clone, Debug)]
659pub enum SeekFrom {
660 /// Sets the offset to the provided number of bytes.
661 Start(u64),
662
663 /// Sets the offset to the size of this object plus the specified number of
664 /// bytes.
665 ///
666 /// It is possible to seek beyond the end of an object, but it's an error to
667 /// seek before byte 0.
668 End(i64),
669
670 /// Sets the offset to the current position plus the specified number of
671 /// bytes.
672 ///
673 /// It is possible to seek beyond the end of an object, but it's an error to
674 /// seek before byte 0.
675 Current(i64),
676}
677
678/// Adaptor to chain together two readers.
679///
680/// This struct is generally created by calling [`chain`] on a reader.
681/// Please see the documentation of [`chain`] for more details.
682///
683/// [`chain`]: trait.Read.html#method.chain
684pub struct Chain<T, U> {
685 first: T,
686 second: U,
687 done_first: bool,
688}
689
690impl<T, U> Chain<T, U> {
691 /// Consumes the `Chain`, returning the wrapped readers.
692 pub fn into_inner(self) -> (T, U) {
693 (self.first, self.second)
694 }
695
696 /// Gets references to the underlying readers in this `Chain`.
697 pub fn get_ref(&self) -> (&T, &U) {
698 (&self.first, &self.second)
699 }
700
701 /// Gets mutable references to the underlying readers in this `Chain`.
702 ///
703 /// Care should be taken to avoid modifying the internal I/O state of the
704 /// underlying readers as doing so may corrupt the internal state of this
705 /// `Chain`.
706 pub fn get_mut(&mut self) -> (&mut T, &mut U) {
707 (&mut self.first, &mut self.second)
708 }
709}
710
711impl<T: fmt::Debug, U: fmt::Debug> fmt::Debug for Chain<T, U> {
712 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
713 f.debug_struct("Chain")
714 .field("t", &self.first)
715 .field("u", &self.second)
716 .finish()
717 }
718}
719
720impl<T: Read, U: Read> Read for Chain<T, U> {
721 fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
722 if !self.done_first {
723 match self.first.read(buf)? {
724 0 if !buf.is_empty() => self.done_first = true,
725 n => return Ok(n),
726 }
727 }
728 self.second.read(buf)
729 }
730}
731
732impl<T: BufRead, U: BufRead> BufRead for Chain<T, U> {
733 fn fill_buf(&mut self) -> Result<&[u8]> {
734 if !self.done_first {
735 match self.first.fill_buf()? {
736 &[] => self.done_first = true,
737 buf => return Ok(buf),
738 }
739 }
740 self.second.fill_buf()
741 }
742
743 fn consume(&mut self, amt: usize) {
744 if !self.done_first {
745 self.first.consume(amt)
746 } else {
747 self.second.consume(amt)
748 }
749 }
750}
751
752/// Reader adaptor which limits the bytes read from an underlying reader.
753///
754/// This struct is generally created by calling [`take`] on a reader.
755/// Please see the documentation of [`take`] for more details.
756///
757/// [`take`]: trait.Read.html#method.take
758#[derive(Debug)]
759pub struct Take<T> {
760 inner: T,
761 limit: u64,
762}
763
764impl<T> Take<T> {
765 /// Returns the number of bytes that can be read before this instance will
766 /// return EOF.
767 ///
768 /// # Note
769 ///
770 /// This instance may reach `EOF` after reading fewer bytes than indicated by
771 /// this method if the underlying [`Read`] instance reaches EOF.
772 ///
773 /// [`Read`]:trait.Read.html
774 pub fn limit(&self) -> u64 {
775 self.limit
776 }
777
778 /// Sets the number of bytes that can be read before this instance will
779 /// return EOF. This is the same as constructing a new `Take` instance, so
780 /// the amount of bytes read and the previous limit value don't matter when
781 /// calling this method.
782 pub fn set_limit(&mut self, limit: u64) {
783 self.limit = limit;
784 }
785
786 /// Consumes the `Take`, returning the wrapped reader.
787 pub fn into_inner(self) -> T {
788 self.inner
789 }
790
791 /// Gets a reference to the underlying reader.
792 pub fn get_ref(&self) -> &T {
793 &self.inner
794 }
795
796 /// Gets a mutable reference to the underlying reader.
797 ///
798 /// Care should be taken to avoid modifying the internal I/O state of the
799 /// underlying reader as doing so may corrupt the internal limit of this
800 /// `Take`.
801 pub fn get_mut(&mut self) -> &mut T {
802 &mut self.inner
803 }
804}
805
806impl<T: Read> Read for Take<T> {
807 fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
808 // Don't call into inner reader at all at EOF because it may still block
809 if self.limit == 0 {
810 return Ok(0);
811 }
812
813 let max = cmp::min(buf.len() as u64, self.limit) as usize;
814 let n = self.inner.read(&mut buf[..max])?;
815 self.limit -= n as u64;
816 Ok(n)
817 }
818
819 #[cfg(feature = "alloc")]
820 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
821 // Pass in a reservation_size closure that respects the current value
822 // of limit for each read. If we hit the read limit, this prevents the
823 // final zero-byte read from allocating again.
824 read_to_end_with_reservation(self, buf, |self_| cmp::min(self_.limit, 32) as usize)
825 }
826}
827
828impl<T: BufRead> BufRead for Take<T> {
829 fn fill_buf(&mut self) -> Result<&[u8]> {
830 // Don't call into inner reader at all at EOF because it may still block
831 if self.limit == 0 {
832 return Ok(&[]);
833 }
834
835 let buf = self.inner.fill_buf()?;
836 let cap = cmp::min(buf.len() as u64, self.limit) as usize;
837 Ok(&buf[..cap])
838 }
839
840 fn consume(&mut self, amt: usize) {
841 // Don't let callers reset the limit by passing an overlarge value
842 let amt = cmp::min(amt as u64, self.limit) as usize;
843 self.limit -= amt as u64;
844 self.inner.consume(amt);
845 }
846}
847
848/// An iterator over `u8` values of a reader.
849///
850/// This struct is generally created by calling [`bytes`] on a reader.
851/// Please see the documentation of [`bytes`] for more details.
852///
853/// [`bytes`]: trait.Read.html#method.bytes
854#[derive(Debug)]
855pub struct Bytes<R> {
856 inner: R,
857}
858
859impl<R: Read> Iterator for Bytes<R> {
860 type Item = Result<u8>;
861
862 fn next(&mut self) -> Option<Result<u8>> {
863 let mut byte = 0;
864 loop {
865 return match self.inner.read(slice::from_mut(&mut byte)) {
866 Ok(0) => None,
867 Ok(..) => Some(Ok(byte)),
868 Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
869 Err(e) => Some(Err(e)),
870 };
871 }
872 }
873}
874
875/// An iterator over the contents of an instance of `BufRead` split on a
876/// particular byte.
877///
878/// This struct is generally created by calling [`split`] on a `BufRead`.
879/// Please see the documentation of [`split`] for more details.
880///
881/// [`split`]: trait.BufRead.html#method.split
882#[cfg(feature = "alloc")]
883#[derive(Debug)]
884pub struct Split<B> {
885 buf: B,
886 delim: u8,
887}
888
889#[cfg(feature = "alloc")]
890impl<B: BufRead> Iterator for Split<B> {
891 type Item = Result<Vec<u8>>;
892
893 fn next(&mut self) -> Option<Result<Vec<u8>>> {
894 let mut buf = Vec::new();
895 match self.buf.read_until(self.delim, &mut buf) {
896 Ok(0) => None,
897 Ok(_n) => {
898 if buf[buf.len() - 1] == self.delim {
899 buf.pop();
900 }
901 Some(Ok(buf))
902 }
903 Err(e) => Some(Err(e)),
904 }
905 }
906}
907
908/// An iterator over the lines of an instance of `BufRead`.
909///
910/// This struct is generally created by calling [`lines`] on a `BufRead`.
911/// Please see the documentation of [`lines`] for more details.
912///
913/// [`lines`]: trait.BufRead.html#method.lines
914#[cfg(feature = "alloc")]
915#[derive(Debug)]
916pub struct Lines<B> {
917 buf: B,
918}
919
920#[cfg(feature = "alloc")]
921impl<B: BufRead> Iterator for Lines<B> {
922 type Item = Result<String>;
923
924 fn next(&mut self) -> Option<Result<String>> {
925 let mut buf = String::new();
926 match self.buf.read_line(&mut buf) {
927 Ok(0) => None,
928 Ok(_n) => {
929 if buf.ends_with('\n') {
930 buf.pop();
931 if buf.ends_with('\r') {
932 buf.pop();
933 }
934 }
935 Some(Ok(buf))
936 }
937 Err(e) => Some(Err(e)),
938 }
939 }
940}
941
942// This uses an adaptive system to extend the vector when it fills. We want to
943// avoid paying to allocate and zero a huge chunk of memory if the reader only
944// has 4 bytes while still making large reads if the reader does have a ton
945// of data to return. Simply tacking on an extra DEFAULT_BUF_SIZE space every
946// time is 4,500 times (!) slower than a default reservation size of 32 if the
947// reader has a very small amount of data to return.
948//
949// Because we're extending the buffer with uninitialized data for trusted
950// readers, we need to make sure to truncate that if any of this panics.
951#[cfg(feature = "alloc")]
952fn read_to_end<R: Read + ?Sized>(r: &mut R, buf: &mut Vec<u8>) -> Result<usize> {
953 read_to_end_with_reservation(r, buf, |_| 32)
954}
955
956#[cfg(feature = "alloc")]
957fn read_to_end_with_reservation<R, F>(
958 r: &mut R,
959 buf: &mut Vec<u8>,
960 mut reservation_size: F,
961) -> Result<usize>
962where
963 R: Read + ?Sized,
964 F: FnMut(&R) -> usize,
965{
966 let start_len = buf.len();
967 let mut g = Guard {
968 len: buf.len(),
969 buf,
970 };
971 let ret;
972 loop {
973 if g.len == g.buf.len() {
974 unsafe {
975 // FIXME(danielhenrymantilla): #42788
976 //
977 // - This creates a (mut) reference to a slice of
978 // _uninitialized_ integers, which is **undefined behavior**
979 //
980 // - Only the standard library gets to soundly "ignore" this,
981 // based on its privileged knowledge of unstable rustc
982 // internals;
983 g.buf.reserve(reservation_size(r));
984 let capacity = g.buf.capacity();
985 g.buf.set_len(capacity);
986 r.initializer().initialize(&mut g.buf[g.len..]);
987 }
988 }
989
990 match r.read(&mut g.buf[g.len..]) {
991 Ok(0) => {
992 ret = Ok(g.len - start_len);
993 break;
994 }
995 Ok(n) => g.len += n,
996 Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
997 Err(e) => {
998 ret = Err(e);
999 break;
1000 }
1001 }
1002 }
1003
1004 ret
1005}
1006
1007#[cfg(feature = "alloc")]
1008struct Guard<'a> {
1009 buf: &'a mut Vec<u8>,
1010 len: usize,
1011}
1012
1013#[cfg(feature = "alloc")]
1014impl Drop for Guard<'_> {
1015 fn drop(&mut self) {
1016 unsafe {
1017 self.buf.set_len(self.len);
1018 }
1019 }
1020}
1021
1022// A few methods below (read_to_string, read_line) will append data into a
1023// `String` buffer, but we need to be pretty careful when doing this. The
1024// implementation will just call `.as_mut_vec()` and then delegate to a
1025// byte-oriented reading method, but we must ensure that when returning we never
1026// leave `buf` in a state such that it contains invalid UTF-8 in its bounds.
1027//
1028// To this end, we use an RAII guard (to protect against panics) which updates
1029// the length of the string when it is dropped. This guard initially truncates
1030// the string to the prior length and only after we've validated that the
1031// new contents are valid UTF-8 do we allow it to set a longer length.
1032//
1033// The unsafety in this function is twofold:
1034//
1035// 1. We're looking at the raw bytes of `buf`, so we take on the burden of UTF-8
1036// checks.
1037// 2. We're passing a raw buffer to the function `f`, and it is expected that
1038// the function only *appends* bytes to the buffer. We'll get undefined
1039// behavior if existing bytes are overwritten to have non-UTF-8 data.
1040#[cfg(feature = "alloc")]
1041fn append_to_string<F>(buf: &mut String, f: F) -> Result<usize>
1042where
1043 F: FnOnce(&mut Vec<u8>) -> Result<usize>,
1044{
1045 unsafe {
1046 let mut g = Guard {
1047 len: buf.len(),
1048 buf: buf.as_mut_vec(),
1049 };
1050 let ret = f(g.buf);
1051 if str::from_utf8(&g.buf[g.len..]).is_err() {
1052 ret.and_then(|_| {
1053 Err(Error::new(
1054 ErrorKind::InvalidData,
1055 "stream did not contain valid UTF-8",
1056 ))
1057 })
1058 } else {
1059 g.len = g.buf.len();
1060 ret
1061 }
1062 }
1063}
1064
1065#[cfg(feature = "alloc")]
1066fn read_until<R: BufRead + ?Sized>(r: &mut R, delim: u8, buf: &mut Vec<u8>) -> Result<usize> {
1067 let mut read = 0;
1068 loop {
1069 let (done, used) = {
1070 let available = match r.fill_buf() {
1071 Ok(n) => n,
1072 Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
1073 Err(e) => return Err(e),
1074 };
1075 // This is intentionally simple. We may improve upon it in the future with a
1076 // proper `memchr` implementation if it proves to be necessary.
1077 match available.iter().position(|&c| c == delim) {
1078 Some(i) => {
1079 buf.extend_from_slice(&available[..=i]);
1080 (true, i + 1)
1081 }
1082 None => {
1083 buf.extend_from_slice(available);
1084 (false, available.len())
1085 }
1086 }
1087 };
1088 r.consume(used);
1089 read += used;
1090 if done || used == 0 {
1091 return Ok(read);
1092 }
1093 }
1094}
1095
1096// Create a shim which translates a Write to a fmt::Write and saves
1097// off I/O errors. instead of discarding them
1098pub(crate) struct WriteFmtAdaptor<'a, T: ?Sized + 'a> {
1099 inner: &'a mut T,
1100 error: Result<()>,
1101}
1102
1103impl<'a, T: ?Sized + 'a> WriteFmtAdaptor<'a, T> {
1104 pub(crate) fn new(inner: &'a mut T) -> Self {
1105 Self {
1106 inner,
1107 error: Ok(()),
1108 }
1109 }
1110}
1111
1112impl<T: Write + ?Sized> fmt::Write for WriteFmtAdaptor<'_, T> {
1113 fn write_str(&mut self, s: &str) -> fmt::Result {
1114 match self.inner.write_all(s.as_bytes()) {
1115 Ok(()) => Ok(()),
1116 Err(e) => {
1117 self.error = Err(e);
1118 Err(fmt::Error)
1119 }
1120 }
1121 }
1122}