Skip to content

Commit

Permalink
feat(io): in the Read trait, read_exact is now auto-implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Oakchris1955 committed Jul 27, 2024
1 parent 4ac6a95 commit f9ca087
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/io.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This module contains all the IO-related objects of the crate

use crate::error::IOError;
use crate::error::{IOError, IOErrorKind};

/// With `use prelude::*`, all IO-related traits are automatically imported
pub mod prelude {
Expand All @@ -27,7 +27,24 @@ pub trait Read: IOBase {
/// Blocks until enough bytes could be read
///
/// Returns an error if EOF is met.
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Self::Error>;
fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<(), Self::Error> {
while !buf.is_empty() {
match self.read(buf) {
Ok(0) => break,
Ok(n) => buf = &mut buf[n..],
Err(ref e) if e.kind().is_interrupted() => {}
Err(e) => return Err(e),
}
}
if !buf.is_empty() {
Err(Self::Error::new(
<<Self::Error as IOError>::Kind as IOErrorKind>::new_unexpected_eof(),
"failed to fill whole buffer",
))
} else {
Ok(())
}
}
}

/// A simplified version of [`std::io::Write`] for use within a `no_std` context
Expand Down

0 comments on commit f9ca087

Please sign in to comment.