pub struct AssocList<K, V, A: Allocator = Global> { /* private fields */ }Expand description
An associated list based on a Vec, providing the usual map functionality.
The methods are purely based on the PartialEq implementation of the key types,
so most have a runtime characteristic of O(n).
In general, you should prefer to use either a HashMap,
or a BTreeMap.
The AssocList exists as a fallback if the key implements neither
Hash nor Ord.
Note: All methods only require PartialEq for the key, but there is a strong argument
to only use key types that are also (at least nearly) Ord.
For example, elements associated with a f32::NAN
cannot be found or deleted (PartialEq::eq will alway return false).
Implementations§
Source§impl<K, V> AssocList<K, V>
impl<K, V> AssocList<K, V>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create a new AssocList with at least the specified capacity.
§Panics
Panics if the new capacity exceeds isize::MAX bytes.
Source§impl<K, V, A: Allocator> AssocList<K, V, A>
impl<K, V, A: Allocator> AssocList<K, V, A>
Sourcepub const fn new_in(alloc: A) -> Self
Available on crate feature allocator_api only.
pub const fn new_in(alloc: A) -> Self
allocator_api only.Create a new AssocList with the provided allocator.
Sourcepub fn with_capacity_in(capacity: usize, alloc: A) -> Self
Available on crate feature allocator_api only.
pub fn with_capacity_in(capacity: usize, alloc: A) -> Self
allocator_api only.Create a new AssocList with at least the specified capacity with the provided allocator.
Source§impl<K, V, A: Allocator> AssocList<K, V, A>
impl<K, V, A: Allocator> AssocList<K, V, A>
Sourcepub fn into_keys(self) -> IntoKeys<K, V, A> ⓘ
pub fn into_keys(self) -> IntoKeys<K, V, A> ⓘ
Return a consuming iterator for all keys in the AssocList.
Sourcepub fn values_mut(&mut self) -> ValuesMut<'_, K, V> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> ⓘ
Return an iterator for mutable access to all values in the AssocList.
Sourcepub fn into_values(self) -> IntoValues<K, V, A> ⓘ
pub fn into_values(self) -> IntoValues<K, V, A> ⓘ
Return a consuming iterator for all values in the AssocList.
Sourcepub fn iter(&self) -> Iter<'_, (K, V)>
pub fn iter(&self) -> Iter<'_, (K, V)>
Return an iterator for all key-value pairs in the AssocList.
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
Return an iterator for all key-value pairs in the AssocList.
Sourcepub fn drain(&mut self) -> Drain<'_, K, V, A> ⓘ
pub fn drain(&mut self) -> Drain<'_, K, V, A> ⓘ
Removes all key-value pairs from the AssocList in bulk, returning all removed elements as an iterator.
If the iterator is dropped before being fully consumed, it drops the remaining removed elements.
§Leaking
See Vec::drain.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Return the number of key-value pairs currently contained in the AssocList.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the total number of elements the AssocList can hold without reallocating.
Sourcepub fn entry(&mut self, key: K) -> Entry<'_, K, V, A>where
K: PartialEq,
pub fn entry(&mut self, key: K) -> Entry<'_, K, V, A>where
K: PartialEq,
Get the Entry associated with the key.
Sourcepub fn contains_key<Q>(&self, key: &Q) -> bool
pub fn contains_key<Q>(&self, key: &Q) -> bool
Does the AssocList contain a value associated with the key.
Sourcepub fn get<Q>(&self, key: &Q) -> Option<&V>
pub fn get<Q>(&self, key: &Q) -> Option<&V>
Get a reference to the value associated with the key.
Sourcepub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
Get a reference to the key-value pair inside the AssocList associated with the key.
Sourcepub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
Get mutable access to the value associated with the key.
Sourcepub fn insert(&mut self, key: K, value: V) -> Option<V>where
K: PartialEq,
pub fn insert(&mut self, key: K, value: V) -> Option<V>where
K: PartialEq,
Insert a new element for the given key.
If the AssocList already contains an element associated with the key, it is replaced and returned.
§Panics
Panics if the new capacity exceeds isize::MAX bytes.
Sourcepub fn remove<Q>(&mut self, key: &Q) -> Option<V>
pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
Remove the element associated with the key from the AssocList and return it.
Sourcepub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
Remove the key-value pair associated with the key from the AssocList and return it.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional more key-value pairs to be inserted
in the given AssocList.
The collection may reserve more space to speculatively avoid frequent reallocations.
After calling reserve, capacity will be greater than or equal to self.len() + additional.
Does nothing if capacity is already sufficient.
§Panics
Panics if the new capacity exceeds isize::MAX bytes.
Sourcepub fn reserve_exact(&mut self, additional: usize)
pub fn reserve_exact(&mut self, additional: usize)
Reserves the minimum capacity for at least additional more key-value pairs to be inserted
in the given AssocList.
Unlike reserve, this will not deliberately over-allocate
to speculatively avoid frequent allocations. After calling reserve_exact,
capacity will be greater than or equal to self.len() + additional.
Does nothing if the capacity is already sufficient.
Note that the allocator may give the collection more space than it requests.
Therefore, capacity can not be relied upon to be precisely minimal.
Prefer reserve if future insertions are expected.
§Panics
Panics if the new capacity exceeds isize::MAX bytes.
Sourcepub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
Tries to reserve capacity for at least additional more key-value pairs to be inserted
in the given AssocList. The collection may reserve more space to speculatively avoid
frequent reallocations. After calling try_reserve,
capacity will be greater than or equal to self.len() + additional if it returns [Ok(())].
Does nothing if capacity is already sufficient.
This method preserves the contents even if an error occurs.
§Errors
If the capacity overflows, or the allocator reports a failure, then an error is returned.
Sourcepub fn try_reserve_exact(
&mut self,
additional: usize,
) -> Result<(), TryReserveError>
pub fn try_reserve_exact( &mut self, additional: usize, ) -> Result<(), TryReserveError>
Tries to reserve the minimum capacity for at least additional key-value pairs
to be inserted in the given AssocList. Unlike try_reserve,
this will not deliberately over-allocate to speculatively avoid frequent allocations.
After calling try_reserve_exact, capacity will be greater than or equal to
self.len() + additional if it returns [Ok(())].
Does nothing if the capacity is already sufficient.
Note that the allocator may give the collection more space than it requests.
Therefore, capacity can not be relied upon to be precisely minimal.
Prefer try_reserve if future insertions are expected.
§Errors
If the capacity overflows, or the allocator reports a failure, then an error is returned.
Sourcepub fn shrink_to(&mut self, min_capacity: usize)
pub fn shrink_to(&mut self, min_capacity: usize)
Shrinks the capacity of the underlying Vec with a lower bound.
The capacity will remain at least as large as both the length and the supplied value.
If the current capacity is less than the lower limit, this is a no-op.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the underlying Vec as much as possible.
It will drop down as close as possible to the length but the allocator may still inform the vector that there is space for a few more elements.
Trait Implementations§
Source§impl<'a, K, V, A: Allocator> Extend<(&'a K, &'a V)> for AssocList<K, V, A>
impl<'a, K, V, A: Allocator> Extend<(&'a K, &'a V)> for AssocList<K, V, A>
Source§fn extend<T: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<K: PartialEq, V, A: Allocator> Extend<(K, V)> for AssocList<K, V, A>
impl<K: PartialEq, V, A: Allocator> Extend<(K, V)> for AssocList<K, V, A>
Source§fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)