SmallMap

Enum SmallMap 

Source
pub enum SmallMap<const N: usize, K, V, S = RandomState> {
    Heap(HashMap<K, V, S>),
    Inline(Inline<N, K, V, S>),
}

Variants§

§

Heap(HashMap<K, V, S>)

§

Inline(Inline<N, K, V, S>)

Implementations§

Source§

impl<const N: usize, K, V, S: Default> SmallMap<N, K, V, S>

Source

pub fn new() -> Self

Creates an empty SmallMap.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates an empty SmallMap with the specified capacity.

The hash map will be able to hold at least capacity elements without reallocating. If capacity is smaller than N, the hash map will not allocate.

Source§

impl<const N: usize, K, V, S> SmallMap<N, K, V, S>

Source

pub const fn with_hasher(hash_builder: S) -> Self

Creates an empty SmallMap which will use the given hash builder to hash keys. It will be allocated with the given allocator.

The hash map is initially created with a capacity of N, so it will not allocate until it its size bigger than inline size N.

§Examples
use core::hash::BuildHasherDefault;

use small_map::SmallMap;

let s = BuildHasherDefault::<ahash::AHasher>::default();
let mut map = SmallMap::<8, _, _, _>::with_hasher(s);
map.insert(1, 2);
Source

pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self

Creates an empty SmallMap with the specified capacity, using hash_builder to hash the keys.

The hash map will be able to hold at least capacity elements without reallocating. If capacity is smaller than or eq to N, the hash map will not allocate.

Source

pub fn is_inline(&self) -> bool

What branch it is now.

Source

pub fn capacity(&self) -> usize

Returns the number of elements the map can hold without reallocating.

This number is a lower bound; the SmallMap<N, K, V> might be able to hold more, but is guaranteed to be able to hold at least this many.

§Examples
use small_map::SmallMap;

let map: SmallMap<8, i32, i32> = SmallMap::with_capacity(100);
assert_eq!(map.len(), 0);
assert!(map.capacity() >= 100);

let map: SmallMap<8, i32, i32> = SmallMap::with_capacity(2);
assert_eq!(map.len(), 0);
assert!(map.capacity() >= 8);
Source§

impl<const N: usize, K, V, S> SmallMap<N, K, V, S>
where K: Eq + Hash, S: BuildHasher,

Source

pub fn get<Q>(&self, k: &Q) -> Option<&V>
where Q: ?Sized + Hash + Equivalent<K>,

Returns a reference to the value corresponding to the key.

§Examples
use small_map::SmallMap;

let mut map: SmallMap<8, i32, &str> = SmallMap::new();
map.insert(1, "a");
assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), None);
Source

pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
where Q: ?Sized + Hash + Equivalent<K>,

Returns a mutable reference to the value corresponding to the key.

§Examples
use small_map::SmallMap;

let mut map: SmallMap<8, i32, i32> = SmallMap::new();
map.insert(1, 10);
if let Some(v) = map.get_mut(&1) {
    *v = 20;
}
assert_eq!(map.get(&1), Some(&20));
Source

pub fn get_key_value<Q>(&self, k: &Q) -> Option<(&K, &V)>
where Q: ?Sized + Hash + Equivalent<K>,

Returns the key-value pair corresponding to the supplied key.

§Examples
use small_map::SmallMap;

let mut map: SmallMap<8, i32, &str> = SmallMap::new();
map.insert(1, "a");
assert_eq!(map.get_key_value(&1), Some((&1, &"a")));
assert_eq!(map.get_key_value(&2), None);
Source

pub fn contains_key<Q>(&self, k: &Q) -> bool
where Q: ?Sized + Hash + Equivalent<K>,

Returns true if the map contains a value for the specified key.

§Examples
use small_map::SmallMap;

let mut map: SmallMap<8, i32, &str> = SmallMap::new();
map.insert(1, "a");
assert!(map.contains_key(&1));
assert!(!map.contains_key(&2));
Source

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

Inserts a key-value pair into the map.

If the map did not have this key present, None is returned. If the map did have this key present, the value is updated, and the old value is returned.

§Examples
use small_map::SmallMap;

