chiark / gitweb /
provide DigestWrite::of
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 3 May 2021 21:42:04 +0000 (22:42 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 3 May 2021 21:42:04 +0000 (22:42 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/bin/otter.rs
src/utils.rs

index b8553adf2ce174bfd0a7c0f35768bfe4ae0da3e0..f9c9457fbcd22adc16877aa0ef8859770d502c9d 100644 (file)
@@ -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 {
index 438984ff856e81a7c22aa100b41799318e637488..00f3c836bb6b1c46ecca9131af0f1800b75f5622 100644 (file)
@@ -584,6 +584,13 @@ impl<D: Digest, W: Write> DigestWrite<D, W> {
 }
 impl<D: Digest> DigestWrite<D, io::Sink> {
   pub fn sink() -> Self { DigestWrite::new(io::sink()) }
+
+  #[throws(io::Error)]
+  pub fn of<R>(r: &mut R) -> digest::Output<D> where R: Read {
+    let mut dw = DigestWrite::<D,_>::sink();
+    io::copy(r, &mut dw)?;
+    dw.finish().0
+  }
 }
 
 impl<D: Digest, W: Write> Write for DigestWrite<D, W> {