diff options
author | edef <edef@unfathomable.blue> | 2022-05-02 01:43:33 +0000 |
---|---|---|
committer | edef <edef@unfathomable.blue> | 2022-05-02 01:43:33 +0000 |
commit | 71c71783d4efa7f44af910daf015e6972f069472 (patch) | |
tree | 8738ba775b3881600052ef50bf2ee730883be576 /ripple/fossil/src/chunker | |
parent | b27d8f87d4689aea63834b59629cccee1410b859 (diff) | |
download | unf-legacy-71c71783d4efa7f44af910daf015e6972f069472.tar.zst |
ripple/fossil/chunker: factor out Chunker::cut
Change-Id: I4fed55703cd02833f377ed0bbc659f3fcfdb949f
Diffstat (limited to 'ripple/fossil/src/chunker')
-rw-r--r-- | ripple/fossil/src/chunker/mod.rs | 36 |
1 files changed, 18 insertions, 18 deletions
diff --git a/ripple/fossil/src/chunker/mod.rs b/ripple/fossil/src/chunker/mod.rs index 9172a15..16e81a2 100644 --- a/ripple/fossil/src/chunker/mod.rs +++ b/ripple/fossil/src/chunker/mod.rs @@ -25,6 +25,16 @@ impl<'a> Chunker<'a> { pub fn from(buffer: &'a [u8]) -> Chunker<'a> { Chunker { buffer } } + + /// SAFETY: `idx` must be an in-bounds index for `self.buffer` + unsafe fn cut(&mut self, idx: usize) -> &'a [u8] { + let ret; + (ret, self.buffer) = ( + self.buffer.get_unchecked(..idx), + self.buffer.get_unchecked(idx..), + ); + ret + } } impl<'a> Iterator for Chunker<'a> { @@ -39,38 +49,28 @@ impl<'a> Iterator for Chunker<'a> { } let bytes = self.buffer.iter().take(MAX_CHUNK_SIZE).skip(MIN_CHUNK_SIZE); - let mut hasher = buz::Rolling::<WINDOW_SIZE>::from_slice(&self.buffer[..MIN_CHUNK_SIZE]); - let chunk; for byte in bytes { let buz::Hash(x) = hasher.sum(); if x % DISCRIMINATOR == DISCRIMINATOR.wrapping_sub(1) { // split point - (chunk, self.buffer) = unsafe { + return Some(unsafe { // SAFETY: `byte` is in bounds of `self.buffer`, so // computing `idx` is safe, and `idx` is in bounds let origin = self.buffer.as_ptr(); let ptr = byte as *const u8; let idx = ptr.offset_from(origin) as usize; - ( - self.buffer.get_unchecked(..idx), - self.buffer.get_unchecked(idx..), - ) - }; - return Some(chunk); + self.cut(idx) + }); } hasher.push(*byte); } - (chunk, self.buffer) = unsafe { - // SAFETY: `mid` is clamped to `self.buffer.len()` - let mid = MAX_CHUNK_SIZE.min(self.buffer.len()); - ( - self.buffer.get_unchecked(..mid), - self.buffer.get_unchecked(mid..), - ) - }; - Some(chunk) + Some(unsafe { + // SAFETY: `idx` is clamped to `self.buffer.len()` + let idx = MAX_CHUNK_SIZE.min(self.buffer.len()); + self.cut(idx) + }) } fn size_hint(&self) -> (usize, Option<usize>) { |