Created
April 4, 2022 14:18
-
-
Save dholth/00c12de80df92c100adeb7a20c12e520 to your computer and use it in GitHub Desktop.
compute digest as a side effect of reading file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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