diff options
-rw-r--r-- | ripple/fossil/src/chunker/mod.rs | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/ripple/fossil/src/chunker/mod.rs b/ripple/fossil/src/chunker/mod.rs index 59b3367..0e1bdc9 100644 --- a/ripple/fossil/src/chunker/mod.rs +++ b/ripple/fossil/src/chunker/mod.rs @@ -42,17 +42,21 @@ impl<'a> Iterator for Chunker<'a> { return None; } - if self.buffer.len() <= MIN_CHUNK_SIZE { - return Some(mem::take(&mut self.buffer)); - } + let max_len = MAX_CHUNK_SIZE.min(self.buffer.len()); + let bytes = match self.buffer.get(MIN_CHUNK_SIZE..max_len) { + None | Some(&[]) => { + return Some(mem::take(&mut self.buffer)); + } + Some(bytes) => bytes, + }; - let bytes = self.buffer.iter().take(MAX_CHUNK_SIZE).skip(MIN_CHUNK_SIZE); let mut hasher = unsafe { // SAFETY: `self.buffer.len > MIN_CHUNK_SIZE`, so this is in bounds buz::Rolling::<WINDOW_SIZE>::from_slice_unchecked( self.buffer.get_unchecked(..MIN_CHUNK_SIZE), ) }; + for byte in bytes { let buz::Hash(x) = hasher.sum(); if x % DISCRIMINATOR == DISCRIMINATOR.wrapping_sub(1) { @@ -70,9 +74,8 @@ impl<'a> Iterator for Chunker<'a> { } Some(unsafe { - // SAFETY: `idx` is clamped to `self.buffer.len()` - let idx = MAX_CHUNK_SIZE.min(self.buffer.len()); - self.cut(idx) + // SAFETY: `max_len` is clamped to `self.buffer.len()` + self.cut(max_len) }) } |