let mut map: SmallMap<8, i32, &str> = SmallMap::new();
assert_eq!(map.insert(1, "a"), None);
assert_eq!(map.insert(1, "b"), Some("a"));
assert_eq!(map.get(&1), Some(&"b"));
Source

pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
where Q: ?Sized + Hash + Equivalent<K>,

Removes a key from the map, returning the value at the key if the key was previously in the map.

§Examples
use small_map::SmallMap;

let mut map: SmallMap<8, i32, &str> = SmallMap::new();
map.insert(1, "a");
assert_eq!(map.remove(&1), Some("a"));
assert_eq!(map.remove(&1), None);
Source

pub fn remove_entry<Q>(&mut self, k: &Q) -> Option<(K, V)>
where Q: ?Sized + Hash + Equivalent<K>,

Removes a key from the map, returning the stored key and value if the key was previously in the map.

§Examples
use small_map::SmallMap;

let mut map: SmallMap<8, i32, &str> = SmallMap::new();
map.insert(1, "a");
assert_eq!(map.remove_entry(&1), Some((1, "a")));
assert_eq!(map.remove_entry(&1), None);
Source

pub fn retain<F>(&mut self, f: F)
where F: FnMut(&K, &mut V) -> bool,

Retains only the elements specified by the predicate.

In other words, remove all pairs (k, v) for which f(&k, &mut v) returns false.

§Examples
use small_map::SmallMap;

let mut map: SmallMap<8, i32, i32> = SmallMap::new();
for i in 0..8 {
    map.insert(i, i * 10);
}
map.retain(|&k, _| k % 2 == 0);
assert_eq!(map.len(), 4);
Source§

impl<const N: usize, K, V, S> SmallMap<N, K, V, S>
where S: Clone,

Source

pub fn clear(&mut self)

Clears the map.

This method clears the map as resets it to the Inline state.

§Examples
use small_map::SmallMap;

let mut map: SmallMap<8, i32, ()> = SmallMap::new();
for i in 0..16 {
    map.insert(i, ());
}
assert!(!map.is_inline());
assert_eq!(map.len(), 16);

map.clear();
assert!(map.is_inline());
assert_eq!(map.len(), 0);
Source§

impl<const N: usize, K, V, S> SmallMap<N, K, V, S>

Source

pub fn len(&self) -> usize

Returns the number of elements in the map.

§Examples
use small_map::SmallMap;

let mut map: SmallMap<8, i32, i32> = SmallMap::new();
assert_eq!(map.len(), 0);
map.insert(1, 10);
assert_eq!(map.len(), 1);
Source

pub fn is_empty(&self) -> bool

Returns true if the map contains no elements.

§Examples
use small_map::SmallMap;

let mut map: SmallMap<8, i32, i32> = SmallMap::new();
assert!(map.is_empty());
map.insert(1, 10);
assert!(!map.is_empty());
Source

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

An iterator visiting all key-value pairs in arbitrary order.

§Examples
use small_map::SmallMap;

let mut map: SmallMap<8, &str, i32> = SmallMap::new();
map.insert("a", 1);
map.insert("b", 2);

for (key, val) in map.iter() {
    println!("key: {key} val: {val}");
}
Source

pub fn keys(&self) -> Keys<'_, N, K, V>

An iterator visiting all keys in arbitrary order.

§Examples
use small_map::SmallMap;

let mut map: SmallMap<8, &str, i32> = SmallMap::new();
map.insert("a", 1);
map.insert("b", 2);

for key in map.keys() {
    println!("{key}");
}
Source

pub fn values(&self) -> Values<'_, N, K, V>

An iterator visiting all values in arbitrary order.

§Examples
use small_map::SmallMap;

let mut map: SmallMap<8, &str, i32> = SmallMap::new();
map.insert("a", 1);
map.insert("b", 2);

for val in map.values() {
    println!("{val}");
}
Source

pub fn into_keys(self) -> IntoKeys<N, K, V>

Creates a consuming iterator visiting all the keys in arbitrary order.

§Examples
use small_map::SmallMap;

let mut map: SmallMap<8, &str, i32> = SmallMap::new();
map.insert("a", 1);
map.insert("b", 2);

