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: Hash + Equivalent<K> + ?Sized,

Source

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

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

Source

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

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

Source

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

Source

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

Source

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

Source§

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

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

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

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<'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?

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