diff options
| author | Jack O'Connor <[email protected]> | 2023-09-16 19:17:58 -0700 |
|---|---|---|
| committer | Jack O'Connor <[email protected]> | 2023-09-16 19:22:36 -0700 |
| commit | b754033a2187c509ee1b29facea31ae13d122904 (patch) | |
| tree | 626600ef973df12cbbdb9809bc3aa66de0912f51 | |
| parent | cb32f0bd1450991d86a8ceb3716fd591382cc507 (diff) | |
make update_reader/mmap/mmap_rayon return self
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.
| -rw-r--r-- | src/lib.rs | 16 | ||||
| -rw-r--r-- | src/test.rs | 27 |
2 files changed, 28 insertions, 15 deletions
@@ -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 @@ -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 @@ -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) } } diff --git a/src/test.rs b/src/test.rs index a319930..c98192f 100644 --- a/src/test.rs +++ b/src/test.rs @@ -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(()) } @@ -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(()) } @@ -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(()) } |
