Skip to content

Commit

Permalink
make update_reader/mmap/mmap_rayon return self
Browse files Browse the repository at this point in the history
This makes them consistent with how the existing update() and
update_rayon() methods work, with the difference being that it's it's
io::Result<&mut Self> instead of just &mut Self.
  • Loading branch information
oconnor663 committed Sep 17, 2023
1 parent cb32f0b commit b754033
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
16 changes: 10 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1346,8 +1346,9 @@ impl Hasher {
/// # }
/// ```
#[cfg(feature = "std")]
pub fn update_reader(&mut self, reader: impl std::io::Read) -> std::io::Result<u64> {
io::copy_wide(reader, self)
pub fn update_reader(&mut self, reader: impl std::io::Read) -> std::io::Result<&mut Self> {
io::copy_wide(reader, self)?;
Ok(self)
}

/// As [`update`](Hasher::update), but using Rayon-based multithreading
Expand Down Expand Up @@ -1417,14 +1418,14 @@ impl Hasher {
/// # }
/// ```
#[cfg(feature = "mmap")]
pub fn update_mmap(&mut self, path: impl AsRef<std::path::Path>) -> std::io::Result<()> {
pub fn update_mmap(&mut self, path: impl AsRef<std::path::Path>) -> std::io::Result<&mut Self> {
let file = std::fs::File::open(path.as_ref())?;
if let Some(mmap) = io::maybe_mmap_file(&file)? {
self.update(&mmap);
} else {
io::copy_wide(&file, self)?;
}
Ok(())
Ok(self)
}

/// As [`update_rayon`](Hasher::update_rayon), but reading the contents of a file using
Expand Down Expand Up @@ -1469,14 +1470,17 @@ impl Hasher {
/// ```
#[cfg(feature = "mmap")]
#[cfg(feature = "rayon")]
pub fn update_mmap_rayon(&mut self, path: impl AsRef<std::path::Path>) -> std::io::Result<()> {
pub fn update_mmap_rayon(
&mut self,
path: impl AsRef<std::path::Path>,
) -> std::io::Result<&mut Self> {
let file = std::fs::File::open(path.as_ref())?;
if let Some(mmap) = io::maybe_mmap_file(&file)? {
self.update_rayon(&mmap);
} else {
io::copy_wide(&file, self)?;
}
Ok(())
Ok(self)
}
}

Expand Down
27 changes: 18 additions & 9 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,9 +701,12 @@ fn test_update_reader() -> Result<(), std::io::Error> {
let mut tempfile = tempfile::NamedTempFile::new()?;
tempfile.write_all(&input)?;
tempfile.flush()?;
let mut hasher = crate::Hasher::new();
hasher.update_reader(std::fs::File::open(tempfile.path())?)?;
assert_eq!(hasher.finalize(), crate::hash(&input));
assert_eq!(
crate::Hasher::new()
.update_reader(std::fs::File::open(tempfile.path())?)?
.finalize(),
crate::hash(&input),
);
Ok(())
}

Expand Down Expand Up @@ -755,9 +758,12 @@ fn test_mmap() -> Result<(), std::io::Error> {
let mut tempfile = tempfile::NamedTempFile::new()?;
tempfile.write_all(&input)?;
tempfile.flush()?;
let mut hasher = crate::Hasher::new();
hasher.update_mmap(tempfile.path())?;
assert_eq!(hasher.finalize(), crate::hash(&input));
assert_eq!(
crate::Hasher::new()
.update_mmap(tempfile.path())?
.finalize(),
crate::hash(&input),
);
Ok(())
}

Expand Down Expand Up @@ -792,8 +798,11 @@ fn test_mmap_rayon() -> Result<(), std::io::Error> {
let mut tempfile = tempfile::NamedTempFile::new()?;
tempfile.write_all(&input)?;
tempfile.flush()?;
let mut hasher = crate::Hasher::new();
hasher.update_mmap_rayon(tempfile.path())?;
assert_eq!(hasher.finalize(), crate::hash(&input));
assert_eq!(
crate::Hasher::new()
.update_mmap_rayon(tempfile.path())?
.finalize(),
crate::hash(&input),
);
Ok(())
}

0 comments on commit b754033

Please sign in to comment.