Skip to main content

FzHashMap

Struct FzHashMap 

Source
pub struct FzHashMap<K, V, BH = RandomState> { /* private fields */ }
Expand description

A map optimized for fast read access with hashable keys.

A frozen collection differs from the traditional Rust collections, such as HashMap and HashSet types in three key ways. First, creating a frozen collection performs an analysis over the input data to determine the best implementation strategy. Depending on the situation, this analysis is performed at build time or runtime, and it can take a relatively long time when a collection is very large. Second, once created, the keys in frozen collections are immutable. And third, querying a frozen collection is typically considerably faster, which is the whole point.

This type requires that the keys implement the Eq and Hash traits. This can frequently be achieved by using #[derive(PartialEq, Eq, Hash)]. If you implement these yourself, it is important that the following property holds:

k1 == k2 -> hash(k1) == hash(k2)

In other words, if two keys are equal, their hashes must be equal. Violating this property is a logic error.

It is also a logic error for a key to be modified in such a way that the key’s hash, as determined by the Hash trait, or its equality, as determined by the Eq trait, changes while it is in the collection. This is normally only possible through core::cell::Cell, core::cell::RefCell, global state, I/O, or unsafe code.

The behavior resulting from either logic error can include panics, incorrect results, memory leaks, and non-termination.

§Alternate Choices

If your keys are integers or enum variants, you should use the FzScalarMap type instead. If your keys are strings, you should use the FzStringMap type instead. Both of these will deliver better performance since they are specifically optimized for those key types.

If your keys are known at compile time, consider using the various fz_*_map macros instead of this type as they generally perform better.

Implementations§

Source§

impl<K, V> FzHashMap<K, V>

Source

pub fn new(entries: Vec<(K, V)>) -> FzHashMap<K, V>
where K: Eq + Hash,

Creates a frozen map. If the input contains duplicate keys, the latter one in the input is used and all former ones are discarded.

Source§

impl<K, V, BH> FzHashMap<K, V, BH>
where BH: BuildHasher,

Source

pub fn with_hasher(entries: Vec<(K, V)>, bh: BH) -> FzHashMap<K, V, BH>
where K: Eq + Hash,

Creates a frozen map which uses the given hash builder to hash keys. If the input contains duplicate keys, the latter one in the input is used and all former ones are discarded.

Source

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

Returns a reference to the value corresponding to the key.

The key may be any borrowed form of the map’s key type.

Source

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

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

The key may be any borrowed form of the map’s key type.

Source

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

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

This is potentially useful:

  • for key types where non-identical keys can be considered equal;

  • for getting the &K stored key value from a borrowed &Q lookup key; or

  • for getting a reference to a key with the same lifetime as the collection.

  • The supplied key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Source

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

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

The key may be any borrowed form of the map’s key type.

Source

pub fn get_disjoint_mut<Q, const N: usize>( &mut self, keys: [&Q; N], ) -> [Option<&mut V>; N]
where Q: Hash + Eq + Equivalent<K> + ?Sized,

Gets multiple mutable values from the map.

§Panics

Panics if the same key is specified multiple times.

Source

pub unsafe fn get_disjoint_unchecked_mut<Q, const N: usize>( &mut self, keys: [&Q; N], ) -> [Option<&mut V>; N]
where Q: Hash + Equivalent<K> + ?Sized,

Gets multiple mutable values from the map.

§Safety

Calling this method with overlapping keys is undefined behavior even if the resulting references are not used.

Source

pub fn len(&self) -> usize

Returns the number of elements in the collection.

Source

pub fn is_empty(&self) -> bool

Returns true if the collection contains no elements.

Source

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

An iterator visiting all entries in arbitrary order.

Source

pub fn iter_mut(&mut self) -> IterMut<'_, K, V>

An iterator producing mutable references to all entries in arbitrary order.

Source

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

An iterator visiting all keys in arbitrary order.

Source

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

A consuming iterator visiting all keys in arbitrary order.

Source

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

An iterator visiting all values in arbitrary order.

Source

pub fn values_mut(&mut self) -> ValuesMut<'_, K, V>

An iterator visiting all values mutably in arbitrary order.

Source

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

A consuming iterator visiting all values in arbitrary order.

