Skip to main content

BoundedMap

Struct BoundedMap 

Source
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). Use 0 for 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>

Source

pub fn len(&self) -> usize

Returns the number of entries.

Source

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.

Source

pub fn iter(&self) -> Iter<'_, (K, V)>

Returns an iterator over the (K, V) entries in insertion order.

Source

pub fn keys(&self) -> impl Iterator<Item = &K>

Returns an iterator over the keys in insertion order.

Source

pub fn values(&self) -> impl Iterator<Item = &V>

Returns an iterator over the values in insertion order.

Source

pub fn as_slice(&self) -> &[(K, V)]

Returns the entries as a slice in insertion order.

Source

pub fn into_inner(self) -> Vec<(K, V)>

Consumes the wrapper and returns the inner Vec<(K, V)>.

Source

pub const fn min_len() -> usize

Returns the minimum allowed number of entries.

Source

pub const fn max_len() -> usize

Returns the maximum allowed number of entries.

Source§

impl<K: PartialEq, V, const MIN: usize, const MAX: usize> BoundedMap<K, V, MIN, MAX>

Source

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.

Source

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.

Source

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.

Source

pub fn get(&self, key: &K) -> Option<&V>

Returns a reference to the value for key, or None if absent.

Source

pub fn get_mut(&mut self, key: &K) -> Option<&mut V>

Returns a mutable reference to the value for key, or None if absent.

Source

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>

Source§

fn clone(&self) -> BoundedMap<K, V, MIN, MAX>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Debug, V: Debug, const MIN: usize, const MAX: usize> Debug for BoundedMap<K, V, MIN, MAX>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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)>

Source§

fn from(value: BoundedMap<K, V, MIN, MAX>) -> Self

Converts to this type from the input type.
Source§

impl<K: Hash, V: Hash, const MIN: usize, const MAX: usize> Hash for BoundedMap<K, V, MIN, MAX>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<K, V, const MIN: usize, const MAX: usize> StructuralPartialEq for BoundedMap<K, V, MIN, MAX>

Source§

impl<K: PartialEq, V, const MIN: usize, const MAX: usize> TryFrom<Vec<(K, V)>> for BoundedMap<K, V, MIN, MAX>

Source§

type Error = CollectionError

The type returned in the event of a conversion error.
Source§

fn try_from(entries: Vec<(K, V)>) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<K, V, const MIN: usize, const MAX: usize> Freeze for BoundedMap<K, V, MIN, MAX>

§

impl<K, V, const MIN: usize, const MAX: usize> RefUnwindSafe for BoundedMap<K, V, MIN, MAX>

§

impl<K, V, const MIN: usize, const MAX: usize> Send for BoundedMap<K, V, MIN, MAX>
where K: Send, V: Send,

§

impl<K, V, const MIN: usize, const MAX: usize> Sync for BoundedMap<K, V, MIN, MAX>
where K: Sync, V: Sync,

§

impl<K, V, const MIN: usize, const MAX: usize> Unpin for BoundedMap<K, V, MIN, MAX>
where K: Unpin, V: Unpin,

§

impl<K, V, const MIN: usize, const MAX: usize> UnsafeUnpin for BoundedMap<K, V, MIN, MAX>

§

impl<K, V, const MIN: usize, const MAX: usize> UnwindSafe for BoundedMap<K, V, MIN, MAX>
where K: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.