let keys: Vec<_> = map.into_keys().collect();
Source

pub fn into_values(self) -> IntoValues<N, K, V>

Creates a consuming iterator visiting all the values in arbitrary order.

§Examples
use small_map::SmallMap;

let mut map: SmallMap<8, &str, i32> = SmallMap::new();
map.insert("a", 1);
map.insert("b", 2);

let values: Vec<_> = map.into_values().collect();

Trait Implementations§

Source§

impl<const N: usize, K: Clone, V: Clone, S: Clone> Clone for SmallMap<N, K, V, S>

Source§

fn clone(&self) -> SmallMap<N, K, V, S>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<const N: usize, K, V, S> Debug for SmallMap<N, K, V, S>
where K: Debug, V: Debug,

Source§

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

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

impl<const N: usize, K, V, S: Default> Default for SmallMap<N, K, V, S>

Source§

fn default() -> Self

Creates an empty SmallMap<N, K, V, S>, with the Default value for the hasher.

§Examples
use std::collections::hash_map::RandomState;

use small_map::SmallMap;

// You can specify all types of SmallMap, including N and hasher.
// Created map is empty and don't allocate memory
let map: SmallMap<8, u32, String> = SmallMap::default();
assert_eq!(map.capacity(), 8);
let map: SmallMap<8, u32, String, RandomState> = SmallMap::default();
assert_eq!(map.capacity(), 8);
Source§

impl<const N: usize, K, V, S> Extend<(K, V)> for SmallMap<N, K, V, S>
where K: Eq + Hash, S: BuildHasher,

Source§

fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<const N: usize, K, V, S> FromIterator<(K, V)> for SmallMap<N, K, V, S>
where K: Eq + Hash, S: BuildHasher + Default,

Source§

fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl<const N: usize, K, V, S, Q> Index<&Q> for SmallMap<N, K, V, S>
where K: Eq + Hash, Q: ?Sized + Hash + Equivalent<K>, S: BuildHasher,

Source§

fn index(&self, key: &Q) -> &V

Returns a reference to the value corresponding to the supplied key.

§Panics

Panics if the key is not present in the SmallMap.

Source§

type Output = V

The returned type after indexing.
Source§

impl<'a, const N: usize, K, V, S> IntoIterator for &'a SmallMap<N, K, V, S>

Source§

fn into_iter(self) -> Iter<'a, N, K, V>

Creates an iterator over the entries of a HashMap in arbitrary order. The iterator element type is (&'a K, &'a V).

Source§

type Item = (&'a K, &'a V)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, N, K, V>

Which kind of iterator are we turning this into?
Source§

impl<const N: usize, K, V, S> IntoIterator for SmallMap<N, K, V, S>

Source§

fn into_iter(self) -> IntoIter<N, K, V>

Creates a consuming iterator, that is, one that moves each key-value pair out of the map in arbitrary order. The map cannot be used after calling this.

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<N, K, V>

Which kind of iterator are we turning this into?
Source§

impl<const N: usize, K, V, S> PartialEq for SmallMap<N, K, V, S>
where K: Eq + Hash, V: PartialEq, S: BuildHasher,

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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<const N: usize, K, V, S> Eq for SmallMap<N, K, V, S>
where K: Eq + Hash, V: Eq, S: BuildHasher,

Auto Trait Implementations§

§

impl<const N: usize, K, V, S> Freeze for SmallMap<N, K, V, S>
where S: Freeze, K: Freeze, V: Freeze,

§

impl<const N: usize, K, V, S> RefUnwindSafe for SmallMap<N, K, V, S>

§

impl<const N: usize, K, V, S> Send for SmallMap<N, K, V, S>
where S: Send, K: Send, V: Send,

§

impl<const N: usize, K, V, S> Sync for SmallMap<N, K, V, S>
where S: Sync, K: Sync, V: Sync,

§

impl<const N: usize, K, V, S> Unpin for SmallMap<N, K, V, S>
where S: Unpin, K: Unpin, V: Unpin,

§

impl<const N: usize, K, V, S> UnwindSafe for SmallMap<N, K, V, S>
where S: UnwindSafe, 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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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.