diff --git a/bounded-collections/src/bounded_btree_map.rs b/bounded-collections/src/bounded_btree_map.rs index 6a441941..574f074f 100644 --- a/bounded-collections/src/bounded_btree_map.rs +++ b/bounded-collections/src/bounded_btree_map.rs @@ -271,6 +271,11 @@ where .collect::, _>>()?, )) } + + /// Returns true if this map is full. + pub fn is_full(&self) -> bool { + self.len() >= Self::bound() + } } impl Default for BoundedBTreeMap @@ -784,4 +789,16 @@ mod test { } } } + + #[test] + fn is_full_works() { + let mut bounded = boundedmap_from_keys::>(&[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])); + } } diff --git a/bounded-collections/src/bounded_btree_set.rs b/bounded-collections/src/bounded_btree_set.rs index e651c862..0942f34e 100644 --- a/bounded-collections/src/bounded_btree_set.rs +++ b/bounded-collections/src/bounded_btree_set.rs @@ -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 Default for BoundedBTreeSet @@ -587,6 +592,18 @@ mod test { let _foo = Foo::default(); } + #[test] + fn is_full_works() { + let mut bounded = boundedset_from_keys::>(&[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::*; diff --git a/bounded-collections/src/bounded_vec.rs b/bounded-collections/src/bounded_vec.rs index 4d56971e..1c3a5b34 100644 --- a/bounded-collections/src/bounded_vec.rs +++ b/bounded-collections/src/bounded_vec.rs @@ -437,7 +437,7 @@ impl> BoundedVec { 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() } @@ -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> = 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]); + } } diff --git a/bounded-collections/src/weak_bounded_vec.rs b/bounded-collections/src/weak_bounded_vec.rs index de5fa712..b6f0846e 100644 --- a/bounded-collections/src/weak_bounded_vec.rs +++ b/bounded-collections/src/weak_bounded_vec.rs @@ -225,6 +225,11 @@ impl> WeakBoundedVec { Err(()) } } + + /// Returns true if this collection is full. + pub fn is_full(&self) -> bool { + self.len() >= Self::bound() + } } impl Default for WeakBoundedVec { @@ -517,4 +522,16 @@ mod test { let w = WeakBoundedVec::>::decode(&mut &v.encode()[..]).unwrap(); assert_eq!(v, *w); } + + #[test] + fn is_full_works() { + let mut bounded: WeakBoundedVec> = 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]); + } }