Derive Macro fixed_map::Key [−][src]
#[derive(Key)] { // Attributes available to this derive: #[key] }
Expand description
Derive to implement the Key
trait.
Requires that fixed_map
is in scope.
This derive implements the Key
trait for a given type.
The Key
trait is what allows fixed_map to set up storage for a type that will be the key in
a fixed map.
Given the following enum:
use fixed_map::Key;
#[derive(Clone, Copy, Key)]
pub enum Key {
First,
Second,
Third,
}
It performs the following simplified expansion:
use fixed_map::Key;
#[derive(Clone, Copy)]
pub enum Key {
First,
Second,
Third,
}
/// Build a storage struct containing an item for each key:
pub struct KeyStorage<V> {
/// Storage for `Key::First`.
f1: Option<V>,
/// Storage for `Key::Second`.
f2: Option<V>,
/// Storage for `Key::Third`.
f3: Option<V>,
}
/// Implement storage for `KeyStorage`.
impl<V> fixed_map::storage::Storage<Key, V> for KeyStorage<V> {
fn get(&self, key: Key) -> Option<&V> {
match key {
Key::First => self.f1.as_ref(),
Key::Second => self.f2.as_ref(),
Key::Third => self.f3.as_ref(),
}
}
/* skipped */
}
impl<V> Default for KeyStorage<V> {
fn default() -> Self {
Self {
f1: None,
f2: None,
f3: None,
}
}
}
/// Implement the `Key` trait to point out storage.
impl<V> fixed_map::key::Key<Key, V> for Key {
type Storage = KeyStorage<V>;
}