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 Key {
First,
Second,
Third,
}
let mut a = Set::new();
a.insert(Key::First);
let mut b = Set::new();
b.insert(Key::Third);
let mut c = Set::new();
c.insert(Key::First);
c.insert(Key::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 Key {
First,
Second,
Third,
}
let mut a = BTreeSet::new();
a.insert(Key::First);
let mut b = BTreeSet::new();
b.insert(Key::Third);
let mut c = BTreeSet::new();
c.insert(Key::First);
c.insert(Key::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.