Struct fixed_map::Map

source ·
#[repr(transparent)]
pub struct Map<K, V>where
    K: Key,
{ /* private fields */ }
Expand description

A fixed map with storage specialized through the Key trait.

Examples

use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key)]
enum Part {
    One,
    Two,
}

#[derive(Clone, Copy, Key)]
enum Key {
    Simple,
    Composite(Part),
    String(&'static str),
    Number(u32),
    Singleton(()),
    Option(Option<Part>),
    Boolean(bool),
}

let mut map = Map::new();

map.insert(Key::Simple, 1);
map.insert(Key::Composite(Part::One), 2);
map.insert(Key::String("foo"), 3);
map.insert(Key::Number(1), 4);
map.insert(Key::Singleton(()), 5);
map.insert(Key::Option(None), 6);
map.insert(Key::Option(Some(Part::One)), 7);
map.insert(Key::Boolean(true), 8);

assert_eq!(map.get(Key::Simple), Some(&1));
assert_eq!(map.get(Key::Composite(Part::One)), Some(&2));
assert_eq!(map.get(Key::Composite(Part::Two)), None);
assert_eq!(map.get(Key::String("foo")), Some(&3));
assert_eq!(map.get(Key::String("bar")), None);
assert_eq!(map.get(Key::Number(1)), Some(&4));
assert_eq!(map.get(Key::Number(2)), None);
assert_eq!(map.get(Key::Singleton(())), Some(&5));
assert_eq!(map.get(Key::Option(None)), Some(&6));
assert_eq!(map.get(Key::Option(Some(Part::One))), Some(&7));
assert_eq!(map.get(Key::Option(Some(Part::Two))), None);
assert_eq!(map.get(Key::Boolean(true)), Some(&8));
assert_eq!(map.get(Key::Boolean(false)), None);

Storing references:

use fixed_map::{Key, Map};

#[derive(Debug, Clone, Copy, Key)]
enum Key {
    First,
    Second,
}

let mut map = Map::new();
let a = 42u32;

map.insert(Key::First, &a);

assert_eq!(map.values().cloned().collect::<Vec<_>>(), vec![&42u32]);

Implementations§

A map implementation that uses fixed storage.

Examples

use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key)]
enum Key {
    One,
    Two,
}

let mut m = Map::new();
m.insert(Key::One, 1);

assert_eq!(m.get(Key::One), Some(&1));
assert_eq!(m.get(Key::Two), None);
use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key)]
enum Part {
    A,
    B,
}

#[derive(Clone, Copy, Key)]
enum Key {
    Simple,
    Composite(Part),
}

let mut m = Map::new();
m.insert(Key::Simple, 1);
m.insert(Key::Composite(Part::A), 2);

assert_eq!(m.get(Key::Simple), Some(&1));
assert_eq!(m.get(Key::Composite(Part::A)), Some(&2));
assert_eq!(m.get(Key::Composite(Part::B)), None);

Creates an empty Map.

Examples
use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key)]
enum Key {
    One,
    Two,
}

let mut map: Map<Key, i32> = Map::new();

An iterator visiting all key-value pairs in arbitrary order. The iterator element type is (K, &'a V).

Examples
use fixed_map::{Key, Map};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Key)]
enum Key {
    One,
    Two,
    Three,
}

let mut map = Map::new();
map.insert(Key::One, 1);
map.insert(Key::Two, 2);

assert_eq!(map.iter().collect::<Vec<_>>(), vec![(Key::One, &1), (Key::Two, &2)]);

An iterator visiting all keys in arbitrary order. The iterator element type is K.

Examples
use fixed_map::{Key, Map};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Key)]
pub enum Key {
    First,
    Second,
    Third,
}

let mut map = Map::new();
map.insert(Key::First, 1);
map.insert(Key::Second, 2);

assert!(map.keys().eq([Key::First, Key::Second]));
assert!(map.keys().rev().eq([Key::Second, Key::First]));

Using a composite key:

