From: Ian Jackson Date: Mon, 3 May 2021 21:42:04 +0000 (+0100) Subject: provide DigestWrite::of X-Git-Tag: otter-0.6.0~421 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=95c85c127a8c646591069df8ca92247529f5afe6;p=otter.git provide DigestWrite::of Signed-off-by: Ian Jackson --- diff --git a/src/bin/otter.rs b/src/bin/otter.rs index b8553adf..f9c9457f 100644 --- a/src/bin/otter.rs +++ b/src/bin/otter.rs @@ -1341,11 +1341,8 @@ mod upload_bundle { .with_context(|| args.bundle_file.clone()) .context("open bundle file")?; let mut f = BufReader::new(f); - let hash = { - let mut dw = bundles::DigestWrite::sink(); - io::copy(&mut f, &mut dw).context("read bundle file (for hash)")?; - dw.finish().0 - }; + let hash = bundles::DigestWrite::of(&mut f) + .context("read bundle file (for hash)")?; let kind = bundles::Kind::only(); f.rewind().context("rewind bundle file")?; let cmd = MC::UploadBundle { diff --git a/src/utils.rs b/src/utils.rs index 438984ff..00f3c836 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -584,6 +584,13 @@ impl DigestWrite { } impl DigestWrite { pub fn sink() -> Self { DigestWrite::new(io::sink()) } + + #[throws(io::Error)] + pub fn of(r: &mut R) -> digest::Output where R: Read { + let mut dw = DigestWrite::::sink(); + io::copy(r, &mut dw)?; + dw.finish().0 + } } impl Write for DigestWrite {