pub struct Map<K: 'static, V: 'static>where
K: Key<K, V>,{ /* private fields */ }
Expand description
A map with a fixed, pre-determined size.
Implementations
sourceimpl<K: 'static, V: 'static> Map<K, V>where
K: Key<K, V>,
impl<K: 'static, V: 'static> Map<K, V>where
K: Key<K, V>,
A map implementation that uses fixed storage.
Examples
use fixed_map::Map;
#[derive(fixed_map::Key)]
enum MyKey {
Foo,
Bar,
}
let mut m = Map::new();
m.insert(MyKey::Foo, 42);
assert_eq!(m.get(&MyKey::Foo), Some(&42));
assert_eq!(m.get(&MyKey::Bar), None);
pub fn new() -> Map<K, V>
sourcepub fn keys<'a, F>(&'a self, f: F)where
F: FnMut(&'a K),
pub fn keys<'a, F>(&'a self, f: F)where
F: FnMut(&'a K),
An iterator visiting all keys in arbitrary order.
The iterator element type is &'a K
.
Examples
use fixed_map::Map;
#[derive(Debug, PartialEq, Eq, fixed_map::Key)]
pub enum Key {
One,
Two,
Three,
}
let mut map = Map::new();
map.insert(Key::One, 1);
map.insert(Key::Two, 2);
let mut out = Vec::new();
map.keys(|key| out.push(key));
assert_eq!(out, vec![&Key::One, &Key::Two]);
sourcepub fn values<'a, F>(&'a self, f: F)where
F: FnMut(&'a V),
pub fn values<'a, F>(&'a self, f: F)where
F: FnMut(&'a V),
An iterator visiting all values in arbitrary order.
The iterator element type is &'a V
.
Examples
use fixed_map::Map;
#[derive(Debug, PartialEq, Eq, fixed_map::Key)]
pub enum Key {
One,
Two,
Three,
}
let mut map = Map::new();
map.insert(Key::One, 1);
map.insert(Key::Two, 2);
let mut out = Vec::new();
map.values(|val| out.push(*val));
assert_eq!(out, vec![1, 2]);
sourcepub fn values_mut<'a, F>(&'a mut self, f: F)where
F: FnMut(&'a mut V),
pub fn values_mut<'a, F>(&'a mut self, f: F)where
F: FnMut(&'a mut V),
An iterator visiting all values mutably in arbitrary order.
The iterator element type is &'a mut V
.
Examples
use fixed_map::Map;
#[derive(Debug, PartialEq, Eq, fixed_map::Key)]
pub enum Key {
One,
Two,
Three,
}
let mut map = Map::new();
map.insert(Key::One, 1);
map.insert(Key::Two, 2);
map.values_mut(|val| *val = *val + 10);
let mut out = Vec::new();
map.values(|val| out.push(*val));
assert_eq!(out, vec![11, 12]);
sourcepub fn iter<'a, F>(&'a self, f: F)where
F: FnMut((&'a K, &'a V)),
pub fn iter<'a, F>(&'a self, f: F)where
F: FnMut((&'a K, &'a V)),
An iterator visiting all key-value pairs in arbitrary order.
The iterator element type is (&'a K, &'a V)
.
Examples
use fixed_map::Map;
#[derive(Debug, PartialEq, Eq, fixed_map::Key)]
enum Key {
One,
Two,
Three,
}
let mut map = Map::new();
map.insert(Key::One, 1);
map.insert(Key::Two, 2);
let mut out = Vec::new();
map.iter(|e| out.push(e));
assert_eq!(out, vec![(&Key::One, &1), (&Key::Two, &2)]);
sourcepub fn iter_mut<'a, F>(&'a mut self, f: F)where
F: FnMut((&'a K, &'a mut V)),
pub fn iter_mut<'a, F>(&'a mut self, f: F)where
F: FnMut((&'a K, &'a mut V)),
An iterator visiting all key-value pairs in arbitrary order,
with mutable references to the values.
The iterator element type is (&'a K, &'a mut V)
.
Examples
use fixed_map::Map;
#[derive(Debug, PartialEq, Eq, fixed_map::Key)]
enum Key {
One,
Two,
Three,
}
let mut map = Map::new();
map.insert(Key::One, 1);
map.insert(Key::Two, 2);
// Update all values
map.iter_mut(|(_, val)| {
*val *= 2;
});
let mut out = Vec::new();
map.iter(|e| out.push(e));
assert_eq!(out, vec![(&Key::One, &2), (&Key::Two, &4)]);
sourcepub fn get(&self, key: &K) -> Option<&V>
pub fn get(&self, key: &K) -> Option<&V>
Returns a reference to the value corresponding to the key.
Examples
use fixed_map::Map;
#[derive(fixed_map::Key)]
enum Key {
One,
Two,
}
let mut map = Map::new();
map.insert(Key::One, "a");
assert_eq!(map.get(&Key::One), Some(&"a"));
assert_eq!(map.get(&Key::Two), None);
sourcepub fn get_mut(&mut self, key: &K) -> Option<&mut V>
pub fn get_mut(&mut self, key: &K) -> Option<&mut V>
Returns a mutable reference to the value corresponding to the key.
Examples
use fixed_map::Map;
#[derive(fixed_map::Key)]
enum Key {
One,
Two,
}
let mut map = Map::new();
map.insert(Key::One, "a");
if let Some(x) = map.get_mut(&Key::One) {
*x = "b";
}
assert_eq!(map.get(&Key::One), Some(&"b"));
sourcepub fn insert(&mut self, key: K, value: V) -> Option<V>
pub fn insert(&mut self, key: K, value: V) -> Option<V>
Inserts a key-value pair into the map.
If the map did not have this key present, None
is returned.
If the map did have this key present, the value is updated, and the old value is returned.
Examples
use fixed_map::Map;
#[derive(fixed_map::Key)]
enum Key {
One,
Two,
}
let mut map = Map::new();
assert_eq!(map.insert(Key::One, "a"), None);
assert_eq!(map.is_empty(), false);
map.insert(Key::Two, "b");
assert_eq!(map.insert(Key::Two, "c"), Some("b"));
assert_eq!(map.get(&Key::Two), Some(&"c"));
sourcepub fn remove(&mut self, key: &K) -> Option<V>
pub fn remove(&mut self, key: &K) -> Option<V>
Removes a key from the map, returning the value at the key if the key was previously in the map.
Examples
use fixed_map::Map;
#[derive(fixed_map::Key)]
enum Key {
One,
Two,
}
let mut map = Map::new();
map.insert(Key::One, "a");
assert_eq!(map.remove(&Key::One), Some("a"));
assert_eq!(map.remove(&Key::One), None);
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the map, removing all key-value pairs. Keeps the allocated memory for reuse.
Examples
use fixed_map::Map;
#[derive(fixed_map::Key)]
enum Key {
One,
Two,
}
let mut map = Map::new();
map.insert(Key::One, "a");
map.clear();
assert!(map.is_empty());