Trait Implementations§

Source§

impl<K, V, BH> Clone for FzHashMap<K, V, BH>
where K: Clone, V: Clone, BH: Clone,

Source§

fn clone(&self) -> FzHashMap<K, V, BH>

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<K, V, BH> Debug for FzHashMap<K, V, BH>
where K: Debug, V: Debug, BH: BuildHasher,

Source§

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

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

impl<K, V, BH> Default for FzHashMap<K, V, BH>
where BH: Default,

Source§

fn default() -> FzHashMap<K, V, BH>

Returns the “default value” for a type. Read more
Source§

impl<'de, K, V, BH> Deserialize<'de> for FzHashMap<K, V, BH>
where K: Deserialize<'de> + Hash + Eq, V: Deserialize<'de>, BH: BuildHasher + Default,

Available on crate feature serde only.
Source§

fn deserialize<D>( deserializer: D, ) -> Result<FzHashMap<K, V, BH>, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<K, V, const N: usize, BH> From<[(K, V); N]> for FzHashMap<K, V, BH>
where K: Eq + Hash, BH: BuildHasher + Default,

Source§

fn from(entries: [(K, V); N]) -> FzHashMap<K, V, BH>

Converts to this type from the input type.
Source§

impl<T, BH> From<FzHashMap<T, (), BH>> for FzHashSet<T, BH>
where T: Hash + Eq, BH: BuildHasher + Default,

Source§

fn from(map: FzHashMap<T, (), BH>) -> FzHashSet<T, BH>

Converts to this type from the input type.
Source§

impl<K, V, BH> FromIterator<(K, V)> for FzHashMap<K, V, BH>
where K: Eq + Hash, BH: BuildHasher + Default,

Source§

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

Creates a value from an iterator. Read more
Source§

impl<K, V, Q, BH> Index<&Q> for FzHashMap<K, V, BH>
where Q: Hash + Equivalent<K> + ?Sized, BH: BuildHasher,

Source§

type Output = V

The returned type after indexing.
Source§

fn index(&self, index: &Q) -> &<FzHashMap<K, V, BH> as Index<&Q>>::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a, K, V, BH> IntoIterator for &'a FzHashMap<K, V, BH>
where BH: BuildHasher,

Source§

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

The type of the elements being iterated over.
Source§

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

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

fn into_iter(self) -> <&'a FzHashMap<K, V, BH> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, K, V, BH> IntoIterator for &'a mut FzHashMap<K, V, BH>
where BH: BuildHasher,

Source§

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

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, K, V>

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

fn into_iter(self) -> <&'a mut FzHashMap<K, V, BH> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K, V, BH> IntoIterator for FzHashMap<K, V, BH>
where BH: BuildHasher,

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<K, V>

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

fn into_iter(self) -> <FzHashMap<K, V, BH> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K, V, BH> Len for FzHashMap<K, V, BH>
where BH: BuildHasher,

Source§

fn len(&self) -> usize

Returns the number of elements in the collection.
Source§

fn is_empty(&self) -> bool

Returns true if the collection contains no elements.
Source§

impl<K, V, Q, BH> MapExtras<K, V, Q> for FzHashMap<K, V, BH>
where Q: Hash + Equivalent<K> + ?Sized, BH: BuildHasher,

Source§

fn get_key_value(&self, key: &Q) -> Option<(&K, &V)>

Returns the key-value pair corresponding to the supplied key. Read more
Source§

fn get_disjoint_mut<const N: usize>( &mut self, keys: [&Q; N], ) -> [Option<&mut V>; N]
where Q: Eq,

Gets multiple mutable values from the map. Read more
Source§

unsafe fn get_disjoint_unchecked_mut<const N: usize>( &mut self, keys: [&Q; N], ) -> [Option<&mut V>; N]

Safety Read more
Source§

impl<K, V, BH> MapIteration<K, V> for FzHashMap<K, V, BH>
where BH: BuildHasher,

Source§

type Iterator<'a> = Iter<'a, K, V> where K: 'a, V: 'a, BH: 'a

The type of the iterator returned by Self::iter.
Source§

type KeyIterator<'a> = Keys<'a, K, V> where K: 'a, V: 'a, BH: 'a

The type of the iterator returned by Self::keys.
Source§

