pub struct BoundedMap<K, V, const MIN: usize, const MAX: usize>(/* private fields */);Expand description
Owned key-value map constrained by inclusive entry-count bounds.
BoundedMap<K, V, MIN, MAX> guarantees that the number of entries is always
in the range MIN..=MAX and that every key is unique. It is backed by a
Vec<(K, V)> in insertion order, so iteration is deterministic and pulls in
no hashing or ordering machinery — lookups are a linear scan, which is fine
for the small, bounded sizes this type is meant for.
Mutations that would violate the bounds return a CollectionError instead
of panicking. Construction rejects duplicate keys with
CollectionError::Duplicate.
§Const parameters
MIN: minimum number of entries (inclusive). Use0for no lower bound.MAX: maximum number of entries (inclusive).
Construction fails with CollectionError::InvalidBounds if MIN > MAX.
§Equality
Equality is order-sensitive: two maps with the same entries inserted in a different order are not equal, matching the insertion-order semantics.
Implementations§
Source§impl<K, V, const MIN: usize, const MAX: usize> BoundedMap<K, V, MIN, MAX>
impl<K, V, const MIN: usize, const MAX: usize> BoundedMap<K, V, MIN, MAX>
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the map contains no entries.
Always returns false when MIN > 0, since construction guarantees at
least MIN entries.
Sourcepub fn iter(&self) -> Iter<'_, (K, V)>
pub fn iter(&self) -> Iter<'_, (K, V)>
Returns an iterator over the (K, V) entries in insertion order.
Sourcepub fn keys(&self) -> impl Iterator<Item = &K>
pub fn keys(&self) -> impl Iterator<Item = &K>
Returns an iterator over the keys in insertion order.
Sourcepub fn values(&self) -> impl Iterator<Item = &V>
pub fn values(&self) -> impl Iterator<Item = &V>
Returns an iterator over the values in insertion order.
Sourcepub fn into_inner(self) -> Vec<(K, V)>
pub fn into_inner(self) -> Vec<(K, V)>
Consumes the wrapper and returns the inner Vec<(K, V)>.
Source§impl<K: PartialEq, V, const MIN: usize, const MAX: usize> BoundedMap<K, V, MIN, MAX>
impl<K: PartialEq, V, const MIN: usize, const MAX: usize> BoundedMap<K, V, MIN, MAX>
Sourcepub fn new(entries: Vec<(K, V)>) -> CollectionResult<Self>
pub fn new(entries: Vec<(K, V)>) -> CollectionResult<Self>
Creates a BoundedMap from a vector of entries.
Returns an error if the bounds are invalid (MIN > MAX), the entry count
is out of range, or two entries share a key.
Sourcepub fn insert(&mut self, key: K, value: V) -> CollectionResult<Option<V>>
pub fn insert(&mut self, key: K, value: V) -> CollectionResult<Option<V>>
Inserts a key-value pair.
If the key already exists, its value is replaced and the old value is
returned as Ok(Some(old)) (the entry count does not change). If the key
is new, it is appended and Ok(None) is returned, unless the map is
already at capacity (len == MAX), in which case
CollectionError::TooMany is returned and nothing changes.
Sourcepub fn remove(&mut self, key: &K) -> CollectionResult<Option<V>>
pub fn remove(&mut self, key: &K) -> CollectionResult<Option<V>>
Removes the entry for key, returning its value.
Returns Ok(None) if the key is absent (no change). Returns
CollectionError::TooFew if removing would bring the entry count below
MIN, leaving the map unchanged.
Sourcepub fn get(&self, key: &K) -> Option<&V>
pub fn get(&self, key: &K) -> Option<&V>
Returns a reference to the value for key, or None if absent.
Sourcepub fn get_mut(&mut self, key: &K) -> Option<&mut V>
pub fn get_mut(&mut self, key: &K) -> Option<&mut V>
Returns a mutable reference to the value for key, or None if absent.
Sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
Returns true if the map contains key.
Trait Implementations§
Source§impl<K: Clone, V: Clone, const MIN: usize, const MAX: usize> Clone for BoundedMap<K, V, MIN, MAX>
impl<K: Clone, V: Clone, const MIN: usize, const MAX: usize> Clone for BoundedMap<K, V, MIN, MAX>
Source§fn clone(&self) -> BoundedMap<K, V, MIN, MAX>
fn clone(&self) -> BoundedMap<K, V, MIN, MAX>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<K: Debug, V: Debug, const MIN: usize, const MAX: usize> Debug for BoundedMap<K, V, MIN, MAX>
impl<K: Debug, V: Debug, const MIN: usize, const MAX: usize> Debug for BoundedMap<K, V, MIN, MAX>
impl<K: Eq, V: Eq, const MIN: usize, const MAX: usize> Eq for BoundedMap<K, V, MIN, MAX>
Source§impl<K, V, const MIN: usize, const MAX: usize> From<BoundedMap<K, V, MIN, MAX>> for Vec<(K, V)>
impl<K, V, const MIN: usize, const MAX: usize> From<BoundedMap<K, V, MIN, MAX>> for Vec<(K, V)>
Source§fn from(value: BoundedMap<K, V, MIN, MAX>) -> Self
fn from(value: BoundedMap<K, V, MIN, MAX>) -> Self
Source§impl<K: Hash, V: Hash, const MIN: usize, const MAX: usize> Hash for BoundedMap<K, V, MIN, MAX>
impl<K: Hash, V: Hash, const MIN: usize, const MAX: usize> Hash for BoundedMap<K, V, MIN, MAX>
Source§impl<K: PartialEq, V: PartialEq, const MIN: usize, const MAX: usize> PartialEq for BoundedMap<K, V, MIN, MAX>
impl<K: PartialEq, V: PartialEq, const MIN: usize, const MAX: usize> PartialEq for BoundedMap<K, V, MIN, MAX>
Source§fn eq(&self, other: &BoundedMap<K, V, MIN, MAX>) -> bool
fn eq(&self, other: &BoundedMap<K, V, MIN, MAX>) -> bool
self and other values to be equal, and is used by ==.