Skip to main content

CollectionCapacity

Trait CollectionCapacity 

Source
pub trait CollectionCapacity: CollectionBase {
    type ReserveError;

    // Required methods
    fn capacity(&self) -> usize;
    fn reserve(&mut self, additional: usize);
    fn try_reserve(
        &mut self,
        additional: usize,
    ) -> Result<(), Self::ReserveError>;

    // Provided method
    fn try_push(&mut self, elem: Self::Item) -> Result<(), Self::Item> { ... }
}
Expand description

Trait implemented by collections that can reserve storage capacity.

Capacity and reservation amounts use the collection’s native storage unit: bytes for strings and items for other collections. A string with a capacity of n may therefore hold fewer than n chars.

Fixed-capacity collections never allocate: their capacity is constant, and a reservation only checks that the requested room is left.

§Untrusted sizes

Collection::with_capacity, CollectionAlloc::with_capacity_in and reserve panic or abort the process when the requested memory cannot be allocated, which makes a size taken from untrusted input a denial-of-service vector. Use try_reserve and try_push for such sizes and data.

Required Associated Types§

Source

type ReserveError

Error returned when a reservation cannot be satisfied.

Required Methods§

Source

fn capacity(&self) -> usize

Returns the current capacity in the collection’s native storage unit.

Source

fn reserve(&mut self, additional: usize)

Reserves space for at least additional more storage units.

§Panics

Panics or aborts if the allocation fails or the new capacity overflows. Panics if a fixed-capacity collection has less room left.

Source

fn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError>

Attempts to reserve space for at least additional more storage units without panicking or aborting on failure.

Provided Methods§

Source

fn try_push(&mut self, elem: Self::Item) -> Result<(), Self::Item>

Attempts to append or insert an item without panicking or aborting when there is no room for it.

Returns the item if the required capacity cannot be reserved. Inserting an already present set value or map key into a full fixed-capacity collection succeeds, since it needs no room.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl CollectionCapacity for String

Available on crate feature alloc only.
Source§

type ReserveError = TryReserveError

Source§

fn capacity(&self) -> usize

Source§

fn reserve(&mut self, additional: usize)

Source§

fn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError>

Source§

fn try_push(&mut self, elem: Self::Item) -> Result<(), Self::Item>

Source§

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

Available on crate feature hashbrown only.
Source§

type ReserveError = TryReserveError

Source§

fn capacity(&self) -> usize

Source§

fn reserve(&mut self, additional: usize)

Source§

fn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError>

Source§

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

Available on crate feature heapless only.
Source§

type ReserveError = CapacityError

Source§

fn capacity(&self) -> usize

Source§

fn reserve(&mut self, additional: usize)

Source§

fn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError>

Source§

fn try_push(&mut self, elem: Self::Item) -> Result<(), Self::Item>

Source§

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

Available on crate feature std only.
Source§

type ReserveError = TryReserveError

Source§

fn capacity(&self) -> usize

Source§

fn reserve(&mut self, additional: usize)

Source§

fn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError>

Source§

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

Available on crate feature indexmap only.
Source§

type ReserveError = TryReserveError

Source§

fn capacity(&self) -> usize

Source§

fn reserve(&mut self, additional: usize)

Source§

fn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError>

Source§

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

Available on crate feature heapless only.
Source§

type ReserveError = CapacityError

Source§

fn capacity(&self) -> usize

Source§

fn reserve(&mut self, additional: usize)

Source§

fn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError>

Source§

fn try_push(&mut self, elem: Self::Item) -> Result<(), Self::Item>

Source§

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

Available on crate feature heapless only.
Source§

type ReserveError = CapacityError

Source§

fn capacity(&self) -> usize

Source§

fn reserve(&mut self, additional: usize)

Source§

fn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError>

Source§

fn try_push(&mut self, elem: Self::Item) -> Result<(), Self::Item>

Source§

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

Available on crate feature alloc only.
Source§

type ReserveError = TryReserveError

Source§

fn capacity(&self) -> usize

Source§

fn reserve(&mut self, additional: usize)

Source§

fn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError>

Source§

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

Available on crate feature heapless only.
Source§

type ReserveError = CapacityError

Source§

fn capacity(&self) -> usize

Source§

fn reserve(&mut self, additional: usize)

Source§

fn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError>

Source§

fn try_push(&mut self, elem: Self::Item) -> Result<(), Self::Item>

Source§

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

Available on crate feature heapless only.
Source§

type ReserveError = CapacityError

Source§

fn capacity(&self) -> usize

Source§

fn reserve(&mut self, additional: usize)

Source§

fn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError>

Source§

fn try_push(&mut self, elem: Self::Item) -> Result<(), Self::Item>

Source§

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

Available on crate feature hashbrown only.
Source§

type ReserveError = TryReserveError

Source§

fn capacity(&self) -> usize

Source§

fn reserve(&mut self, additional: usize)

Source§

fn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError>

Source§

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

Available on crate feature heapless only.
Source§

type ReserveError = CapacityError

Source§

fn capacity(&self) -> usize

Source§

fn reserve(&mut self, additional: usize)

Source§

fn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError>

Source§

fn try_push(&mut self, elem: Self::Item) -> Result<(), Self::Item>

Source§

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

Available on crate feature std only.
Source§

type ReserveError = TryReserveError

Source§

fn capacity(&self) -> usize

Source§

fn reserve(&mut self, additional: usize)

Source§

fn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError>

Source§

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

Available on crate feature indexmap only.
Source§

type ReserveError = TryReserveError

Source§

fn capacity(&self) -> usize

Source§

fn reserve(&mut self, additional: usize)

Source§

fn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError>

Source§

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

Available on crate feature heapless only.
Source§

type ReserveError = CapacityError

Source§

fn capacity(&self) -> usize

Source§

fn reserve(&mut self, additional: usize)

Source§

fn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError>

Source§

fn try_push(&mut self, elem: Self::Item) -> Result<(), Self::Item>

Source§

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

Available on crate feature alloc only.
Source§

type ReserveError = TryReserveError

Source§

fn capacity(&self) -> usize

Source§

fn reserve(&mut self, additional: usize)

Source§

fn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError>

Source§

impl<T> CollectionCapacity for VecDeque<T>

Available on crate feature alloc only.
Source§

type ReserveError = TryReserveError

Source§

fn capacity(&self) -> usize

Source§

fn reserve(&mut self, additional: usize)

Source§

fn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError>

Implementors§