type ValueIterator<'a> = Values<'a, K, V> where K: 'a, V: 'a, BH: 'a

The type of the iterator returned by Self::values.
Source§

type MutIterator<'a> = IterMut<'a, K, V> where K: 'a, V: 'a, BH: 'a

The type of the mutable iterator returned by Self::iter_mut.
Source§

type ValueMutIterator<'a> = ValuesMut<'a, K, V> where K: 'a, V: 'a, BH: 'a

The type of the mutable iterator returned by Self::values_mut.
Source§

type IntoKeyIterator = IntoKeys<K, V>

The type of the iterator returned by Self::into_keys.
Source§

type IntoValueIterator = IntoValues<K, V>

The type of the iterator returned by Self::into_values.
Source§

fn iter(&self) -> <FzHashMap<K, V, BH> as MapIteration<K, V>>::Iterator<'_>

An iterator visiting all entries in arbitrary order.
Source§

fn iter_mut( &mut self, ) -> <FzHashMap<K, V, BH> as MapIteration<K, V>>::MutIterator<'_>

An iterator producing mutable references to all entries in arbitrary order.
Source§

fn keys(&self) -> <FzHashMap<K, V, BH> as MapIteration<K, V>>::KeyIterator<'_>

An iterator visiting all keys in arbitrary order.
Source§

fn into_keys( self, ) -> <FzHashMap<K, V, BH> as MapIteration<K, V>>::IntoKeyIterator

A consuming iterator visiting all keys in arbitrary order.
Source§

fn values( &self, ) -> <FzHashMap<K, V, BH> as MapIteration<K, V>>::ValueIterator<'_>

An iterator visiting all values in arbitrary order.
Source§

fn values_mut( &mut self, ) -> <FzHashMap<K, V, BH> as MapIteration<K, V>>::ValueMutIterator<'_>

An iterator visiting all values mutably in arbitrary order.
Source§

fn into_values( self, ) -> <FzHashMap<K, V, BH> as MapIteration<K, V>>::IntoValueIterator

A consuming iterator visiting all values in arbitrary order.
Source§

impl<K, V, Q, BH> MapQuery<Q, V> for FzHashMap<K, V, BH>
where Q: Hash + Equivalent<K> + ?Sized, BH: BuildHasher,

Source§

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

Returns a reference to the value corresponding to the key. Read more
Source§

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

Returns a mutable reference to the value corresponding to the key. Read more
Source§

fn contains_key(&self, key: &Q) -> bool

Returns true if the map contains a value for the specified key. Read more
Source§

impl<K, V, MT, BH> PartialEq<MT> for FzHashMap<K, V, BH>
where K: PartialEq + Hash, V: PartialEq, MT: MapQuery<K, V>, BH: BuildHasher,

Source§

fn eq(&self, other: &MT) -> 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<K, V, BH> Serialize for FzHashMap<K, V, BH>
where K: Serialize, V: Serialize, BH: BuildHasher,

Available on crate feature serde only.
Source§

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<K, V, BH> Eq for FzHashMap<K, V, BH>
where K: Eq + Hash, V: Eq, BH: BuildHasher,

Source§

impl<K, V, Q, BH> Map<K, V, Q> for FzHashMap<K, V, BH>
where Q: Hash + Equivalent<K> + ?Sized, BH: BuildHasher,

Auto Trait Implementations§

§

impl<K, V, BH> Freeze for FzHashMap<K, V, BH>
where BH: Freeze,

§

impl<K, V, BH> RefUnwindSafe for FzHashMap<K, V, BH>

§

impl<K, V, BH> Send for FzHashMap<K, V, BH>
where BH: Send, K: Send, V: Send,

§

impl<K, V, BH> Sync for FzHashMap<K, V, BH>
where BH: Sync, K: Sync, V: Sync,

§

impl<K, V, BH> Unpin for FzHashMap<K, V, BH>
where BH: Unpin,

§

impl<K, V, BH> UnsafeUnpin for FzHashMap<K, V, BH>
where BH: UnsafeUnpin,

§

impl<K, V, BH> UnwindSafe for FzHashMap<K, V, BH>
where BH: 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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

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

Compare self to key and return true if they are equal.
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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,