Trait Key

Source
pub trait Key: Copy {
    type MapStorage<V>: MapStorage<Self, V>;
    type SetStorage: SetStorage<Self>;
}
Expand description

The trait for a key that can be used to store values in a Map or Set.

This is implemented automatically from enums through the Key. The following is a simple key which has no nested keys:

use fixed_map::Key;

#[derive(Clone, Copy, Key)]
enum MyKey {
    First,
    Second,
}

Composite keys are when keys structurally includes other keys. They have slightly worse performance than simple keys because they can’t be simply arranged as arrays internally. bool below here implements Key and using it in one key constructs a composite key. It’s a simple form of key since it can only inhabit two values - true or false. Option<K> can also be used as a composite key:

use fixed_map::Key;

#[derive(Clone, Copy, Key)]
enum MyKey {
    First(bool),
    Second(Option<bool>),
}

Some composite keys require dynamic storage since they can inhabit a large number of values, and preferrably should be avoided in favor of using a HashMap directly. But if you absolutely have to you can enable the map feature:

use fixed_map::Key;

#[derive(Clone, Copy, Key)]
enum MyKey {
    First(u32),
    Second,
}

§Ordering

Keys provide their own ordering semantics instead of relying on the PartialOrd and Ord traits.

Therefore keys when stored in a collection such as Map and Set are always ordered in declaration order. This allows those containers themselves to be ordered if the underlying key supports, it similarly to how BTreeMap and BTreeSet works.

use fixed_map::{Key, Set};

#[derive(Clone, Copy, Key)]
enum MyKey {
    First,
    Second,
    Third,
}

let mut a = Set::new();
a.insert(MyKey::First);

let mut b = Set::new();
b.insert(MyKey::Third);

let mut c = Set::new();
c.insert(MyKey::First);
c.insert(MyKey::Third);

assert!(a < b);
assert!(c < b);
assert!(a < c);

The same example with BTreeSet:

use std::collections::BTreeSet;

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
enum MyKey {
    First,
    Second,
    Third,
}

let mut a = BTreeSet::new();
a.insert(MyKey::First);

let mut b = BTreeSet::new();
b.insert(MyKey::Third);

let mut c = BTreeSet::new();
c.insert(MyKey::First);
c.insert(MyKey::Third);

assert!(a < b);
assert!(c < b);
assert!(a < c);

Required Associated Types§

Source

type MapStorage<V>: MapStorage<Self, V>

The Map storage implementation to use for the key implementing this trait.

Source

type SetStorage: SetStorage<Self>

The Set storage implementation to use for the key implementing this trait.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Key for &'static str

Source§

type MapStorage<V> = HashbrownMapStorage<&'static str, V>

Source§

type SetStorage = HashbrownSetStorage<&'static str>

Source§

impl Key for &'static [u8]

Source§

type MapStorage<V> = HashbrownMapStorage<&'static [u8], V>

Source§

type SetStorage = HashbrownSetStorage<&'static [u8]>

Source§

impl Key for bool

Source§

type MapStorage<V> = BooleanMapStorage<V>

Source§

type SetStorage = BooleanSetStorage

Source§

impl Key for char

Source§

type MapStorage<V> = HashbrownMapStorage<char, V>

Source§

type SetStorage = HashbrownSetStorage<char>

Source§

impl Key for i8

Source§

type MapStorage<V> = HashbrownMapStorage<i8, V>

Source§

type SetStorage = HashbrownSetStorage<i8>

Source§

impl Key for i32

Source§

type MapStorage<V> = HashbrownMapStorage<i32, V>

Source§

type SetStorage = HashbrownSetStorage<i32>

Source§

impl Key for i64

Source§

type MapStorage<V> = HashbrownMapStorage<i64, V>

Source§

type SetStorage = HashbrownSetStorage<i64>

Source§

impl Key for i128

Source§

type MapStorage<V> = HashbrownMapStorage<i128, V>

Source§

type SetStorage = HashbrownSetStorage<i128>

Source§

impl Key for isize

Source§

type MapStorage<V> = HashbrownMapStorage<isize, V>

Source§

type SetStorage = HashbrownSetStorage<isize>

Source§

impl Key for u8

Source§

type MapStorage<V> = HashbrownMapStorage<u8, V>

Source§

type SetStorage = HashbrownSetStorage<u8>

Source§

impl Key for u32

Source§

type MapStorage<V> = HashbrownMapStorage<u32, V>

Source§

type SetStorage = HashbrownSetStorage<u32>

Source§

impl Key for u64

Source§

type MapStorage<V> = HashbrownMapStorage<u64, V>

Source§

type SetStorage = HashbrownSetStorage<u64>

Source§

impl Key for u128

Source§

type MapStorage<V> = HashbrownMapStorage<u128, V>

Source§

type SetStorage = HashbrownSetStorage<u128>

Source§

impl Key for ()

Source§

type MapStorage<V> = SingletonMapStorage<V>

Source§

type SetStorage = SingletonSetStorage

Source§

impl Key for usize

Source§

type MapStorage<V> = HashbrownMapStorage<usize, V>

Source§

type SetStorage = HashbrownSetStorage<usize>

Source§

impl<K> Key for Option<K>
where K: Key,

Source§

type MapStorage<V> = OptionMapStorage<K, V>

Source§

type SetStorage = OptionSetStorage<K>

Implementors§