FrozenMap

Struct FrozenMap 

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

Source

pub fn new() -> Self

Creates an empty FrozenMap. Does not perform any heap allocations.

Source§

impl<K, V> FrozenMap<K, V>
where K: Ord,

Source

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.

Source

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>

Source

pub const fn len(&self) -> usize

Returns the number of elements in the map.

Source

pub const fn is_empty(&self) -> bool

Returns true if the map contains no elements.

Source

pub const fn as_slice(&self) -> &[(K, V)]

Returns a reference to the underlying sorted slice.

Source

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

Returns a reference to the value corresponding to the key.

Source

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

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

Source

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

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

Source

pub fn first_key_value(&self) -> Option<(&K, &V)>

Returns the first key-value pair in the map.

Source

pub fn last_key_value(&self) -> Option<(&K, &V)>

Returns the last key-value pair in the map.

Source

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

Gets an iterator over the entries of the map, sorted by key.

Source

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

Gets an iterator over the keys of the map, in sorted order.

Source

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

Gets an iterator over the values of the map, in order by key.

Source

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

Creates a consuming iterator visiting all the keys, in sorted order.

Source

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

Creates a consuming iterator visiting all the values, in order by key.

Source

pub fn range<T, R>(&self, range: R) -> Range<'_, K, V>
where T: Ord + ?Sized, K: Borrow<T> + Ord, R: RangeBounds<T>,

Constructs a double-ended iterator over a sub-range of entries in the map.

Source

pub fn extend(&self, entries: impl IntoIterator<Item = (K, V)>) -> Self
where K: Clone + Ord, V: Clone,

Extend this FrozenMap by constructing a new map with the additional entries. New entries with overlapping keys will overwrite existing ones.

Trait Implementations§

Source§

impl<K, V> AsRef<[(K, V)]> for FrozenMap<K, V>

Source§

fn as_ref(&self) -> &[(K, V)]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

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>

Attempt to decode this type with the given BorrowDecode.
Source§

impl<K: Clone, V: Clone> Clone for FrozenMap<K, V>

Source§

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

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: Debug, V: Debug> Debug for FrozenMap<K, V>

Source§

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

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

impl<K, V, __Context> Decode<__Context> for FrozenMap<K, V>
where K: Decode<__Context> + 'static, V: Decode<__Context> + 'static,

Source§

fn decode<__D: Decoder<Context = __Context>>( decoder: &mut __D, ) -> Result<Self, DecodeError>

Attempt to decode this type with the given Decode.
Source§

impl<K, V> Default for FrozenMap<K, V>

Source§

fn default() -> Self

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

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

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

impl<K, V> Encode for FrozenMap<K, V>
where K: Encode, V: Encode,

Source§

fn encode<__E: Encoder>(&self, encoder: &mut __E) -> Result<(), EncodeError>

Encode a given type.
Source§

impl<K, V> From<&[(K, V)]> for FrozenMap<K, V>
where K: Ord + Clone, V: Clone,

Source§

fn from(entries: &[(K, V)]) -> Self

Creates a FrozenMap from a slice of key-value pairs. Keys and values are cloned.

If there are overlapping keys, the last entry for each key is kept.

Source§

impl<K: Ord, V, const N: usize> From<[(K, V); N]> for FrozenMap<K, V>

Source§

fn from(entries: [(K, V); N]) -> Self

Creates a FrozenMap from an owned array of key-value pairs.

If there are overlapping keys, the last entry for each key is kept.

Source§

impl<K, V> From<BTreeMap<K, V>> for FrozenMap<K, V>

Source§

fn from(map: BTreeMap<K, V>) -> Self

Creates a FrozenMap from a BTreeMap.

This is more efficient than From<HashMap<K, V>> because BTreeMap already iterates in sorted order, so no re-sorting is needed.

Source§

impl<K: Ord, V> From<Box<[(K, V)]>> for FrozenMap<K, V>

Source§

fn from(entries: Box<[(K, V)]>) -> Self

Creates a FrozenMap from a boxed slice of key-value pairs.

If there are overlapping keys, the last entry for each key is kept.

Source§

impl<K, V> From<FrozenMap<K, V>> for Box<[(K, V)]>

Source§

fn from(map: FrozenMap<K, V>) -> Self

Converts to this type from the input type.
Source§

impl<K, V, S> From<HashMap<K, V, S>> for FrozenMap<K, V>
where K: Ord, S: BuildHasher,

Source§

fn from(map: HashMap<K, V, S>) -> Self

Creates a FrozenMap from a HashMap.

The entries are sorted by key during construction.

Source§

impl<K, V, S> From<IndexMap<K, V, S>> for FrozenMap<K, V>
where K: Ord, S: BuildHasher,

Source§

fn from(map: IndexMap<K, V, S>) -> Self

Creates a FrozenMap from an [IndexMap].

The entries are sorted by key during construction.

Source§

impl<K: Ord, V> From<Vec<(K, V)>> for FrozenMap<K, V>

Source§

fn from(entries: Vec<(K, V)>) -> Self

Creates a FrozenMap from a Vec of key-value pairs.

If there are overlapping keys, the last entry for each key is kept.

Source§

impl<K: Ord, V> FromIterator<(K, V)> for FrozenMap<K, V>

Source§

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

Creates a FrozenMap from an iterator of key-value pairs.

If there are overlapping keys, the last entry for each key is kept.

Source§

impl<K: Hash, V: Hash> Hash for FrozenMap<K, V>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<K, Q, V> Index<&Q> for FrozenMap<K, V>
where K: Borrow<Q> + Ord, Q: Ord + ?Sized,

Source§

type Output = V

The returned type after indexing.
Source§

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

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

impl<'a, K, V> IntoIterator for &'a FrozenMap<K, V>

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) -> Iter<'a, K, V>

Creates an iterator from a value. Read more
Source§

impl<K, V> IntoIterator for FrozenMap<K, V>

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) -> IntoIter<K, V>

Creates an iterator from a value. Read more
Source§

impl<K: Ord, V: Ord> Ord for FrozenMap<K, V>

Source§

fn cmp(&self, other: &FrozenMap<K, V>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<K: PartialEq, V: PartialEq> PartialEq for FrozenMap<K, V>

Source§

fn eq(&self, other: &FrozenMap<K, V>) -> 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: PartialOrd, V: PartialOrd> PartialOrd for FrozenMap<K, V>

Source§

fn partial_cmp(&self, other: &FrozenMap<K, V>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<K, V> Serialize for FrozenMap<K, V>
where K: Serialize, V: Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

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

impl<K: Eq, V: Eq> Eq for FrozenMap<K, V>

Source§

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>

§

impl<K, V> Send for FrozenMap<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for FrozenMap<K, V>
where K: Sync, V: Sync,

§

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

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

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