Skip to content

Commit

Permalink
Add is_full API to all bounded collections (#887)
Browse files Browse the repository at this point in the history
* add `is_full` api to all bounded collections

* Update bounded-collections/src/weak_bounded_vec.rs

Co-authored-by: Bastian Köcher <[email protected]>

---------

Co-authored-by: Bastian Köcher <[email protected]>
  • Loading branch information
shawntabrizi and bkchr authored Dec 9, 2024
1 parent 8082691 commit d54ac84
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
17 changes: 17 additions & 0 deletions bounded-collections/src/bounded_btree_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ where
.collect::<Result<BTreeMap<_, _>, _>>()?,
))
}

/// Returns true if this map is full.
pub fn is_full(&self) -> bool {
self.len() >= Self::bound()
}
}

impl<K, V, S> Default for BoundedBTreeMap<K, V, S>
Expand Down Expand Up @@ -784,4 +789,16 @@ mod test {
}
}
}

#[test]
fn is_full_works() {
let mut bounded = boundedmap_from_keys::<u32, ConstU32<4>>(&[1, 2, 3]);
assert!(!bounded.is_full());
bounded.try_insert(0, ()).unwrap();
assert_eq!(*bounded, map_from_keys(&[1, 0, 2, 3]));

assert!(bounded.is_full());
assert!(bounded.try_insert(9, ()).is_err());
assert_eq!(*bounded, map_from_keys(&[1, 0, 2, 3]));
}
}
17 changes: 17 additions & 0 deletions bounded-collections/src/bounded_btree_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ where
{
self.0.take(value)
}

/// Returns true if this set is full.
pub fn is_full(&self) -> bool {
self.len() >= Self::bound()
}
}

impl<T, S> Default for BoundedBTreeSet<T, S>
Expand Down Expand Up @@ -587,6 +592,18 @@ mod test {
let _foo = Foo::default();
}

#[test]
fn is_full_works() {
let mut bounded = boundedset_from_keys::<u32, ConstU32<4>>(&[1, 2, 3]);
assert!(!bounded.is_full());
bounded.try_insert(0).unwrap();
assert_eq!(*bounded, set_from_keys(&[1, 0, 2, 3]));

assert!(bounded.is_full());
assert!(bounded.try_insert(9).is_err());
assert_eq!(*bounded, set_from_keys(&[1, 0, 2, 3]));
}

#[cfg(feature = "serde")]
mod serde {
use super::*;
Expand Down
14 changes: 13 additions & 1 deletion bounded-collections/src/bounded_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ impl<T, S: Get<u32>> BoundedVec<T, S> {
S::get() as usize
}

/// Returns true of this collection is full.
/// Returns true if this collection is full.
pub fn is_full(&self) -> bool {
self.len() >= Self::bound()
}
Expand Down Expand Up @@ -1368,4 +1368,16 @@ mod test {
}
let _foo = Foo { bar: 42, slice: BoundedSlice::truncate_from(&[0, 1][..]), map: BoundedVec::default() };
}

#[test]
fn is_full_works() {
let mut bounded: BoundedVec<u32, ConstU32<4>> = bounded_vec![1, 2, 3];
assert!(!bounded.is_full());
bounded.try_insert(1, 0).unwrap();
assert_eq!(*bounded, vec![1, 0, 2, 3]);

assert!(bounded.is_full());
assert!(bounded.try_insert(0, 9).is_err());
assert_eq!(*bounded, vec![1, 0, 2, 3]);
}
}
17 changes: 17 additions & 0 deletions bounded-collections/src/weak_bounded_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ impl<T, S: Get<u32>> WeakBoundedVec<T, S> {
Err(())
}
}

/// Returns true if this collection is full.
pub fn is_full(&self) -> bool {
self.len() >= Self::bound()
}
}

impl<T, S> Default for WeakBoundedVec<T, S> {
Expand Down Expand Up @@ -517,4 +522,16 @@ mod test {
let w = WeakBoundedVec::<u32, ConstU32<4>>::decode(&mut &v.encode()[..]).unwrap();
assert_eq!(v, *w);
}

#[test]
fn is_full_works() {
let mut bounded: WeakBoundedVec<u32, ConstU32<4>> = vec![1, 2, 3].try_into().unwrap();
assert!(!bounded.is_full());
bounded.try_insert(1, 0).unwrap();
assert_eq!(*bounded, vec![1, 0, 2, 3]);

assert!(bounded.is_full());
assert!(bounded.try_insert(0, 9).is_err());
assert_eq!(*bounded, vec![1, 0, 2, 3]);
}
}

0 comments on commit d54ac84

Please sign in to comment.