Expand description
A map with a fixed, pre-determined size.
Implementations
sourceimpl<K: 'static> Set<K>where
K: Key<K, ()>,
impl<K: 'static> Set<K>where
K: Key<K, ()>,
A map implementation that uses fixed storage.
Examples
use fixed_map::Set;
#[derive(Clone, Copy, fixed_map::Key)]
enum Key {
One,
Two,
}
let mut m = Set::new();
m.insert(Key::One);
assert_eq!(m.contains(Key::One), true);
assert_eq!(m.contains(Key::Two), false);
use fixed_map::Set;
#[derive(Clone, Copy, fixed_map::Key)]
enum Part {
A,
B,
}
#[derive(Clone, Copy, fixed_map::Key)]
enum Key {
Simple,
Composite(Part),
}
let mut m = Set::new();
m.insert(Key::Simple);
m.insert(Key::Composite(Part::A));
assert_eq!(m.contains(Key::Simple), true);
assert_eq!(m.contains(Key::Composite(Part::A)), true);
assert_eq!(m.contains(Key::Composite(Part::B)), false);
pub fn new() -> Set<K>
sourcepub fn iter(&self) -> Iter<K>
pub fn iter(&self) -> Iter<K>
An iterator visiting all values in arbitrary order.
The iterator element type is K
.
Because of limitations in how Rust can express lifetimes through traits, this method will first pre-allocate a vector to store all references.
For a zero-cost version of this function, see Set::iter_fn
.
Examples
use fixed_map::Set;
#[derive(Debug, Clone, Copy, PartialEq, Eq, fixed_map::Key)]
enum Key {
One,
Two,
Three,
}
let mut map = Set::new();
map.insert(Key::One);
map.insert(Key::Two);
assert_eq!(map.iter().collect::<Vec<_>>(), vec![Key::One, Key::Two]);
sourcepub fn iter_fn<'a, F>(&'a self, f: F)where
F: FnMut(K),
pub fn iter_fn<'a, F>(&'a self, f: F)where
F: FnMut(K),
An closure visiting all values in arbitrary order.
The closure argument type is K
.
This is a zero-cost version of Set::iter
.
Examples
use fixed_map::Set;
#[derive(Debug, Clone, Copy, PartialEq, Eq, fixed_map::Key)]
enum Key {
One,
Two,
Three,
}
let mut map = Set::new();
map.insert(Key::One);
map.insert(Key::Two);
let mut out = Vec::new();
map.iter_fn(|e| out.push(e));
assert_eq!(out, vec![Key::One, Key::Two]);
sourcepub fn contains(&self, key: K) -> bool
pub fn contains(&self, key: K) -> bool
Returns true
if the set contains a value.
Returns a reference to the value corresponding to the key.
Examples
use fixed_map::Set;
#[derive(Clone, Copy, fixed_map::Key)]
enum Key {
One,
Two,
}
let mut map = Set::new();
map.insert(Key::One);
assert_eq!(map.contains(Key::One), true);
assert_eq!(map.contains(Key::Two), false);
sourcepub fn insert(&mut self, value: K) -> bool
pub fn insert(&mut self, value: K) -> bool
Adds a value to the set.
If the set did not have this value present, true
is returned.
If the set did have this value present, false
is returned.
Examples
use fixed_map::Set;
#[derive(Clone, Copy, fixed_map::Key)]
enum Key {
One,
Two,
}
let mut set = Set::new();
assert_eq!(set.insert(Key::One), true);
assert_eq!(set.is_empty(), false);
set.insert(Key::Two);
assert_eq!(set.insert(Key::Two), false);
assert_eq!(set.contains(Key::Two), true);
sourcepub fn remove(&mut self, key: K) -> bool
pub fn remove(&mut self, key: K) -> bool
Removes a value from the set. Returns true
if the value was
present in the set.
Examples
use fixed_map::Set;
#[derive(Clone, Copy, fixed_map::Key)]
enum Key {
One,
Two,
}
let mut set = Set::new();
set.insert(Key::One);
assert_eq!(set.remove(Key::One), true);
assert_eq!(set.remove(Key::One), false);
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the set, removing all values.
Examples
use fixed_map::Set;
#[derive(Clone, Copy, fixed_map::Key)]
enum Key {
One,
Two,
}
let mut set = Set::new();
set.insert(Key::One);
set.clear();
assert!(set.is_empty());