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§
Sourcetype ReserveError
type ReserveError
Error returned when a reservation cannot be satisfied.
Required Methods§
Sourcefn capacity(&self) -> usize
fn capacity(&self) -> usize
Returns the current capacity in the collection’s native storage unit.
Sourcefn reserve(&mut self, additional: usize)
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.
Sourcefn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError>
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§
Sourcefn try_push(&mut self, elem: Self::Item) -> Result<(), Self::Item>
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.
impl CollectionCapacity for String
alloc only.Source§impl<K: Eq + Hash, V, S: BuildHasher, A: Allocator> CollectionCapacity for HashMap<K, V, S, A>
Available on crate feature hashbrown only.
impl<K: Eq + Hash, V, S: BuildHasher, A: Allocator> CollectionCapacity for HashMap<K, V, S, A>
hashbrown only.type ReserveError = TryReserveError
fn capacity(&self) -> usize
fn reserve(&mut self, additional: usize)
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.
impl<K: Eq + Hash, V, S: BuildHasher, const N: usize> CollectionCapacity for IndexMap<K, V, S, N>
heapless only.Source§impl<K: Eq + Hash, V, S: BuildHasher> CollectionCapacity for HashMap<K, V, S>
Available on crate feature std only.
impl<K: Eq + Hash, V, S: BuildHasher> CollectionCapacity for HashMap<K, V, S>
std only.type ReserveError = TryReserveError
fn capacity(&self) -> usize
fn reserve(&mut self, additional: usize)
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.
impl<K: Eq + Hash, V, S: BuildHasher> CollectionCapacity for IndexMap<K, V, S>
indexmap only.type ReserveError = TryReserveError
fn capacity(&self) -> usize
fn reserve(&mut self, additional: usize)
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.
impl<K: Eq, V, const N: usize> CollectionCapacity for LinearMap<K, V, N>
heapless only.Source§impl<LenT: LenType, const N: usize> CollectionCapacity for String<N, LenT>
Available on crate feature heapless only.
impl<LenT: LenType, const N: usize> CollectionCapacity for String<N, LenT>
heapless only.Source§impl<T, A: Allocator> CollectionCapacity for Vec<T, A>
Available on crate feature alloc only.
impl<T, A: Allocator> CollectionCapacity for Vec<T, A>
alloc only.type ReserveError = TryReserveError
fn capacity(&self) -> usize
fn reserve(&mut self, additional: usize)
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.
impl<T, LenT: LenType, const N: usize> CollectionCapacity for Vec<T, N, LenT>
heapless only.Source§impl<T, const N: usize> CollectionCapacity for Deque<T, N>
Available on crate feature heapless only.
impl<T, const N: usize> CollectionCapacity for Deque<T, N>
heapless only.Source§impl<T: Eq + Hash, S: BuildHasher, A: Allocator> CollectionCapacity for HashSet<T, S, A>
Available on crate feature hashbrown only.
impl<T: Eq + Hash, S: BuildHasher, A: Allocator> CollectionCapacity for HashSet<T, S, A>
hashbrown only.type ReserveError = TryReserveError
fn capacity(&self) -> usize
fn reserve(&mut self, additional: usize)
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.
impl<T: Eq + Hash, S: BuildHasher, const N: usize> CollectionCapacity for IndexSet<T, S, N>
heapless only.Source§impl<T: Eq + Hash, S: BuildHasher> CollectionCapacity for HashSet<T, S>
Available on crate feature std only.
impl<T: Eq + Hash, S: BuildHasher> CollectionCapacity for HashSet<T, S>
std only.type ReserveError = TryReserveError
fn capacity(&self) -> usize
fn reserve(&mut self, additional: usize)
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.
impl<T: Eq + Hash, S: BuildHasher> CollectionCapacity for IndexSet<T, S>
indexmap only.type ReserveError = TryReserveError
fn capacity(&self) -> usize
fn reserve(&mut self, additional: usize)
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.
impl<T: Ord, K: Kind, const N: usize> CollectionCapacity for BinaryHeap<T, K, N>
heapless only.Source§impl<T: Ord> CollectionCapacity for BinaryHeap<T>
Available on crate feature alloc only.
impl<T: Ord> CollectionCapacity for BinaryHeap<T>
alloc only.type ReserveError = TryReserveError
fn capacity(&self) -> usize
fn reserve(&mut self, additional: usize)
fn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError>
Source§impl<T> CollectionCapacity for VecDeque<T>
Available on crate feature alloc only.
impl<T> CollectionCapacity for VecDeque<T>
alloc only.