pub struct FrozenMap<K, V> { /* private fields */ }Expand description
A compact frozen (immutable) ordered map backed by a sorted boxed slice.
This is a read-only map that stores key-value pairs in a contiguous, sorted array. It provides efficient sorted iteration and binary search lookups, but cannot be modified after construction.
§Construction
If you’re building a new map, and you don’t expect many overlapping keys, consider pushing
entries into a Vec<(K, V)> and calling FrozenMap::from. It is typically cheaper to
collect into a Vec and sort the entries once at the end than it is to maintain a temporary
map data structure.
If you already have a map, need to perform lookups during construction, or you have many
overlapping keys that you don’t want to temporarily hold onto, you can use the provided From
trait implementations to create a FrozenMap from one of many common collections. You should
prefer using a BTreeMap, as it matches the sorted iteration order of FrozenMap and
avoids a sort operation during conversion.
If you don’t have an existing collection, you can use the FromIterator<(K, V)> trait
implementation to .collect() tuples into a FrozenMap.
Finally, if you have a list of pre-sorted tuples with unique keys, you can use the advanced
FrozenMap::from_unique_sorted_box or FrozenMap::from_unique_sorted_box_unchecked
constructors, which provide the cheapest possible construction.
Overlapping keys encountered during construction preserve the last overlapping entry, matching similar behavior for other maps in the standard library.
Implementations§
Source§impl<K, V> FrozenMap<K, V>where
K: Ord,
impl<K, V> FrozenMap<K, V>where
K: Ord,
Sourcepub fn from_unique_sorted_box(entries: Box<[(K, V)]>) -> Self
pub fn from_unique_sorted_box(entries: Box<[(K, V)]>) -> Self
Creates a FrozenMap from a pre-sorted boxed slice with unique keys.
Panics if the keys in entries are not unique and sorted.
Sourcepub fn from_unique_sorted_box_unchecked(entries: Box<[(K, V)]>) -> Self
pub fn from_unique_sorted_box_unchecked(entries: Box<[(K, V)]>) -> Self
Creates a FrozenMap from a pre-sorted boxed slice with unique keys.
§Correctness
The caller must ensure that:
- The entries are sorted by key in ascending order according to
K: Ord - There are no overlapping keys
If these invariants are not upheld, the map will behave incorrectly (e.g.,
FrozenMap::get may fail to find keys that are present), but no memory unsafety will
occur.
When debug_assertions is enabled, this will panic if an invariant is not upheld.
Source§impl<K, V> FrozenMap<K, V>
impl<K, V> FrozenMap<K, V>
Sourcepub fn get<Q>(&self, key: &Q) -> Option<&V>
pub fn get<Q>(&self, key: &Q) -> Option<&V>
Returns a reference to the value corresponding to the key.
Sourcepub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
Returns the key-value pair corresponding to the supplied key.
Sourcepub fn contains_key<Q>(&self, key: &Q) -> bool
pub fn contains_key<Q>(&self, key: &Q) -> bool
Returns true if the map contains a value for the specified key.
Sourcepub fn first_key_value(&self) -> Option<(&K, &V)>
pub fn first_key_value(&self) -> Option<(&K, &V)>
Returns the first key-value pair in the map.
Sourcepub fn last_key_value(&self) -> Option<(&K, &V)>
pub fn last_key_value(&self) -> Option<(&K, &V)>
Returns the last key-value pair in the map.
Sourcepub fn iter(&self) -> Iter<'_, K, V> ⓘ
pub fn iter(&self) -> Iter<'_, K, V> ⓘ
Gets an iterator over the entries of the map, sorted by key.
Sourcepub fn keys(&self) -> Keys<'_, K, V> ⓘ
pub fn keys(&self) -> Keys<'_, K, V> ⓘ
Gets an iterator over the keys of the map, in sorted order.
Sourcepub fn values(&self) -> Values<'_, K, V> ⓘ
pub fn values(&self) -> Values<'_, K, V> ⓘ
Gets an iterator over the values of the map, in order by key.
Sourcepub fn into_keys(self) -> IntoKeys<K, V> ⓘ
pub fn into_keys(self) -> IntoKeys<K, V> ⓘ
Creates a consuming iterator visiting all the keys, in sorted order.
Sourcepub fn into_values(self) -> IntoValues<K, V> ⓘ
pub fn into_values(self) -> IntoValues<K, V> ⓘ
Creates a consuming iterator visiting all the values, in order by key.
Trait Implementations§
Source§impl<'__de, K, V, __Context> BorrowDecode<'__de, __Context> for FrozenMap<K, V>where
K: BorrowDecode<'__de, __Context> + '__de,
V: BorrowDecode<'__de, __Context> + '__de,
impl<'__de, K, V, __Context> BorrowDecode<'__de, __Context> for FrozenMap<K, V>where
K: BorrowDecode<'__de, __Context> + '__de,
V: BorrowDecode<'__de, __Context> + '__de,
Source§fn borrow_decode<__D: BorrowDecoder<'__de, Context = __Context>>(
decoder: &mut __D,
) -> Result<Self, DecodeError>
fn borrow_decode<__D: BorrowDecoder<'__de, Context = __Context>>( decoder: &mut __D, ) -> Result<Self, DecodeError>
Source§impl<'de, K, V> Deserialize<'de> for FrozenMap<K, V>where
K: Deserialize<'de>,
V: Deserialize<'de>,
impl<'de, K, V> Deserialize<'de> for FrozenMap<K, V>where
K: Deserialize<'de>,
V: Deserialize<'de>,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<'a, K, V> IntoIterator for &'a FrozenMap<K, V>
impl<'a, K, V> IntoIterator for &'a FrozenMap<K, V>
Source§impl<K, V> IntoIterator for FrozenMap<K, V>
impl<K, V> IntoIterator for FrozenMap<K, V>
Source§impl<K: Ord, V: Ord> Ord for FrozenMap<K, V>
impl<K: Ord, V: Ord> Ord for FrozenMap<K, V>
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<K: PartialOrd, V: PartialOrd> PartialOrd for FrozenMap<K, V>
impl<K: PartialOrd, V: PartialOrd> PartialOrd for FrozenMap<K, V>
impl<K: Eq, V: Eq> Eq for FrozenMap<K, V>
impl<K, V> StructuralPartialEq for FrozenMap<K, V>
Auto Trait Implementations§
impl<K, V> Freeze for FrozenMap<K, V>
impl<K, V> RefUnwindSafe for FrozenMap<K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Send for FrozenMap<K, V>
impl<K, V> Sync for FrozenMap<K, V>
impl<K, V> Unpin for FrozenMap<K, V>
impl<K, V> UnwindSafe for FrozenMap<K, V>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,
§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.