pub struct SmallMap<K, V, const N: usize> { /* private fields */ }Expand description
A map that lives on the stack for N items, then automatically spills to the heap.
§Overview
- Stack State: Zero allocations. Extremely fast FNV hashing. Data is stored inline.
- Heap State: Standard
HashMapperformance. Data is stored on the heap. - Spill: Occurs automatically when the stack capacity
Nis exceeded. This is a “Zero-Allocation Move”—keys/values are moved, not cloned.
§Capacity Constraints (N)
Due to the underlying heapless implementation constraints:
Nmust be a power of two (e.g., 2, 4, 8, 16, 32).Nmust be greater than 1.
Compilation will fail if these constraints are not met.
Implementations§
Source§impl<K, V, const N: usize> SmallMap<K, V, N>
impl<K, V, const N: usize> SmallMap<K, V, N>
Sourcepub const MAX_STACK_SIZE: usize
pub const MAX_STACK_SIZE: usize
The maximum allowed stack size in bytes (16 KB).
Because SmallMap stores data inline, a large N or large Key/Value types
can easily exceed the thread stack size. This limit prevents that.
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty map on the stack.
§Compile-Time Safety Check
This function enforces a strict size limit of 16 KB (MAX_STACK_SIZE).
Because the map size is known at compile time, the compiler will fail to build
if the total size of SmallMap<K, V, N> exceeds this limit. This prevents
accidental Stack Overflows (Segfaults).
§How to fix the build error
If your code fails to compile pointing to this assertion, you have two options:
- Reduce
N: If you don’t need that many items on the stack. - Box the Value: Change
SmallMap<K, V, N>toSmallMap<K, Box<V>, N>. This moves the bulk of the data to the heap immediately, keeping the stack footprint small.
Sourcepub fn is_on_stack(&self) -> bool
pub fn is_on_stack(&self) -> bool
Returns true if the map is currently storing data on the stack.
Returns false if it has spilled to the heap.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the map, removing all key-value pairs.
- Stack: Resets the index to 0.
- Heap: Clears the map but keeps the allocated memory for reuse.
Sourcepub fn insert(&mut self, key: K, value: V) -> Option<V>
pub fn insert(&mut self, key: K, value: V) -> Option<V>
Inserts a key-value pair into the map.
If the map is on the stack and full, this triggers a Spill to Heap. This implementation explicitly checks capacity constraints before insertion, ensuring a clean separation between the “Spill Decision” and the “Insert Action”.
Sourcepub fn get<Q>(&self, key: &Q) -> Option<&V>
pub fn get<Q>(&self, key: &Q) -> Option<&V>
Retrieves a reference to the value corresponding to the key.
This method is generic over the key type Q. This allows you to lookup
values using a reference (like &str) without allocating a new owned
key (like String).
§How it works (The Borrow Trait)
If the map stores keys of type K (e.g., String), you can pass a
query key of type Q (e.g., str) as long as:
KimplementsBorrow<Q>(meaningStringcan be viewed asstr).QimplementsHashandEq.- The hash of the borrowed
Kis identical to the hash ofQ.
§Example
// Assuming generic import for doc test
use small_collections::SmallMap;
let mut map = SmallMap::<String, i32, 4>::new();
map.insert("Apple".to_string(), 10);
// Works efficiently without allocating a new String for the lookup:
assert_eq!(map.get("Apple"), Some(&10));Sourcepub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
Retrieves a mutable reference to the value corresponding to the key.
Trait Implementations§
Source§impl<K: Eq + Hash, V, const N: usize> AnyMap<K, V> for SmallMap<K, V, N>
impl<K: Eq + Hash, V, const N: usize> AnyMap<K, V> for SmallMap<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, V, Q, const N: usize> Index<&Q> for SmallMap<K, V, N>
Allows read access using map[&key].
impl<K, V, Q, const N: usize> Index<&Q> for SmallMap<K, V, N>
Allows read access using map[&key].
§Panics
Panics if the key is not present in the map.
Source§impl<K, V, Q, const N: usize> IndexMut<&Q> for SmallMap<K, V, N>
Allows mutable access using map[&key] = new_value.
impl<K, V, Q, const N: usize> IndexMut<&Q> for SmallMap<K, V, N>
Allows mutable access using map[&key] = new_value.
§Panics
Panics if the key is not present in the map.
impl<K, V, const N: usize> Eq for SmallMap<K, V, N>
Auto Trait Implementations§
impl<K, V, const N: usize> Freeze for SmallMap<K, V, N>
impl<K, V, const N: usize> RefUnwindSafe for SmallMap<K, V, N>where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V, const N: usize> Send for SmallMap<K, V, N>
impl<K, V, const N: usize> Sync for SmallMap<K, V, N>
impl<K, V, const N: usize> Unpin for SmallMap<K, V, N>
impl<K, V, const N: usize> UnsafeUnpin for SmallMap<K, V, N>where
K: UnsafeUnpin,
V: UnsafeUnpin,
impl<K, V, const N: usize> UnwindSafe for SmallMap<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.