From a9d9d51f32e9addc905cdb2b4eec03a149506f0e Mon Sep 17 00:00:00 2001 From: "Jorge C. Leitao" Date: Wed, 29 Sep 2021 18:01:51 +0000 Subject: [PATCH] Added `MutableBitmap::shrink_to_fit` --- src/bitmap/mutable.rs | 6 ++++++ tests/it/bitmap/mutable.rs | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/src/bitmap/mutable.rs b/src/bitmap/mutable.rs index b2be349c9f8..798ac406859 100644 --- a/src/bitmap/mutable.rs +++ b/src/bitmap/mutable.rs @@ -208,6 +208,12 @@ impl MutableBitmap { pub fn set(&mut self, index: usize, value: bool) { set_bit(&mut self.buffer.as_mut_slice(), index, value) } + + /// Shrinks the capacity of the [`MutableBitmap`] to fit its current length. + /// When the feature `cache_aligned`, the new capacity will be a multiple of 64 bytes. + pub fn shrink_to_fit(&mut self) { + self.buffer.shrink_to_fit(); + } } impl MutableBitmap { diff --git a/tests/it/bitmap/mutable.rs b/tests/it/bitmap/mutable.rs index ab6a7d15789..858a2fb5883 100644 --- a/tests/it/bitmap/mutable.rs +++ b/tests/it/bitmap/mutable.rs @@ -329,3 +329,11 @@ fn extend_bitmap_other() { ]) ); } + +#[test] +fn shrink_to_fit() { + let mut a = MutableBitmap::with_capacity(1025); + a.push(false); + a.shrink_to_fit(); + assert!(a.capacity() < 1025); +}