pub struct HeaplessOrderedMap<K: Eq + Hash, V, const N: usize> { /* private fields */ }Expand description
A stack-allocated, insertion-order-preserving map backed by heapless::LinearMap.
§Architecture & Pseudocode
This map stores elements linearly in the exact sequence they were added. It relies on a simple contiguous array layout under the hood, yielding O(N) operations.
map: Aheapless::LinearMap<K, V, N>.
§Insert Algorithm
1. If map is physically full (`len == N`) AND the key is not already inside:
a. Return `Err((key, value))` (triggers heap spill in `SmallOrderedMap`).
2. Else (capacity available or updating existing key):
a. Let `old_val = map.insert(key, value)`.
b. Return `Ok(old_val)`.§Remove Algorithm (Order-Preserving)
1. Initialize a temporary empty `LinearMap`.
2. Move the elements from the current map into `old_map` using `core::mem::replace`.
3. Iterate through `old_map` elements:
a. If `element.key == target_key` (and we haven't removed it yet):
i. Save `element.value` as the return value.
b. Else:
i. Insert `element` into the temporary map.
4. Replace `self.map` with the temporary map.
5. Return the saved `old_val`.Implementations§
Source§impl<K: Eq + Hash, V, const N: usize> HeaplessOrderedMap<K, V, N>
impl<K: Eq + Hash, V, const N: usize> HeaplessOrderedMap<K, V, N>
Sourcepub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
Returns true if the map has reached its compile-time capacity N.
When is_full() returns true, inserting a new key will return Err.
Sourcepub fn insert(&mut self, key: K, value: V) -> Result<Option<V>, (K, V)>
pub fn insert(&mut self, key: K, value: V) -> Result<Option<V>, (K, V)>
Inserts or updates a key-value pair.
§Returns
| Variant | Meaning |
|---|---|
Ok(Some(old)) | Key already existed; old value returned, new value stored in place. |
Ok(None) | Key was new; entry appended (insertion order preserved). |
Err((key, value)) | Map is full and key is new; caller must spill to heap. |
§Complexity
O(N) — linear scan to check for the existing key before delegating to LinearMap.
Sourcepub fn get<Q>(&self, key: &Q) -> Option<&V>
pub fn get<Q>(&self, key: &Q) -> Option<&V>
Returns a shared reference to the value associated with key, or None.
Accepts any type Q where K: Borrow<Q> and Q: Hash + Eq.
Complexity: O(N) linear scan.
Sourcepub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
Returns an exclusive reference to the value associated with key, or None.
Accepts any type Q where K: Borrow<Q> and Q: Hash + Eq.
Complexity: O(N) linear scan.
Sourcepub fn remove<Q>(&mut self, key: &Q) -> Option<V>
pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
Removes and returns the value for key, preserving insertion order for remaining entries.
Accepts any type Q where K: Borrow<Q> and Q: Hash + Eq.
§Implementation note
heapless::LinearMap::remove requires K: PartialEq<Q>, which is more restrictive
than K: Borrow<Q>. To stay generic we drain the map into a temporary buffer,
skipping the matching entry, then write the buffer back. This preserves insertion
order and is allocation-free, at the cost of O(N) time.
Sourcepub fn contains_key<Q>(&self, key: &Q) -> bool
pub fn contains_key<Q>(&self, key: &Q) -> bool
Returns true if the map contains an entry for key.
Accepts any type Q where K: Borrow<Q> and Q: Hash + Eq.
Complexity: O(N).
Sourcepub fn iter(&self) -> Iter<'_, K, V>
pub fn iter(&self) -> Iter<'_, K, V>
Returns an iterator over (&K, &V) pairs in insertion order.
Sourcepub fn into_inner(self) -> LinearMap<K, V, N>
pub fn into_inner(self) -> LinearMap<K, V, N>
Consumes self and returns the underlying heapless::LinearMap.
Useful when the caller needs direct access to the inner map, e.g. during spill-to-heap.
Trait Implementations§
Source§impl<K: Eq + Hash, V, const N: usize> AnyMap<K, V> for HeaplessOrderedMap<K, V, N>
impl<K: Eq + Hash, V, const N: usize> AnyMap<K, V> for HeaplessOrderedMap<K, V, N>
Source§fn insert(&mut self, key: K, value: V) -> Option<V>
fn insert(&mut self, key: K, value: V) -> Option<V>
Source§fn get<Q>(&self, key: &Q) -> Option<&V>
fn get<Q>(&self, key: &Q) -> Option<&V>
Source§fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
Source§fn remove<Q>(&mut self, key: &Q) -> Option<V>
fn remove<Q>(&mut self, key: &Q) -> Option<V>
Source§fn contains_key<Q>(&self, key: &Q) -> bool
fn contains_key<Q>(&self, key: &Q) -> bool
true if the collection contains the item or key.Source§impl<K: Clone + Eq + Hash, V: Clone, const N: usize> Clone for HeaplessOrderedMap<K, V, N>
impl<K: Clone + Eq + Hash, V: Clone, const N: usize> Clone for HeaplessOrderedMap<K, V, N>
Source§fn clone(&self) -> HeaplessOrderedMap<K, V, N>
fn clone(&self) -> HeaplessOrderedMap<K, V, N>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<K: Eq + Hash, V, const N: usize> Default for HeaplessOrderedMap<K, V, N>
impl<K: Eq + Hash, V, const N: usize> Default for HeaplessOrderedMap<K, V, N>
Source§fn default() -> Self
fn default() -> Self
Creates an empty map. Equivalent to HeaplessOrderedMap::new.
Source§impl<K: Eq + Hash, V, const N: usize> IntoIterator for HeaplessOrderedMap<K, V, N>
impl<K: Eq + Hash, V, const N: usize> IntoIterator for HeaplessOrderedMap<K, V, N>
Source§impl<K, V, const N: usize> PartialEq for HeaplessOrderedMap<K, V, N>
impl<K, V, const N: usize> PartialEq for HeaplessOrderedMap<K, V, N>
impl<K, V, const N: usize> Eq for HeaplessOrderedMap<K, V, N>
Auto Trait Implementations§
impl<K, V, const N: usize> Freeze for HeaplessOrderedMap<K, V, N>
impl<K, V, const N: usize> RefUnwindSafe for HeaplessOrderedMap<K, V, N>where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V, const N: usize> Send for HeaplessOrderedMap<K, V, N>
impl<K, V, const N: usize> Sync for HeaplessOrderedMap<K, V, N>
impl<K, V, const N: usize> Unpin for HeaplessOrderedMap<K, V, N>
impl<K, V, const N: usize> UnsafeUnpin for HeaplessOrderedMap<K, V, N>where
K: UnsafeUnpin,
V: UnsafeUnpin,
impl<K, V, const N: usize> UnwindSafe for HeaplessOrderedMap<K, V, N>where
K: UnwindSafe,
V: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.