diff --git a/Cargo.toml b/Cargo.toml index 2fb38dd..6ddaa57 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "smallbitvec" -version = "2.3.0" +version = "2.4.0" authors = ["Matt Brubeck "] license = "MIT / Apache-2.0" description = "A bit vector optimized for size and inline storage" diff --git a/src/lib.rs b/src/lib.rs index 0223934..f3bcb51 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -279,6 +279,12 @@ impl SmallBitVec { } } + /// Get the last bit in this bit vector. + #[inline] + pub fn last(&self) -> Option { + self.len().checked_sub(1).map(|n| unsafe { self.get_unchecked(n) }) + } + /// Get the nth bit in this bit vector, without bounds checks. #[inline] pub unsafe fn get_unchecked(&self, n: usize) -> bool { @@ -357,15 +363,11 @@ impl SmallBitVec { /// ``` #[inline] pub fn pop(&mut self) -> Option { - let old_len = self.len(); - if old_len == 0 { - return None; - } - unsafe { - let val = self.get_unchecked(old_len - 1); - self.set_len(old_len - 1); - Some(val) - } + self.len().checked_sub(1).map(|last| unsafe { + let val = self.get_unchecked(last); + self.set_len(last); + val + }) } /// Remove and return the bit at index `idx`, shifting all later bits toward the front.