Skip to content

Commit

Permalink
Add SmallBitVec::last(&self) -> Option<bool>
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed May 15, 2019
1 parent 50db64b commit bc3a3b1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "smallbitvec"
version = "2.3.0"
version = "2.4.0"
authors = ["Matt Brubeck <mbrubeck@limpet.net>"]
license = "MIT / Apache-2.0"
description = "A bit vector optimized for size and inline storage"
Expand Down
20 changes: 11 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,12 @@ impl SmallBitVec {
}
}

/// Get the last bit in this bit vector.
#[inline]
pub fn last(&self) -> Option<bool> {
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 {
Expand Down Expand Up @@ -357,15 +363,11 @@ impl SmallBitVec {
/// ```
#[inline]
pub fn pop(&mut self) -> Option<bool> {
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.
Expand Down

0 comments on commit bc3a3b1

Please sign in to comment.