Skip to content

Instantly share code, notes, and snippets.

@dholth
Created April 4, 2022 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dholth/00c12de80df92c100adeb7a20c12e520 to your computer and use it in GitHub Desktop.
Save dholth/00c12de80df92c100adeb7a20c12e520 to your computer and use it in GitHub Desktop.
compute digest as a side effect of reading file
// read and digest at the same time
pub struct DigestReader<R> {
pub digest: Blake2b<consts::U32>,
reader: R,
}
impl<R: Read> DigestReader<R> {
/// Construct a `DigestReader` reader from an existing reader
pub fn new(r: R) -> DigestReader<R> {
DigestReader {
digest: Blake2b::<consts::U32>::new(),
reader: r,
}
}
}
impl<R: Read> Read for DigestReader<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let num = self.reader.read(buf)?;
self.digest.update(&buf[..num]);
Ok(num)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment