Significantly simplify src/blake3.rs by using the new update_reader method

This commit is contained in:
bjorn3 2024-01-08 19:41:23 +01:00
parent 3d3edca3b7
commit 5fed46f97e
3 changed files with 9 additions and 33 deletions

7
Cargo.lock generated
View File

@ -212,16 +212,15 @@ dependencies = [
[[package]]
name = "blake3"
version = "1.4.1"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "199c42ab6972d92c9f8995f086273d25c42fc0f7b2a1fcefba465c1352d25ba5"
checksum = "0231f06152bf547e9c2b5194f247cd97aacf6dcd8b15d8e5ec0663f64580da87"
dependencies = [
"arrayref",
"arrayvec 0.7.4",
"cc",
"cfg-if 1.0.0",
"constant_time_eq 0.3.0",
"digest 0.10.7",
]
[[package]]
@ -1237,7 +1236,7 @@ dependencies = [
name = "redox_cookbook"
version = "0.1.0"
dependencies = [
"blake3 1.4.1",
"blake3 1.5.0",
"pbr",
"pkgar",
"pkgar-keys",

View File

@ -20,7 +20,7 @@ name = "cookbook"
path = "src/lib.rs"
[dependencies]
blake3 = "1"
blake3 = "1.5"
pbr = "1.0.2"
pkgar = "0.1.7"
pkgar-keys = "0.1.0"

View File

@ -1,30 +1,8 @@
use blake3::Hasher;
use std::{
fs,
io::{Read, Result},
path::Path,
time::Duration,
};
use std::{fs, io::Result, path::Path, time::Duration};
use crate::progress_bar::{ProgressBar, ProgressBarRead};
pub fn blake3<R: Read>(r: &mut R) -> Result<String> {
let mut hasher = Hasher::new();
let mut data = vec![0; 4 * 1024 * 1024];
loop {
let count = r.read(&mut data)?;
if count == 0 {
break;
}
hasher.update(&data[..count]);
}
let hash = hasher.finalize();
Ok(format!("{}", hash.to_hex()))
}
pub fn blake3_progress<P: AsRef<Path>>(path: P) -> Result<String> {
let len = fs::metadata(&path)?.len();
@ -35,12 +13,11 @@ pub fn blake3_progress<P: AsRef<Path>>(path: P) -> Result<String> {
pb.set_max_refresh_rate(Some(Duration::new(1, 0)));
pb.set_units(pbr::Units::Bytes);
let res = {
let mut pbr = ProgressBarRead::new(&mut pb, &mut f);
blake3(&mut pbr)
};
let mut pbr = ProgressBarRead::new(&mut pb, &mut f);
let hash = Hasher::new().update_reader(&mut pbr)?.finalize();
let res = format!("{}", hash.to_hex());
pb.finish_println("");
res
Ok(res)
}