diff --git a/src/bitmap/immutable.rs b/src/bitmap/immutable.rs index 45d8e89e4e5..b63310b07cb 100644 --- a/src/bitmap/immutable.rs +++ b/src/bitmap/immutable.rs @@ -239,7 +239,12 @@ impl Bitmap { pub fn make_mut(self) -> MutableBitmap { match self.into_mut() { Either::Left(data) => { - MutableBitmap::from_vec(data.bytes.as_ref().to_vec(), data.length) + if data.offset > 0 { + // we have to recreate the bytes because a MutableBitmap does not have an `offset`. + data.iter().collect() + } else { + MutableBitmap::from_vec(data.bytes.as_ref().to_vec(), data.length) + } } Either::Right(data) => data, } diff --git a/src/bitmap/mutable.rs b/src/bitmap/mutable.rs index 766dd8ad160..d490a74eaca 100644 --- a/src/bitmap/mutable.rs +++ b/src/bitmap/mutable.rs @@ -313,7 +313,7 @@ impl MutableBitmap { /// Returns an iterator over mutable slices, [`BitChunksExactMut`] pub(crate) fn bitchunks_exact_mut(&mut self) -> BitChunksExactMut { - BitChunksExactMut::new(&mut self.buffer) + BitChunksExactMut::new(&mut self.buffer, self.length) } } diff --git a/src/bitmap/utils/chunks_exact_mut.rs b/src/bitmap/utils/chunks_exact_mut.rs index 023687442d8..afc55ee093d 100644 --- a/src/bitmap/utils/chunks_exact_mut.rs +++ b/src/bitmap/utils/chunks_exact_mut.rs @@ -16,13 +16,12 @@ pub struct BitChunksExactMut<'a, T: BitChunk> { impl<'a, T: BitChunk> BitChunksExactMut<'a, T> { /// Returns a new [`BitChunksExactMut`] #[inline] - pub fn new(bitmap: &'a mut [u8]) -> Self { + pub fn new(bitmap: &'a mut [u8], length: usize) -> Self { let size_of = std::mem::size_of::(); - let length = bitmap.len(); - let split = (length / size_of) * size_of; + let split = (length / 8 / size_of) * size_of; let (chunks, remainder) = bitmap.split_at_mut(split); - let remainder_len = (length - chunks.len()) * 8; + let remainder_len = length - chunks.len() * 8; let chunks = chunks.chunks_exact_mut(size_of); Self {