Trait fixed_map::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 can be derived automatically from enums. 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.

Implementations on Foreign Types§

source§

impl Key for i8

§

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

§

type SetStorage = HashbrownSetStorage<i8>

source§

impl Key for u128

§

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

§

type SetStorage = HashbrownSetStorage<u128>

source§

impl Key for usize

§

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

§

type SetStorage = HashbrownSetStorage<usize>

source§

impl Key for u8

§

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

§

type SetStorage = HashbrownSetStorage<u8>

source§

impl Key for ()

§

type MapStorage<V> = SingletonMapStorage<V>

§

type SetStorage = SingletonSetStorage

source§

impl Key for i64

§

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

§

type SetStorage = HashbrownSetStorage<i64>

source§

impl Key for u32

§

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

§

type SetStorage = HashbrownSetStorage<u32>

source§

impl Key for &'static [u8]

§

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

§

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

source§

impl Key for i128

§

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

§

type SetStorage = HashbrownSetStorage<i128>

source§

impl Key for &'static str

§

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

§

type SetStorage = HashbrownSetStorage<&'static str>

source§

impl Key for u64

§

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

§

type SetStorage = HashbrownSetStorage<u64>

source§

impl Key for char

§

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

§

type SetStorage = HashbrownSetStorage<char>

source§

impl Key for isize

§

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

§

type SetStorage = HashbrownSetStorage<isize>

source§

impl Key for bool

§

type MapStorage<V> = BooleanMapStorage<V>

§

type SetStorage = BooleanSetStorage

source§

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

§

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

§

type SetStorage = OptionSetStorage<K>

source§

impl Key for i32

§

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

§

type SetStorage = HashbrownSetStorage<i32>

Implementors§