pub struct Set<T>where
T: Key,{ /* private fields */ }
Expand description
A fixed set with storage specialized through the Key
trait.
Examples
use fixed_map::{Key, Set};
#[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 set = Set::new();
set.insert(Key::Simple);
set.insert(Key::Composite(Part::One));
set.insert(Key::String("foo"));
set.insert(Key::Number(1));
set.insert(Key::Singleton(()));
set.insert(Key::Option(None));
set.insert(Key::Option(Some(Part::One)));
set.insert(Key::Boolean(true));
assert!(set.contains(Key::Simple));
assert!(set.contains(Key::Composite(Part::One)));
assert!(!set.contains(Key::Composite(Part::Two)));
assert!(set.contains(Key::String("foo")));
assert!(!set.contains(Key::String("bar")));
assert!(set.contains(Key::Number(1)));
assert!(!set.contains(Key::Number(2)));
assert!(set.contains(Key::Singleton(())));
assert!(set.contains(Key::Option(None)));
assert!(set.contains(Key::Option(Some(Part::One))));
assert!(!set.contains(Key::Option(Some(Part::Two))));
assert!(set.contains(Key::Boolean(true)));
assert!(!set.contains(Key::Boolean(false)));
Implementations§
source§impl<T> Set<T>where
T: Key,
impl<T> Set<T>where
T: Key,
A set implementation that uses fixed storage.
Examples
use fixed_map::{Key, Set};
#[derive(Clone, Copy, Key)]
enum Key {
First,
Second,
}
let mut m = Set::new();
m.insert(Key::First);
assert_eq!(m.contains(Key::First), true);
assert_eq!(m.contains(Key::Second), false);
use fixed_map::{Key, Set};
#[derive(Clone, Copy, Key)]
enum Part {
A,
B,
}
#[derive(Clone, Copy, 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);
sourcepub fn iter(&self) -> Iter<'_, T>
pub fn iter(&self) -> Iter<'_, T>
An iterator visiting all values in arbitrary order.
The iterator element type is T
.
Examples
use fixed_map::{Key, Set};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Key)]
enum Key {
One,
Two,
Three,
}
let mut set = Set::new();
set.insert(Key::One);
set.insert(Key::Two);
assert_eq!(set.iter().collect::<Vec<_>>(), vec![Key::One, Key::Two]);
sourcepub fn contains(&self, value: T) -> bool
pub fn contains(&self, value: T) -> bool
Returns true
if the set currently contains the given value.
Examples
use fixed_map::{Key, Set};
#[derive(Clone, Copy, Key)]
enum Key {
One,
Two,
}
let mut set = Set::new();
set.insert(Key::One);
assert_eq!(set.contains(Key::One), true);
assert_eq!(set.contains(Key::Two), false);
sourcepub fn insert(&mut self, value: T) -> bool
pub fn insert(&mut self, value: T) -> 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::{Key, Set};
#[derive(Clone, Copy, Key)]
enum Key {
One,
Two,
}
let mut set = Set::new();
assert!(set.insert(Key::One));
assert!(!set.is_empty());
set.insert(Key::Two);
assert!(!set.insert(Key::Two));
assert!(set.contains(Key::Two));
sourcepub fn remove(&mut self, value: T) -> bool
pub fn remove(&mut self, value: T) -> bool
Removes a value from the set. Returns true
if the value was
present in the set.
Examples
use fixed_map::{Key, Set};
#[derive(Clone, Copy, 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 retain<F>(&mut self, f: F)where
F: FnMut(T) -> bool,
pub fn retain<F>(&mut self, f: F)where
F: FnMut(T) -> bool,
Retains only the elements specified by the predicate.
In other words, remove all elements e for which f(e) returns false. The elements are visited in unsorted (and unspecified) order.
Examples
use fixed_map::{Key, Set};
#[derive(Clone, Copy, Key)]
enum Key {
First,
Second,
}
let mut set = Set::new();
set.insert(Key::First);
set.insert(Key::Second);
set.retain(|k| matches!(k, Key::First));
assert_eq!(set.len(), 1);
assert_eq!(set.contains(Key::First), true);
assert_eq!(set.contains(Key::Second), false);
Using a composite key:
use fixed_map::{Key, Set};
#[derive(Clone, Copy, Key)]
enum Key {
First(bool),
Second(bool),
}
let mut set = Set::new();
set.insert(Key::First(true));
set.insert(Key::First(false));
set.insert(Key::Second(true));
set.insert(Key::Second(false));
let mut other = set.clone();
assert_eq!(set.len(), 4);
set.retain(|k| matches!(k, Key::First(true) | Key::Second(true)));
assert_eq!(set.len(), 2);
assert_eq!(set.contains(Key::First(true)), true);
assert_eq!(set.contains(Key::First(false)), false);
assert_eq!(set.contains(Key::Second(true)), true);
assert_eq!(set.contains(Key::Second(false)), false);
other.retain(|k| matches!(k, Key::First(_)));
assert_eq!(other.len(), 2);
assert_eq!(other.contains(Key::First(true)), true);
assert_eq!(other.contains(Key::First(false)), true);
assert_eq!(other.contains(Key::Second(true)), false);
assert_eq!(other.contains(Key::Second(false)), false);
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the set, removing all values.
Examples
use fixed_map::{Key, Set};
#[derive(Clone, Copy, Key)]
enum Key {
One,
Two,
}
let mut set = Set::new();
set.insert(Key::One);
set.clear();
assert!(set.is_empty());
Trait Implementations§
source§impl<T> Clone for Set<T>where
T: Key,
T::SetStorage: Clone,
impl<T> Clone for Set<T>where
T: Key,
T::SetStorage: Clone,
Clone
implementation for a Set
.
Examples
use fixed_map::{Key, Set};
#[derive(Debug, Clone, Copy, Key)]
enum Key {
First(bool),
Second,
}
let mut a = Set::new();
a.insert(Key::First(true));
let mut b = a.clone();
b.insert(Key::Second);
assert_ne!(a, b);
assert!(a.contains(Key::First(true)));
assert!(!a.contains(Key::Second));
assert!(b.contains(Key::First(true)));
assert!(b.contains(Key::Second));
source§impl<T> Debug for Set<T>where
T: Key + Debug,
impl<T> Debug for Set<T>where
T: Key + Debug,
source§impl<T> Default for Set<T>where
T: Key,
impl<T> Default for Set<T>where
T: Key,
source§impl<'de, T> Deserialize<'de> for Set<T>where
T: Key + Deserialize<'de>,
impl<'de, T> Deserialize<'de> for Set<T>where
T: Key + Deserialize<'de>,
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
source§impl<T> FromIterator<T> for Set<T>where
T: Key,
impl<T> FromIterator<T> for Set<T>where
T: Key,
source§fn from_iter<I>(iter: I) -> Selfwhere
I: IntoIterator<Item = T>,
fn from_iter<I>(iter: I) -> Selfwhere
I: IntoIterator<Item = T>,
source§impl<T> Hash for Set<T>where
T: Key,
T::SetStorage: Hash,
impl<T> Hash for Set<T>where
T: Key,
T::SetStorage: Hash,
Hash
implementation for a Set
.
Examples
use std::collections::HashSet;
use fixed_map::{Key, Set};
#[derive(Debug, Clone, Copy, Hash, Key)]
enum Key {
First,
Second,
}
let mut a = Set::new();
a.insert(Key::First);
let mut set = HashSet::new();
set.insert(a);
Using a composite key:
use std::collections::HashSet;
use fixed_map::{Key, Set};
#[derive(Debug, Clone, Copy, Hash, Key)]
enum Key {
First(bool),
Second,
}
let mut a = Set::new();
a.insert(Key::First(true));
// TODO: support this
// let mut set = HashSet::new();
// set.insert(a);
source§impl<'a, T> IntoIterator for &'a Set<T>where
T: Key,
impl<'a, T> IntoIterator for &'a Set<T>where
T: Key,
source§impl<T> IntoIterator for Set<T>where
T: Key,
impl<T> IntoIterator for Set<T>where
T: Key,
Produce an owning iterator which iterates over all elements in the set in order.
Examples
use fixed_map::{Key, Set};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Key)]
enum Key {
First,
Second,
Third,
}
let mut set = Set::new();
set.insert(Key::First);
set.insert(Key::Second);
assert_eq!(set.into_iter().collect::<Vec<_>>(), vec![Key::First, Key::Second]);
source§fn into_iter(self) -> Self::IntoIter
fn into_iter(self) -> Self::IntoIter
An iterator visiting all values in arbitrary order.
The iterator element type is T
.
Examples
use fixed_map::{Key, Set};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Key)]
enum Key {
One,
Two,
Three,
}
let mut set = Set::new();
set.insert(Key::One);
set.insert(Key::Two);
assert_eq!(set.into_iter().collect::<Vec<_>>(), vec![Key::One, Key::Two]);
§type IntoIter = <<T as Key>::SetStorage as SetStorage<T>>::IntoIter
type IntoIter = <<T as Key>::SetStorage as SetStorage<T>>::IntoIter
source§impl<T> Ord for Set<T>where
T: Key,
T::SetStorage: Ord,
impl<T> Ord for Set<T>where
T: Key,
T::SetStorage: Ord,
For more details on ordering, see the Key
documentation.
Examples
use fixed_map::{Key, Set};
#[derive(Debug, Clone, Copy, Hash, Key)]
enum Key {
First,
Second,
}
let mut a = Set::new();
a.insert(Key::First);
let mut b = Set::new();
b.insert(Key::Second);
let mut list = vec![b, a];
list.sort();
assert_eq!(list, [a, b]);
Using a composite key:
use fixed_map::{Key, Set};
#[derive(Debug, Clone, Copy, Hash, Key)]
enum Key {
First(bool),
Second,
}
let mut a = Set::new();
a.insert(Key::First(true));
let mut b = Set::new();
b.insert(Key::Second);
// TODO: support this
// let mut list = vec![a, b];
// list.sort();
source§impl<T> PartialEq<Set<T>> for Set<T>where
T: Key,
T::SetStorage: PartialEq,
impl<T> PartialEq<Set<T>> for Set<T>where
T: Key,
T::SetStorage: PartialEq,
PartialEq
implementation for a Set
.
Examples
use fixed_map::{Key, Set};
#[derive(Debug, Clone, Copy, Key)]
enum Key {
First,
Second,
}
let mut a = Set::new();
a.insert(Key::First);
// Note: `a` is Copy since it's using a simple key.
let mut b = a;
assert_eq!(a, b);
b.insert(Key::Second);
assert_ne!(a, b);
Using a composite key:
use fixed_map::{Key, Set};
#[derive(Debug, Clone, Copy, Key)]
enum Key {
First(bool),
Second,
}
let mut a = Set::new();
a.insert(Key::First(true));
let mut b = a.clone();
assert_eq!(a, b);
b.insert(Key::Second);
assert_ne!(a, b);
source§impl<T> PartialOrd<Set<T>> for Set<T>where
T: Key,
T::SetStorage: PartialOrd,
impl<T> PartialOrd<Set<T>> for Set<T>where
T: Key,
T::SetStorage: PartialOrd,
PartialOrd
implementation for a Set
.
For more details on ordering, see the Key
documentation.
Examples
use fixed_map::{Key, Set};
#[derive(Debug, Clone, Copy, Hash, Key)]
enum Key {
First,
Second,
Third,
}
let mut a = Set::new();
a.insert(Key::First);
let mut b = Set::new();
b.insert(Key::Third);
assert!(a < b);
let mut empty = Set::new();
assert!(empty < a);
assert!(empty < b);
Using a composite key:
use fixed_map::{Key, Set};
#[derive(Debug, Clone, Copy, Hash, Key)]
enum Key {
First(bool),
Second,
}
let mut a = Set::new();
a.insert(Key::First(true));
let mut b = Set::new();
b.insert(Key::Second);
// TODO: support this
// assert!(a < b);
impl<T> Copy for Set<T>where
T: Key,
T::SetStorage: Copy,
The Copy
implementation for a Set
depends on its Key
. If the
derived key only consists of unit variants the corresponding Set
will be
Copy
as well.
Examples
use fixed_map::{Key, Set};
#[derive(Debug, Clone, Copy, Key)]
enum Key {
First,
Second,
}
let mut a = Set::new();
a.insert(Key::First);
let mut b = a;
b.insert(Key::Second);
assert_ne!(a, b);
assert!(a.contains(Key::First));
assert!(!a.contains(Key::Second));
assert!(b.contains(Key::First));
assert!(b.contains(Key::Second));