Struct OrderedMap

Source
pub struct OrderedMap<K: 'static, V: 'static> { /* private fields */ }
Expand description

An order-preserving map which can be constructed at compile time.

“Order-preserving” means iteration order is guaranteed to match the definition order.

Implementations§

Source§

impl<K, V> OrderedMap<K, V>

Source

pub const fn new() -> Self

Create an empty map.

Source

pub const fn len(&self) -> usize

Returns the number of entries in the map.

Source

pub const fn is_empty(&self) -> bool

Returns true if the map is empty.

Source

pub fn get<T: ?Sized>(&self, key: &T) -> Option<&V>
where K: PhfKeyProxy<T>,

Returns a reference to the value that key maps to.

Examples found in repository?
examples/custom.rs (line 19)
18fn main() {
19    BY_VAL.get(&12).unwrap();
20    BY_REF.get(&10).unwrap();
21}
More examples
Hide additional examples
examples/simple.rs (line 20)
19fn main() {
20    let v = KEYWORDS.get("loop");
21
22    let Some(Keyword::Loop) = v else {
23        panic!("failed")
24    };
25}
examples/nested.rs (line 29)
28fn main() {
29    let v = KEYWORDS.get(&2).unwrap().get("break");
30
31    let Some(Keyword::Break) = v else {
32        panic!("failed")
33    };
34}
examples/uncased.rs (line 9)
8fn main() {
9    assert_eq!(MAP.get("foo").cloned(), Some(0));
10    assert_eq!(MAP.get("bAR").cloned(), Some(1));
11    assert_eq!(MAP.get(&"bAR".to_owned()).cloned(), Some(1));
12}
Source

pub fn get_key<T: ?Sized>(&self, key: &T) -> Option<&K>
where K: PhfKeyProxy<T>,

Returns a reference to the map’s internal static instance of the given key.

This can be useful for interning schemes.

Source

pub fn contains_key<T: ?Sized>(&self, key: &T) -> bool
where K: PhfKeyProxy<T>,

Determines if key is in the OrderedMap.

Source

pub fn get_index<T: ?Sized>(&self, key: &T) -> Option<usize>
where K: PhfKeyProxy<T>,

Returns the index of the key within the list used to initialize the ordered map.

Source

pub fn index(&self, index: usize) -> Option<(&K, &V)>

Returns references to both the key and values at an index within the list used to initialize the ordered map. See .get_index(key).

Source

pub fn get_entry<T: ?Sized>(&self, key: &T) -> Option<(&K, &V)>
where K: PhfKeyProxy<T>,

Like get, but returns both the key and the value.

Source

pub fn entries(&self) -> Entries<'_, K, V>

Returns an iterator over the key/value pairs in the map.

Entries are returned in the same order in which they were defined.

Source

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

Returns an iterator over the keys in the map.

Keys are returned in the same order in which they were defined.

Source

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

Returns an iterator over the values in the map.

Values are returned in the same order in which they were defined.

Trait Implementations§

Source§

impl<K, V> Debug for OrderedMap<K, V>
where K: Debug, V: Debug,

Source§

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

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

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

Source§

fn default() -> Self

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

impl<'a, K, V, T: ?Sized> Index<&'a T> for OrderedMap<K, V>
where K: PhfKeyProxy<T>,

Source§

type Output = V

The returned type after indexing.
Source§

fn index(&self, k: &'a T) -> &V

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

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

Source§

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

The type of the elements being iterated over.
Source§

type IntoIter = Map<Iter<'a, (K, V)>, fn(&(K, V)) -> (&K, &V)>

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

fn into_iter(self) -> Entries<'a, K, V>

Creates an iterator from a value. Read more
Source§

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

Source§

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

Auto Trait Implementations§

§

impl<K, V> Freeze for OrderedMap<K, V>

§

impl<K, V> RefUnwindSafe for OrderedMap<K, V>

§

impl<K, V> Send for OrderedMap<K, V>
where K: Sync, V: Sync,

§

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

§

impl<K, V> Unpin for OrderedMap<K, V>

§

impl<K, V> UnwindSafe for OrderedMap<K, V>

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, W> HasTypeWitness<W> for T
where W: MakeTypeWitness<Arg = T>, T: ?Sized,

Source§

const WITNESS: W = W::MAKE

A constant of the type witness
Source§

impl<T> Identity for T
where T: ?Sized,

Source§

const TYPE_EQ: TypeEq<T, <T as Identity>::Type> = TypeEq::NEW

Proof that Self is the same type as Self::Type, provides methods for casting between Self and Self::Type.
Source§

type Type = T

The same type as Self, used to emulate type equality bounds (T == U) with associated type equality constraints (T: Identity<Type = U>).
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, 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.