Skip to main content

CollectionBase

Trait CollectionBase 

Source
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§

Source

type Item

Item type contained within the collection.

Required Methods§

Source

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.

Source

fn push(&mut self, elem: Self::Item)

Appends or inserts an element into the collection.

§Panics

Panics if a fixed-capacity collection is full. Growable collections panic or abort if the required memory cannot be allocated.

Source

fn clear(&mut self)

Removes all elements from the collection.

Provided Methods§

Source

fn is_empty(&self) -> bool

Detects whether a collection is empty. This is O(1) for every implementation.

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.
Source§

type Item = AsciiChar

Source§

fn item_count(&self) -> usize

Source§

fn push(&mut self, elem: Self::Item)

Source§

fn clear(&mut self)

Source§

impl CollectionBase for String

Available on crate feature alloc only.
Source§

type Item = char

Source§

fn item_count(&self) -> usize

Source§

fn is_empty(&self) -> bool

Source§

fn push(&mut self, elem: Self::Item)

Source§

fn clear(&mut self)

Source§

impl<K: Eq + Hash, V, S: BuildHasher, A: Allocator> CollectionBase for HashMap<K, V, S, A>

Available on crate feature hashbrown only.
Source§

type Item = (K, V)

Source§

fn item_count(&self) -> usize

Source§

fn push(&mut self, elem: Self::Item)

Source§

fn clear(&mut self)

Source§

impl<K: Eq + Hash, V, S: BuildHasher, const N: usize> CollectionBase for IndexMap<K, V, S, N>

Available on crate feature heapless only.
Source§

type Item = (K, V)

Source§

fn item_count(&self) -> usize

Source§

fn push(&mut self, elem: Self::Item)

Source§

fn clear(&mut self)

Source§

impl<K: Eq + Hash, V, S: BuildHasher> CollectionBase for HashMap<K, V, S>

Available on crate feature std only.
Source§

type Item = (K, V)

Source§

fn item_count(&self) -> usize

Source§

fn push(&mut self, elem: Self::Item)

Source§

fn clear(&mut self)

Source§

impl<K: Eq + Hash, V, S: BuildHasher> CollectionBase for IndexMap<K, V, S>

Available on crate feature indexmap only.
Source§

type Item = (K, V)

Source§

fn item_count(&self) -> usize

Source§

fn push(&mut self, elem: Self::Item)

Source§

fn clear(&mut self)

Source§

impl<K: Eq, V, const N: usize> CollectionBase for LinearMap<K, V, N>

Available on crate feature heapless only.
Source§

type Item = (K, V)

Source§

fn item_count(&self) -> usize

Source§

fn push(&mut self, elem: Self::Item)

Source§

fn clear(&mut self)

Source§

impl<K: Ord, V> CollectionBase for BTreeMap<K, V>

Available on crate feature alloc only.
Source§

type Item = (K, V)

Source§

fn item_count(&self) -> usize

Source§

fn push(&mut self, elem: Self::Item)

Source§

fn clear(&mut self)

Source§

impl<LenT: LenType, const N: usize> CollectionBase for String<N, LenT>

Available on crate feature heapless only.
Source§

type Item = char

Source§

fn item_count(&self) -> usize

Source§

fn is_empty(&self) -> bool

Source§

fn push(&mut self, elem: Self::Item)

Source§

fn clear(&mut self)

Source§

impl<T, A: Allocator> CollectionBase for Vec<T, A>

Available on crate feature alloc only.
Source§

type Item = T

Source§

fn item_count(&self) -> usize

Source§

fn push(&mut self, elem: Self::Item)

Source§

fn clear(&mut self)

Source§

impl<T, LenT: LenType, const N: usize> CollectionBase for Vec<T, N, LenT>

Available on crate feature heapless only.
Source§

type Item = T

Source§

fn item_count(&self) -> usize

Source§

fn push(&mut self, elem: Self::Item)

Source§

fn clear(&mut self)

Source§

impl<T, const N: usize> CollectionBase for Deque<T, N>

Available on crate feature heapless only.
Source§

type Item = T

Source§

fn item_count(&self) -> usize

Source§

fn push(&mut self, elem: Self::Item)

Source§

fn clear(&mut self)

Source§

impl<T: Eq + Hash, S: BuildHasher, A: Allocator> CollectionBase for HashSet<T, S, A>

Available on crate feature hashbrown only.
Source§

type Item = T

Source§

fn item_count(&self) -> usize

Source§

fn push(&mut self, elem: Self::Item)

Source§

fn clear(&mut self)

Source§

impl<T: Eq + Hash, S: BuildHasher, const N: usize> CollectionBase for IndexSet<T, S, N>

Available on crate feature heapless only.
Source§

type Item = T

Source§

fn item_count(&self) -> usize

Source§

fn push(&mut self, elem: Self::Item)

Source§

fn clear(&mut self)

Source§

impl<T: Eq + Hash, S: BuildHasher> CollectionBase for HashSet<T, S>

Available on crate feature std only.
Source§

type Item = T

Source§

fn item_count(&self) -> usize

Source§

fn push(&mut self, elem: Self::Item)

Source§

fn clear(&mut self)

Source§

impl<T: Eq + Hash, S: BuildHasher> CollectionBase for IndexSet<T, S>

Available on crate feature indexmap only.
Source§

type Item = T

Source§

fn item_count(&self) -> usize

Source§

fn push(&mut self, elem: Self::Item)

Source§

fn clear(&mut self)

Source§

impl<T: Ord, K: Kind, const N: usize> CollectionBase for BinaryHeap<T, K, N>

Available on crate feature heapless only.
Source§

type Item = T

Source§

fn item_count(&self) -> usize

Source§

fn push(&mut self, elem: Self::Item)

Source§

fn clear(&mut self)

Source§

impl<T: Ord> CollectionBase for BTreeSet<T>

Available on crate feature alloc only.
Source§

type Item = T

Source§

fn item_count(&self) -> usize

Source§

fn push(&mut self, elem: Self::Item)

Source§

fn clear(&mut self)

Source§

impl<T: Ord> CollectionBase for BinaryHeap<T>

Available on crate feature alloc only.
Source§

type Item = T

Source§

fn item_count(&self) -> usize

Source§

fn push(&mut self, elem: Self::Item)

Source§

fn clear(&mut self)

Source§

impl<T> CollectionBase for LinkedList<T>

Available on crate feature alloc only.
Source§

type Item = T

Source§

fn item_count(&self) -> usize

Source§

fn push(&mut self, elem: Self::Item)

Source§

fn clear(&mut self)

Source§

impl<T> CollectionBase for VecDeque<T>

Available on crate feature alloc only.
Source§

type Item = T

Source§

fn item_count(&self) -> usize

Source§

fn push(&mut self, elem: Self::Item)

Source§

fn clear(&mut self)

Implementors§