pub trait CollectionBase {
type Item;
// Required methods
fn item_count(&self) -> usize;
fn push(&mut self, elem: Self::Item);
fn clear(&mut self);
// Provided method
fn is_empty(&self) -> bool { ... }
}Expand description
Trait implemented by all collection types.
§Collection contract
item_count() returns the number of values of Item held by the
collection. For UTF-8 strings this counts chars and is O(n); it is O(1)
for other collections. is_empty() is O(1) for every implementation.
push() appends to sequences and linked lists, adds to a BinaryHeap,
inserts into sets and maps, and appends a char to strings. Duplicate set
values do not increase the item count; inserting an existing map key
replaces its value.
push() is infallible: growable collections abort or panic when the
allocation fails, and fixed-capacity collections panic when they are full.
Use CollectionCapacity::try_push to get the item back instead.
No length method is defined by this trait, so a concrete type’s inherent
len() remains unambiguous. Use CollectionBytes when the byte length is
needed.
use collections::CollectionBase;
fn length<C: CollectionBase>(collection: &C) -> usize {
collection.len()
}Required Associated Types§
Required Methods§
Sourcefn item_count(&self) -> usize
fn item_count(&self) -> usize
Returns the number of Items in the collection.
This is O(n) for UTF-8 strings, where it counts chars, and O(1) for
all other collection types.
Provided Methods§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl CollectionBase for AsciiString
Available on crate feature ascii only.
impl CollectionBase for AsciiString
ascii only.Source§impl CollectionBase for String
Available on crate feature alloc only.
impl CollectionBase for String
alloc only.Source§impl<K: Eq + Hash, V, S: BuildHasher, A: Allocator> CollectionBase for HashMap<K, V, S, A>
Available on crate feature hashbrown only.
impl<K: Eq + Hash, V, S: BuildHasher, A: Allocator> CollectionBase for HashMap<K, V, S, A>
hashbrown only.Source§impl<K: Eq + Hash, V, S: BuildHasher, const N: usize> CollectionBase for IndexMap<K, V, S, N>
Available on crate feature heapless only.
impl<K: Eq + Hash, V, S: BuildHasher, const N: usize> CollectionBase for IndexMap<K, V, S, N>
heapless only.Source§impl<K: Eq + Hash, V, S: BuildHasher> CollectionBase for HashMap<K, V, S>
Available on crate feature std only.
impl<K: Eq + Hash, V, S: BuildHasher> CollectionBase for HashMap<K, V, S>
std only.Source§impl<K: Eq + Hash, V, S: BuildHasher> CollectionBase for IndexMap<K, V, S>
Available on crate feature indexmap only.
impl<K: Eq + Hash, V, S: BuildHasher> CollectionBase for IndexMap<K, V, S>
indexmap only.Source§impl<K: Eq, V, const N: usize> CollectionBase for LinearMap<K, V, N>
Available on crate feature heapless only.
impl<K: Eq, V, const N: usize> CollectionBase for LinearMap<K, V, N>
heapless only.Source§impl<K: Ord, V> CollectionBase for BTreeMap<K, V>
Available on crate feature alloc only.
impl<K: Ord, V> CollectionBase for BTreeMap<K, V>
alloc only.Source§impl<LenT: LenType, const N: usize> CollectionBase for String<N, LenT>
Available on crate feature heapless only.
impl<LenT: LenType, const N: usize> CollectionBase for String<N, LenT>
heapless only.Source§impl<T, A: Allocator> CollectionBase for Vec<T, A>
Available on crate feature alloc only.
impl<T, A: Allocator> CollectionBase for Vec<T, A>
alloc only.Source§impl<T, LenT: LenType, const N: usize> CollectionBase for Vec<T, N, LenT>
Available on crate feature heapless only.
impl<T, LenT: LenType, const N: usize> CollectionBase for Vec<T, N, LenT>
heapless only.Source§impl<T, const N: usize> CollectionBase for Deque<T, N>
Available on crate feature heapless only.
impl<T, const N: usize> CollectionBase for Deque<T, N>
heapless only.Source§impl<T: Eq + Hash, S: BuildHasher, A: Allocator> CollectionBase for HashSet<T, S, A>
Available on crate feature hashbrown only.
impl<T: Eq + Hash, S: BuildHasher, A: Allocator> CollectionBase for HashSet<T, S, A>
hashbrown only.Source§impl<T: Eq + Hash, S: BuildHasher, const N: usize> CollectionBase for IndexSet<T, S, N>
Available on crate feature heapless only.
impl<T: Eq + Hash, S: BuildHasher, const N: usize> CollectionBase for IndexSet<T, S, N>
heapless only.Source§impl<T: Eq + Hash, S: BuildHasher> CollectionBase for HashSet<T, S>
Available on crate feature std only.
impl<T: Eq + Hash, S: BuildHasher> CollectionBase for HashSet<T, S>
std only.Source§impl<T: Eq + Hash, S: BuildHasher> CollectionBase for IndexSet<T, S>
Available on crate feature indexmap only.
impl<T: Eq + Hash, S: BuildHasher> CollectionBase for IndexSet<T, S>
indexmap only.Source§impl<T: Ord, K: Kind, const N: usize> CollectionBase for BinaryHeap<T, K, N>
Available on crate feature heapless only.
impl<T: Ord, K: Kind, const N: usize> CollectionBase for BinaryHeap<T, K, N>
heapless only.Source§impl<T: Ord> CollectionBase for BTreeSet<T>
Available on crate feature alloc only.
impl<T: Ord> CollectionBase for BTreeSet<T>
alloc only.Source§impl<T: Ord> CollectionBase for BinaryHeap<T>
Available on crate feature alloc only.
impl<T: Ord> CollectionBase for BinaryHeap<T>
alloc only.Source§impl<T> CollectionBase for LinkedList<T>
Available on crate feature alloc only.
impl<T> CollectionBase for LinkedList<T>
alloc only.Source§impl<T> CollectionBase for VecDeque<T>
Available on crate feature alloc only.
impl<T> CollectionBase for VecDeque<T>
alloc only.