Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add is_full API to all bounded collections #887

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]);
}
}
Loading