Struct LiteMap
struct LiteMap<K: ?Sized, V: ?Sized, S = alloc::vec::Vec<(K, V)>> { ... }
A simple "flat" map based on a sorted vector
See the [module level documentation][super] for why one should use this.
The API is roughly similar to that of std::collections::BTreeMap.
Implementations
impl<'a, K, V> LiteMap<K, V, &'a [(K, V)]>
const fn const_len(self: &Self) -> usizeConst version of [
LiteMap::len()] for a slice store.Note: This function will no longer be needed if const trait behavior is stabilized.
Examples
use LiteMap; static map: = from_sorted_store_unchecked; static len: usize = map.const_len; assert_eq!;const fn const_is_empty(self: &Self) -> boolConst version of [
LiteMap::is_empty()] for a slice store.Note: This function will no longer be needed if const trait behavior is stabilized.
Examples
use LiteMap; static map: = from_sorted_store_unchecked; static is_empty: bool = map.const_is_empty; assert!;const fn const_get_indexed_or_panic(self: &Self, index: usize) -> &'a (K, V)Const version of [
LiteMap::get_indexed()] for a slice store.Note: This function will no longer be needed if const trait behavior is stabilized.
Panics
Panics if the index is out of bounds.
Examples
use LiteMap; static map: = from_sorted_store_unchecked; static t: & = map.const_get_indexed_or_panic; assert_eq!; assert_eq!;
impl<'a, K: 'a, V: 'a, S> LiteMap<K, V, S>
fn iter_mut(self: &'a mut Self) -> impl DoubleEndedIterator<Item = (&'a K, &'a mut V)>Produce an ordered mutable iterator over key-value pairs
impl<'a, K: 'a, V: 'a, S> LiteMap<K, V, S>
fn to_borrowed_keys_values<KB: ?Sized, VB: ?Sized, SB>(self: &'a Self) -> LiteMap<&'a KB, &'a VB, SB> where K: Borrow<KB>, V: Borrow<VB>, SB: StoreMut<&'a KB, &'a VB>Returns a new
LiteMapwith keys and values borrowed from this one.Examples
use LiteMap; let mut map: = new_vec; map.insert; map.insert; let borrowed_map: = map.to_borrowed_keys_values; assert_eq!;fn to_borrowed_keys<KB: ?Sized, SB>(self: &'a Self) -> LiteMap<&'a KB, V, SB> where K: Borrow<KB>, V: Clone, SB: StoreMut<&'a KB, V>Returns a new
LiteMapwith keys borrowed from this one and cloned values.Examples
use LiteMap; let mut map: = new_vec; map.insert; map.insert; let borrowed_map: = map.to_borrowed_keys; assert_eq!;fn to_borrowed_values<VB: ?Sized, SB>(self: &'a Self) -> LiteMap<K, &'a VB, SB> where K: Clone, V: Borrow<VB>, SB: StoreMut<K, &'a VB>Returns a new
LiteMapwith values borrowed from this one and cloned keys.Examples
use LiteMap; let mut map: = new_vec; map.insert; map.insert; let borrowed_map: = map.to_borrowed_values; assert_eq!;
impl<'a, K: 'a, V: 'a, S> LiteMap<K, V, S>
fn iter(self: &'a Self) -> impl DoubleEndedIterator<Item = (&'a K, &'a V)>Produce an ordered iterator over key-value pairs
fn iter_keys(self: &'a Self) -> impl DoubleEndedIterator<Item = &'a K>Produce an ordered iterator over keys
fn iter_values(self: &'a Self) -> impl DoubleEndedIterator<Item = &'a V>Produce an iterator over values, ordered by their keys
fn keys(self: &'a Self) -> impl DoubleEndedIterator<Item = &'a K>Produce an ordered iterator over keys
fn values(self: &'a Self) -> impl DoubleEndedIterator<Item = &'a V>Produce an iterator over values, ordered by their keys
impl<'a, V> LiteMap<&'a [u8], V, &'a [(&'a [u8], V)]>
const fn const_get_with_index(self: &Self, key: &[u8]) -> Option<(usize, &'a V)>Const function to get the value associated with a
&[u8]key, if it exists.Also returns the index of the value.
Note: This function will no longer be needed if const trait behavior is stabilized.
Examples
use LiteMap; static map: = from_sorted_store_unchecked; static d: = map.const_get_with_index; assert_eq!; static n: = map.const_get_with_index; assert_eq!;
impl<'a, V> LiteMap<&'a str, V, &'a [(&'a str, V)]>
const fn const_get_with_index(self: &Self, key: &str) -> Option<(usize, &'a V)>Const function to get the value associated with a
&strkey, if it exists.Also returns the index of the value.
Note: This function will no longer be needed if const trait behavior is stabilized.
Examples
use LiteMap; static map: = from_sorted_store_unchecked; static d: = map.const_get_with_index; assert_eq!; static n: = map.const_get_with_index; assert_eq!;
impl<'a, V> LiteMap<i128, V, &'a [(i128, V)]>
const fn const_get_with_index(self: &Self, key: i128) -> Option<(usize, &'a V)>Const function to get the value associated with an integer key, if it exists.
Note: This function will no longer be needed if const trait behavior is stabilized.
Also returns the index of the value.
impl<'a, V> LiteMap<i16, V, &'a [(i16, V)]>
const fn const_get_with_index(self: &Self, key: i16) -> Option<(usize, &'a V)>Const function to get the value associated with an integer key, if it exists.
Note: This function will no longer be needed if const trait behavior is stabilized.
Also returns the index of the value.
impl<'a, V> LiteMap<i32, V, &'a [(i32, V)]>
const fn const_get_with_index(self: &Self, key: i32) -> Option<(usize, &'a V)>Const function to get the value associated with an integer key, if it exists.
Note: This function will no longer be needed if const trait behavior is stabilized.
Also returns the index of the value.
impl<'a, V> LiteMap<i64, V, &'a [(i64, V)]>
const fn const_get_with_index(self: &Self, key: i64) -> Option<(usize, &'a V)>Const function to get the value associated with an integer key, if it exists.
Note: This function will no longer be needed if const trait behavior is stabilized.
Also returns the index of the value.
impl<'a, V> LiteMap<i8, V, &'a [(i8, V)]>
const fn const_get_with_index(self: &Self, key: i8) -> Option<(usize, &'a V)>Const function to get the value associated with an integer key, if it exists.
Note: This function will no longer be needed if const trait behavior is stabilized.
Also returns the index of the value.
impl<'a, V> LiteMap<isize, V, &'a [(isize, V)]>
const fn const_get_with_index(self: &Self, key: isize) -> Option<(usize, &'a V)>Const function to get the value associated with an integer key, if it exists.
Note: This function will no longer be needed if const trait behavior is stabilized.
Also returns the index of the value.
impl<'a, V> LiteMap<u128, V, &'a [(u128, V)]>
const fn const_get_with_index(self: &Self, key: u128) -> Option<(usize, &'a V)>Const function to get the value associated with an integer key, if it exists.
Note: This function will no longer be needed if const trait behavior is stabilized.
Also returns the index of the value.
impl<'a, V> LiteMap<u16, V, &'a [(u16, V)]>
const fn const_get_with_index(self: &Self, key: u16) -> Option<(usize, &'a V)>Const function to get the value associated with an integer key, if it exists.
Note: This function will no longer be needed if const trait behavior is stabilized.
Also returns the index of the value.
impl<'a, V> LiteMap<u32, V, &'a [(u32, V)]>
const fn const_get_with_index(self: &Self, key: u32) -> Option<(usize, &'a V)>Const function to get the value associated with an integer key, if it exists.
Note: This function will no longer be needed if const trait behavior is stabilized.
Also returns the index of the value.
impl<'a, V> LiteMap<u64, V, &'a [(u64, V)]>
const fn const_get_with_index(self: &Self, key: u64) -> Option<(usize, &'a V)>Const function to get the value associated with an integer key, if it exists.
Note: This function will no longer be needed if const trait behavior is stabilized.
Also returns the index of the value.
impl<'a, V> LiteMap<u8, V, &'a [(u8, V)]>
const fn const_get_with_index(self: &Self, key: u8) -> Option<(usize, &'a V)>Const function to get the value associated with an integer key, if it exists.
Note: This function will no longer be needed if const trait behavior is stabilized.
Also returns the index of the value.
impl<'a, V> LiteMap<usize, V, &'a [(usize, V)]>
const fn const_get_with_index(self: &Self, key: usize) -> Option<(usize, &'a V)>Const function to get the value associated with an integer key, if it exists.
Note: This function will no longer be needed if const trait behavior is stabilized.
Also returns the index of the value.
impl<K, V> LiteMap<K, V>
const fn new_vec() -> SelfConstruct a new
LiteMapbacked by Vec
impl<K, V> LiteMap<K, V, Vec<(K, V)>>
fn into_tuple_vec(self: Self) -> Vec<(K, V)>Convert a
LiteMapinto a sortedVec<(K, V)>.
impl<K, V, S> LiteMap<K, V, S>
fn entry(self: &mut Self, key: K) -> Entry<'_, K, V, S>Gets the entry for the given key in the map for in-place manipulation.
impl<K, V, S> LiteMap<K, V, S>
fn extend_from_litemap(self: &mut Self, other: Self) -> Option<Self>Insert all elements from
otherinto thisLiteMap.If
othercontains keys that already exist inself, the values inotherreplace the corresponding ones inself, and the rejected items fromselfare returned as a newLiteMap. Otherwise,Noneis returned.The implementation of this function is optimized if
selfandotherhave no overlap.Examples
use LiteMap; let mut map1 = new_vec; map1.insert; map1.insert; let mut map2 = new_vec; map2.insert; map2.insert; let leftovers = map1.extend_from_litemap; assert_eq!; assert_eq!; assert_eq!; assert_eq!; let map3 = leftovers.expect; assert_eq!; assert_eq!;
impl<K, V, S> LiteMap<K, V, S>
fn get_mut<Q>(self: &mut Self, key: &Q) -> Option<&mut V> where K: Borrow<Q>, Q: Ord + ?SizedGet the value associated with
key, if it exists, as a mutable reference.use LiteMap; let mut map = new_vec; map.insert; map.insert; if let Some = map.get_mut assert_eq!;fn try_append(self: &mut Self, key: K, value: V) -> Option<(K, V)>Appends
valuewithkeyto the end of the underlying vector, returningkeyandvalueif it failed. Useful for extending with an existing sorted list.use LiteMap; let mut map = new_vec; assert!; assert!; assert!; assert!; assert_eq!; // contains the original value for the key: 3 assert_eq!; // not appended since it wasn't in order assert_eq!;fn insert(self: &mut Self, key: K, value: V) -> Option<V>Insert
valuewithkey, returning the existing value if it exists.use LiteMap; let mut map = new_vec; map.insert; map.insert; assert_eq!; assert_eq!;fn try_insert(self: &mut Self, key: K, value: V) -> Option<(K, V)>Attempts to insert a unique entry into the map.
If
keyis not already in the map, inserts it with the correspondingvalueand returnsNone.If
keyis already in the map, no change is made to the map, and the key and value are returned back to the caller.use LiteMap; let mut map = new_vec; map.insert; map.insert; // 2 is not yet in the map... assert_eq!; assert_eq!; // ...but now it is. assert_eq!; assert_eq!;fn try_get_or_insert<E, impl FnOnce(&K) -> Result<V, E>: FnOnce(&K) -> Result<V, E>>(self: &mut Self, key: K, value: impl FnOnce(&K) -> Result<V, E>) -> Result<(usize, &V), E>Attempts to insert a unique entry into the map.
If
keyis not already in the map, invokes the closure to computevalue, inserts the pair into the map, and returns a reference to the value. The closure is passed a reference to thekeyargument.If
keyis already in the map, a reference to the existing value is returned.Additionally, the index of the value in the map is returned. If it is not desirable to hold on to the mutable reference's lifetime, the index can be used to access the element via [
LiteMap::get_indexed()].The closure returns a
Resultto allow for a fallible insertion function. If the creation ofvalueis infallible, you can usecore::convert::Infallible.use LiteMap; /// Helper function to unwrap an `Infallible` result from the insertion function let mut map = new_vec; map.insert; map.insert; // 2 is not yet in the map... let result1 = unwrap_infallible; assert_eq!; assert_eq!; // ...but now it is. let result1 = unwrap_infallible; assert_eq!; assert_eq!;fn remove<Q>(self: &mut Self, key: &Q) -> Option<V> where K: Borrow<Q>, Q: Ord + ?SizedRemove the value at
key, returning it if it exists.use LiteMap; let mut map = new_vec; map.insert; map.insert; assert_eq!; assert_eq!;
impl<K, V, S> LiteMap<K, V, S>
const fn from_sorted_store_unchecked(values: S) -> SelfConstruct a new
LiteMapusing the given valuesThe store must be sorted and have no duplicate keys.
impl<K, V, S> LiteMap<K, V, S>
fn retain<F>(self: &mut Self, predicate: F) where F: FnMut(&K, &V) -> boolRetains only the elements specified by the predicate.
In other words, remove all elements such that
f((&k, &v))returnsfalse.Example
use LiteMap; let mut map = new_vec; map.insert; map.insert; map.insert; // Retain elements with odd keys map.retain; assert_eq!; assert_eq!;
impl<K, V, S> LiteMap<K, V, S>
fn with_capacity(capacity: usize) -> SelfConstruct a new
LiteMapwith a given capacityfn clear(self: &mut Self)Remove all elements from the
LiteMapfn reserve(self: &mut Self, additional: usize)Reserve capacity for
additionalmore elements to be inserted into theLiteMapto avoid frequent reallocations.See
Vec::reserve()for more information.
impl<K, V: ?Sized, S> LiteMap<K, V, S>
fn get<Q>(self: &Self, key: &Q) -> Option<&V> where K: Borrow<Q>, Q: Ord + ?SizedGet the value associated with
key, if it exists.use LiteMap; let mut map = new_vec; map.insert; map.insert; assert_eq!; assert_eq!;fn get_by<impl FnMut(&K) -> Ordering: FnMut(&K) -> Ordering>(self: &Self, predicate: impl FnMut(&K) -> Ordering) -> Option<&V>Binary search the map with
predicateto find a key, returning the value.fn contains_key<Q>(self: &Self, key: &Q) -> bool where K: Borrow<Q>, Q: Ord + ?SizedReturns whether
keyis contained in this mapuse LiteMap; let mut map = new_vec; map.insert; map.insert; assert!; assert!;fn find_index<Q>(self: &Self, key: &Q) -> Result<usize, usize> where K: Borrow<Q>, Q: Ord + ?SizedObtain the index for a given key, or if the key is not found, the index at which it would be inserted.
(The return value works equivalently to [
slice::binary_search_by()])The indices returned can be used with [
Self::get_indexed()]. Prefer using [Self::get()] directly where possible.
impl<K: ?Sized, V: ?Sized, S> LiteMap<K, V, S>
fn get_indexed_range(self: &Self, range: Range<usize>) -> Option<LiteMap<K, V, &<S as >::Slice>>Creates a new
LiteMapfrom a range of the currentLiteMap.Examples
use LiteMap; let mut map = new_vec; map.insert; map.insert; map.insert; let mut sub_map = map.get_indexed_range.expect; assert_eq!; assert_eq!; assert_eq!;fn as_sliced(self: &Self) -> LiteMap<K, V, &<S as >::Slice>Borrows this
LiteMapas one of its slice type.This can be useful in situations where you need a
LiteMapby value but do not want to clone the owned version.Examples
use LiteMap; let mut map = new_vec; map.insert; map.insert; let borrowed_map = map.as_sliced; assert_eq!; assert_eq!;fn as_slice(self: &Self) -> &<S as >::SliceBorrows the backing buffer of this
LiteMapas its slice type.The slice will be sorted.
Examples
use LiteMap; let mut map = new_vec; map.insert; map.insert; let slice = map.as_slice; assert_eq!;
impl<K: ?Sized, V: ?Sized, S> LiteMap<K, V, S>
const fn new() -> SelfCreate a new empty
LiteMap
impl<K: ?Sized, V: ?Sized, S> LiteMap<K, V, S>
fn len(self: &Self) -> usizeThe number of elements in the
LiteMapfn is_empty(self: &Self) -> boolWhether the
LiteMapis emptyfn get_indexed(self: &Self, index: usize) -> Option<(&K, &V)>Get the key-value pair residing at a particular index
In most cases, prefer [
LiteMap::get()] over this method.fn first(self: &Self) -> Option<(&K, &V)>Get the lowest-rank key/value pair from the
LiteMap, if it exists.Examples
use LiteMap; let mut map = from_iter; assert_eq!;fn last(self: &Self) -> Option<(&K, &V)>Get the highest-rank key/value pair from the
LiteMap, if it exists.Examples
use LiteMap; let mut map = from_iter; assert_eq!;fn to_boxed_keys_values<KB: ?Sized, VB: ?Sized, SB>(self: &Self) -> LiteMap<Box<KB>, Box<VB>, SB> where SB: StoreMut<Box<KB>, Box<VB>>, K: Borrow<KB>, V: Borrow<VB>, Box<KB>: for<'a> From<&'a KB>, Box<VB>: for<'a> From<&'a VB>Returns a new
LiteMapwith owned keys and values.The trait bounds allow transforming most slice and string types.
Examples
use LiteMap; let mut map: = new_vec; map.insert; map.insert; let boxed_map: = map.to_boxed_keys_values; assert_eq!;fn to_boxed_keys<KB: ?Sized, SB>(self: &Self) -> LiteMap<Box<KB>, V, SB> where V: Clone, SB: StoreMut<Box<KB>, V>, K: Borrow<KB>, Box<KB>: for<'a> From<&'a KB>Returns a new
LiteMapwith owned keys and cloned values.The trait bounds allow transforming most slice and string types.
Examples
use LiteMap; let mut map: = new_vec; map.insert; map.insert; let boxed_map: = map.to_boxed_keys; assert_eq!;fn to_boxed_values<VB: ?Sized, SB>(self: &Self) -> LiteMap<K, Box<VB>, SB> where K: Clone, SB: StoreMut<K, Box<VB>>, V: Borrow<VB>, Box<VB>: for<'a> From<&'a VB>Returns a new
LiteMapwith cloned keys and owned values.The trait bounds allow transforming most slice and string types.
Examples
use LiteMap; let mut map: = new_vec; map.insert; map.insert; let boxed_map: = map.to_boxed_values; assert_eq!;
impl<K, V, S> Default for LiteMap<K, V, S>
fn default() -> Self
impl<K, V, S> Extend for LiteMap<K, V, S>
fn extend<T: IntoIterator<Item = (K, V)>>(self: &mut Self, iter: T)
impl<K, V, S> Freeze for LiteMap<K, V, S>
impl<K, V, S> FromIterator for LiteMap<K, V, S>
fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self
impl<K, V, S> Index for LiteMap<K, V, S>
fn index(self: &Self, key: &K) -> &V
impl<K, V, S> IndexMut for LiteMap<K, V, S>
fn index_mut(self: &mut Self, key: &K) -> &mut V
impl<K, V, S> IntoIterator for LiteMap<K, V, S>
fn into_iter(self: Self) -> <Self as >::IntoIter
impl<K, V, S> RefUnwindSafe for LiteMap<K, V, S>
impl<K, V, S> Send for LiteMap<K, V, S>
impl<K, V, S> Sync for LiteMap<K, V, S>
impl<K, V, S> Unpin for LiteMap<K, V, S>
impl<K, V, S> UnsafeUnpin for LiteMap<K, V, S>
impl<K, V, S> UnwindSafe for LiteMap<K, V, S>
impl<K: $crate::clone::Clone + ?Sized, V: $crate::clone::Clone + ?Sized, S: $crate::clone::Clone> Clone for LiteMap<K, V, S>
fn clone(self: &Self) -> LiteMap<K, V, S>
impl<K: $crate::cmp::Eq + ?Sized, V: $crate::cmp::Eq + ?Sized, S: $crate::cmp::Eq> Eq for LiteMap<K, V, S>
impl<K: $crate::cmp::Ord + ?Sized, V: $crate::cmp::Ord + ?Sized, S: $crate::cmp::Ord> Ord for LiteMap<K, V, S>
fn cmp(self: &Self, other: &LiteMap<K, V, S>) -> Ordering
impl<K: $crate::cmp::PartialEq + ?Sized, V: $crate::cmp::PartialEq + ?Sized, S: $crate::cmp::PartialEq> PartialEq for LiteMap<K, V, S>
fn eq(self: &Self, other: &LiteMap<K, V, S>) -> bool
impl<K: $crate::cmp::PartialOrd + ?Sized, V: $crate::cmp::PartialOrd + ?Sized, S: $crate::cmp::PartialOrd> PartialOrd for LiteMap<K, V, S>
fn partial_cmp(self: &Self, other: &LiteMap<K, V, S>) -> Option<Ordering>
impl<K: $crate::fmt::Debug + ?Sized, V: $crate::fmt::Debug + ?Sized, S: $crate::fmt::Debug> Debug for LiteMap<K, V, S>
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl<K: $crate::hash::Hash + ?Sized, V: $crate::hash::Hash + ?Sized, S: $crate::hash::Hash> Hash for LiteMap<K, V, S>
fn hash<__H: $crate::hash::Hasher>(self: &Self, state: &mut __H)
impl<K: ?Sized, V: ?Sized, S> StructuralPartialEq for LiteMap<K, V, S>
impl<T> Any for LiteMap<K, V, S>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for LiteMap<K, V, S>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for LiteMap<K, V, S>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for LiteMap<K, V, S>
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> From for LiteMap<K, V, S>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for LiteMap<K, V, S>
fn to_owned(self: &Self) -> Tfn clone_into(self: &Self, target: &mut T)
impl<T, U> Into for LiteMap<K, V, S>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for LiteMap<K, V, S>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for LiteMap<K, V, S>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>