use fixed_map::{Key, Map};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Key)]
pub enum Key {
    First,
    Second(bool),
    Third,
}

let mut map = Map::new();
map.insert(Key::First, 1);
map.insert(Key::Second(false), 2);

dbg!(map.keys().collect::<Vec<_>>());

assert!(map.keys().eq([Key::First, Key::Second(false)]));
assert!(map.keys().rev().eq([Key::Second(false), Key::First]));

An iterator visiting all values in arbitrary order. The iterator element type is &'a V.

Examples
use fixed_map::{Key, Map};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Key)]
pub enum Key {
    First,
    Second,
    Third,
}

let mut map = Map::new();
map.insert(Key::First, 1);
map.insert(Key::Second, 2);

assert!(map.values().copied().eq([1, 2]));
assert!(map.values().rev().copied().eq([2, 1]));

Using a composite key:

use fixed_map::{Key, Map};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Key)]
pub enum Key {
    First(bool),
    Second,
    Third,
}

let mut map = Map::new();
map.insert(Key::First(false), 1);
map.insert(Key::Second, 2);

assert!(map.values().copied().eq([1, 2]));
assert!(map.values().rev().copied().eq([2, 1]));

An iterator visiting all key-value pairs in arbitrary order, with mutable references to the values. The iterator element type is (K, &'a mut V).

Examples
use fixed_map::{Key, Map};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Key)]
enum Key {
    First,
    Second,
}

let mut map = Map::new();
map.insert(Key::First, 1);
map.insert(Key::Second, 2);

// Update all values
for (_, val) in map.iter_mut() {
    *val *= 2;
}

assert!(map.iter().eq([(Key::First, &2), (Key::Second, &4)]));

Using a composite key:

use fixed_map::{Key, Map};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Key)]
enum Key {
    First(bool),
    Second,
}

let mut map = Map::new();
map.insert(Key::First(true), 1);
map.insert(Key::Second, 2);

// Update all values
for (_, val) in map.iter_mut() {
    *val *= 2;
}

assert!(map.iter().eq([(Key::First(true), &2), (Key::Second, &4)]));

An iterator visiting all values mutably in arbitrary order. The iterator element type is &'a mut V.

Examples
use fixed_map::{Key, Map};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Key)]
pub enum Key {
    First,
    Second,
}

let mut map = Map::new();
map.insert(Key::First, 2);
map.insert(Key::Second, 5);

for (index, val) in map.values_mut().enumerate() {
    *val *= index + 1;
}

assert!(map.values().copied().eq([2, 10]));

let mut map = Map::new();
map.insert(Key::First, 2);
map.insert(Key::Second, 5);

for (index, val) in map.values_mut().rev().enumerate() {
    *val *= index + 1;
}

assert!(map.values().copied().eq([4, 5]));

Using a composite key:

use fixed_map::{Key, Map};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Key)]
pub enum Key {
    First(bool),
    Second,
}

let mut map = Map::new();
map.insert(Key::First(false), 2);
map.insert(Key::Second, 5);

for (index, val) in map.values_mut().enumerate() {
    *val *= index + 1;
}

assert!(map.values().copied().eq([2, 10]));

let mut map = Map::new();
map.insert(Key::First(false), 2);
map.insert(Key::Second, 5);

for (index, val) in map.values_mut().rev().enumerate() {
    *val *= index + 1;
}

assert!(map.values().copied().eq([4, 5]));

Returns true if the map currently contains the given key.

Examples
use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key)]
enum Key {
    First,
    Second,
}

let mut map = Map::new();
map.insert(Key::First, "a");
assert_eq!(map.contains_key(Key::First), true);
assert_eq!(map.contains_key(Key::Second), false);

Returns a reference to the value corresponding to the key.

Examples
use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key)]
enum Key {
    First,
    Second,
}

let mut map = Map::new();
map.insert(Key::First, "a");
assert_eq!(map.get(Key::First).copied(), Some("a"));
assert_eq!(map.get(Key::Second), None);

Using a composite key:

use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key)]
enum Key {
    First(bool),
    Second,
}

