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§
Sourcetype MapStorage<V>: MapStorage<Self, V>
type MapStorage<V>: MapStorage<Self, V>
The Map storage implementation to use for the key
implementing this trait.
Sourcetype SetStorage: SetStorage<Self>
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
Available on crate feature hashbrown only.
impl Key for &'static str
hashbrown only.type MapStorage<V> = HashbrownMapStorage<&'static str, V>
type SetStorage = HashbrownSetStorage<&'static str>
Source§impl Key for &'static [u8]
Available on crate feature hashbrown only.
impl Key for &'static [u8]
hashbrown only.type MapStorage<V> = HashbrownMapStorage<&'static [u8], V>
type SetStorage = HashbrownSetStorage<&'static [u8]>
Source§impl Key for bool
impl Key for bool
type MapStorage<V> = BooleanMapStorage<V>
type SetStorage = BooleanSetStorage
Source§impl Key for char
Available on crate feature hashbrown only.
impl Key for char
hashbrown only.type MapStorage<V> = HashbrownMapStorage<char, V>
type SetStorage = HashbrownSetStorage<char>
Source§impl Key for i8
Available on crate feature hashbrown only.
impl Key for i8
hashbrown only.type MapStorage<V> = HashbrownMapStorage<i8, V>
type SetStorage = HashbrownSetStorage<i8>
Source§impl Key for i32
Available on crate feature hashbrown only.
impl Key for i32
hashbrown only.type MapStorage<V> = HashbrownMapStorage<i32, V>
type SetStorage = HashbrownSetStorage<i32>
Source§impl Key for i64
Available on crate feature hashbrown only.
impl Key for i64
hashbrown only.type MapStorage<V> = HashbrownMapStorage<i64, V>
type SetStorage = HashbrownSetStorage<i64>
Source§impl Key for i128
Available on crate feature hashbrown only.
impl Key for i128
hashbrown only.type MapStorage<V> = HashbrownMapStorage<i128, V>
type SetStorage = HashbrownSetStorage<i128>
Source§impl Key for isize
Available on crate feature hashbrown only.
impl Key for isize
hashbrown only.type MapStorage<V> = HashbrownMapStorage<isize, V>
type SetStorage = HashbrownSetStorage<isize>
Source§impl Key for u8
Available on crate feature hashbrown only.
impl Key for u8
hashbrown only.type MapStorage<V> = HashbrownMapStorage<u8, V>
type SetStorage = HashbrownSetStorage<u8>
Source§impl Key for u32
Available on crate feature hashbrown only.
impl Key for u32
hashbrown only.type MapStorage<V> = HashbrownMapStorage<u32, V>
type SetStorage = HashbrownSetStorage<u32>
Source§impl Key for u64
Available on crate feature hashbrown only.
impl Key for u64
hashbrown only.type MapStorage<V> = HashbrownMapStorage<u64, V>
type SetStorage = HashbrownSetStorage<u64>
Source§impl Key for u128
Available on crate feature hashbrown only.
impl Key for u128
hashbrown only.type MapStorage<V> = HashbrownMapStorage<u128, V>
type SetStorage = HashbrownSetStorage<u128>
Source§impl Key for ()
impl Key for ()
type MapStorage<V> = SingletonMapStorage<V>
type SetStorage = SingletonSetStorage
Source§impl Key for usize
Available on crate feature hashbrown only.
impl Key for usize
hashbrown only.