Struct fixed_map::Map

source ·
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

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);

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]);

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]);

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]);

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)]);

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)]);

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);

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"));

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"));

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);

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());

Returns true if the map contains no elements.

Examples
use fixed_map::Map;

#[derive(fixed_map::Key)]
enum Key {
    One,
    Two,
}

let mut map = Map::new();
assert!(map.is_empty());
map.insert(Key::One, "a");
assert!(!map.is_empty());

Returns the number of elements in the map.

Examples
use fixed_map::Map;

#[derive(fixed_map::Key)]
enum Key {
    One,
    Two,
}

let mut map = Map::new();
assert_eq!(map.len(), 0);
map.insert(Key::One, "a");
assert_eq!(map.len(), 1);

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.