let mut map = Map::new();
map.insert(Key::First(true), "a");
assert_eq!(map.get(Key::First(true)).copied(), Some("a"));
assert_eq!(map.get(Key::Second), None);

Returns a mutable reference to the value corresponding to the key.

Examples
use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key)]
enum Key {
    First,
    Second,
}

let mut map = Map::new();
map.insert(Key::First, "a");

if let Some(x) = map.get_mut(Key::First) {
    *x = "b";
}

assert_eq!(map.get(Key::First).copied(), Some("b"));

Using a composite key:

use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key)]
enum Key {
    First(bool),
    Second(()),
    Third,
}

let mut map = Map::new();
map.insert(Key::First(true), "a");

if let Some(x) = map.get_mut(Key::First(true)) {
    *x = "b";
}

assert_eq!(map.get(Key::First(true)).copied(), 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::{Key, Map};

#[derive(Clone, Copy, 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::{Key, Map};

#[derive(Clone, Copy, 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);

Retains only the elements specified by the predicate.

In other words, remove all pairs (k, v) for which f(k, &mut v) returns false. The elements are visited in unsorted (and unspecified) order.

Examples
use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key)]
enum Key {
    First,
    Second,
}

let mut map: Map<Key, i32> = Map::new();

map.insert(Key::First, 42);
map.insert(Key::Second, -10);

map.retain(|k, v| *v > 0);

assert_eq!(map.len(), 1);
assert_eq!(map.get(Key::First), Some(&42));
assert_eq!(map.get(Key::Second), None);

Using a composite key:

use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key)]
enum Key {
    First(bool),
    Second,
}

let mut map: Map<Key, i32> = Map::new();

map.insert(Key::First(true), 42);
map.insert(Key::First(false), -31);
map.insert(Key::Second, 100);

let mut other = map.clone();
assert_eq!(map.len(), 3);

map.retain(|k, v| *v > 0);

assert_eq!(map.len(), 2);
assert_eq!(map.get(Key::First(true)), Some(&42));
assert_eq!(map.get(Key::First(false)), None);
assert_eq!(map.get(Key::Second), Some(&100));

other.retain(|k, v| matches!(k, Key::First(_)));

assert_eq!(other.len(), 2);
assert_eq!(other.get(Key::First(true)), Some(&42));
assert_eq!(other.get(Key::First(false)), Some(&-31));
assert_eq!(other.get(Key::Second), None);

Clears the map, removing all key-value pairs. Keeps the allocated memory for reuse.

Examples
use fixed_map::{Key, Map};

#[derive(Clone, Copy, 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::{Key, Map};

#[derive(Clone, Copy, Key)]
enum Key {
    First,
    Second,
}

let mut map = Map::new();
assert!(map.is_empty());
map.insert(Key::First, 1);
assert!(!map.is_empty());

An empty key:

use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key)]
enum Key {}

let map = Map::<Key, u32>::new();
assert!(map.is_empty());

Gets the current length of a Map.

Examples
use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key)]
enum Key {
    First,
    Second,
}

let mut map: Map<Key, i32> = Map::new();
assert_eq!(map.len(), 0);

map.insert(Key::First, 42);
assert_eq!(map.len(), 1);

map.insert(Key::First, 42);
assert_eq!(map.len(), 1);

map.remove(Key::First);
assert_eq!(map.len(), 0);

Using a composite key:

use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key)]
enum Key {
    First(bool),
    Second,
}

let mut map: Map<Key, i32> = Map::new();
assert_eq!(map.len(), 0);

map.insert(Key::First(true), 42);
assert_eq!(map.len(), 1);

map.insert(Key::First(false), 42);
assert_eq!(map.len(), 2);

map.remove(Key::First(true));
assert_eq!(map.len(), 1);

Gets the given key’s corresponding Entry in the Map for in-place manipulation.

Examples
use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key)]
enum Key {
    Even,
    Odd,
}

let mut map: Map<Key, u32> = Map::new();

for n in [3, 45, 3, 23, 2, 10, 59, 11, 51, 70] {
    map
        .entry(if n % 2 == 0 { Key::Even } else { Key::Odd })
        .and_modify(|x| *x += 1)
        .or_insert(1);
}

assert_eq!(map.get(Key::Even), Some(&3));
assert_eq!(map.get(Key::Odd), Some(&7));

Using a composite key:

use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key)]
enum Key {
    First(bool),
    Second,
}

let mut map: Map<Key, Vec<i32>> = Map::new();

map.entry(Key::First(true)).or_default().push(1);
map.entry(Key::Second).or_insert_with(|| vec![2; 8]).truncate(4);

assert_eq!(map.get(Key::First(true)), Some(&vec![1]));
assert_eq!(map.get(Key::Second), Some(&vec![2; 4]));

Trait Implementations§

Clone implementation for a Map.

Examples

use fixed_map::{Key, Map};

#[derive(Debug, Clone, Copy, Key)]
enum Key {
    First(bool),
    Second,
}

let mut a = Map::new();
a.insert(Key::First(true), 1);
let mut b = a.clone();
b.insert(Key::Second, 2);

assert_ne!(a, b);

assert_eq!(a.get(Key::First(true)), Some(&1));
assert_eq!(a.get(Key::Second), None);

assert_eq!(b.get(Key::First(true)), Some(&1));
assert_eq!(b.get(Key::Second), Some(&2));
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more

The Debug implementation for a Map.

Examples

use fixed_map::{Key, Map};

#[derive(Debug, Clone, Copy, Key)]
enum Key {
    First,
    Second,
}

let mut a = Map::new();
a.insert(Key::First, 42);

assert_eq!("{First: 42}", format!("{:?}", a));
Formats the value using the given formatter. Read more

The Default implementation for a Map produces an empty map.

Examples

use fixed_map::{Key, Map};

#[derive(Debug, Clone, Copy, Key)]
enum Key {
    First,
    Second,
}

let a = Map::<Key, u32>::default();
let b = Map::<Key, u32>::new();

assert_eq!(a, b);
Returns the “default value” for a type. Read more
Deserialize this value from the given Serde deserializer. Read more

A simple FromIterator implementation for Map.

Example

use fixed_map::{Key, Map};

#[derive(Debug, Clone, Copy, Key)]
enum Key {
    First,
    Second,
}

let v = vec![(Key::First, 1), (Key::Second, 2), (Key::First, 3)];
let m: Map<_, u8> = v.into_iter().collect();

let mut n = Map::new();
n.insert(Key::Second, 2);
n.insert(Key::First, 3);

assert_eq!(m, n);
Creates a value from an iterator. Read more

Hash implementation for a Set.

Examples

use std::collections::HashSet;

use fixed_map::{Key, Map};

#[derive(Debug, Clone, Copy, Hash, Key)]
enum Key {
    First,
    Second,
}

let mut a = Map::new();
a.insert(Key::First, 1);

let mut set = HashSet::new();
set.insert(a);

Using a composite key:

use std::collections::HashSet;

use fixed_map::{Key, Map};

#[derive(Debug, Clone, Copy, Hash, Key)]
enum Key {
    First(bool),
    Second,
}

let mut a = Map::new();
a.insert(Key::First(true), 1);

// TODO: support this
// let mut set = HashSet::new();
// set.insert(a);
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more

IntoIterator implementation which uses Map::iter_mut. See its documentation for more.

The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more

Produce an owning iterator visiting all key-value pairs of the Map in an arbitrary order. The iterator element type is (K, V).

Examples

use fixed_map::{Key, Map};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Key)]
enum Key {
    First,
    Second,
    Third,
}

let mut map = Map::new();
map.insert(Key::First, 1);
map.insert(Key::Third, 3);

let mut it = map.into_iter();
assert_eq!(it.next(), Some((Key::First, 1)));
assert_eq!(it.next(), Some((Key::Third, 3)));
assert_eq!(it.next(), None);

let mut it = map.into_iter().rev();
assert_eq!(it.next(), Some((Key::Third, 3)));
assert_eq!(it.next(), Some((Key::First, 1)));
assert_eq!(it.next(), None);

Into iterator with a composite key:

use fixed_map::{Key, Map};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Key)]
enum Key {
    First(bool),
    Second,
    Third,
}

let mut map = Map::<_, u32>::new();
map.insert(Key::First(false), 1);
map.insert(Key::Third, 3);

let mut it = map.into_iter();
assert_eq!(it.next(), Some((Key::First(false), 1)));
assert_eq!(it.next(), Some((Key::Third, 3)));
assert_eq!(it.next(), None);

let mut it = map.into_iter().rev();
assert_eq!(it.next(), Some((Key::Third, 3)));
assert_eq!(it.next(), Some((Key::First(false), 1)));
assert_eq!(it.next(), None);
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more

Ord implementation for a Map.

For more details on ordering, see the Key documentation.

Examples

use fixed_map::{Key, Map};

#[derive(Debug, Clone, Copy, Hash, Key)]
enum Key {
    First,
    Second,
}

let mut a = Map::new();
a.insert(Key::First, 1);

let mut b = Map::new();
b.insert(Key::Second, 1);

let mut list = vec![b, a];
list.sort();

assert_eq!(list, [a, b]);

Using a composite key:

use fixed_map::{Key, Map};

#[derive(Debug, Clone, Copy, Hash, Key)]
enum Key {
    First(bool),
    Second,
}

let mut a = Map::new();
a.insert(Key::First(true), 1);

let mut b = Map::new();
b.insert(Key::Second, 1);

// TODO: support this
// let mut list = vec![a, b];
// list.sort();
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more

PartialEq implementation for a Map.

Examples

use fixed_map::{Key, Map};

#[derive(Debug, Clone, Copy, Key)]
enum Key {
    First,
    Second,
}

let mut a = Map::new();
a.insert(Key::First, 42);
// Note: `a` is Copy since it's using a simple key.
let mut b = a;

assert_eq!(a, b);

b.insert(Key::Second, 42);
assert_ne!(a, b);

Using a composite key:

use fixed_map::{Key, Map};

#[derive(Debug, Clone, Copy, Key)]
enum Key {
    First(bool),
    Second,
}

let mut a = Map::new();
a.insert(Key::First(true), 42);
let mut b = a.clone();

assert_eq!(a, b);

b.insert(Key::Second, 42);
assert_ne!(a, b);
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

PartialOrd implementation for a Map.

For more details on ordering, see the Key documentation.

Examples

use fixed_map::{Key, Map};

#[derive(Debug, Clone, Copy, Hash, Key)]
enum Key {
    First,
    Second,
    Third,
}

let mut a = Map::new();
a.insert(Key::First, 1);

let mut b = Map::new();
b.insert(Key::Third, 1);

assert!(a < b);

let mut empty = Map::new();
assert!(empty < a);
assert!(empty < b);

Using a composite key:

use fixed_map::{Key, Map};

#[derive(Debug, Clone, Copy, Hash, Key)]
enum Key {
    First(bool),
    Second,
}

let mut a = Map::new();
a.insert(Key::First(true), 1);

let mut b = Map::new();
b.insert(Key::Second, 1);

// TODO: support this
// assert!(a < b);
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Serialize this value into the given Serde serializer. Read more

The Copy implementation for a Map depends on its Key. If the derived key only consists of unit variants the corresponding Map will be Copy as well.

Examples

use fixed_map::{Key, Map};

#[derive(Debug, Clone, Copy, Key)]
enum Key {
    First,
    Second,
}

let mut a = Map::new();
a.insert(Key::First, 1);
let mut b = a;
b.insert(Key::Second, 2);

assert_ne!(a, b);

assert_eq!(a.get(Key::First), Some(&1));
assert_eq!(a.get(Key::Second), None);

assert_eq!(b.get(Key::First), Some(&1));
assert_eq!(b.get(Key::Second), Some(&2));

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.