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 MyKey {
Simple,
Composite(Part),
String(&'static str),
Number(u32),
Singleton(()),
Option(Option<Part>),
Boolean(bool),
}
let mut set = Set::new();
set.insert(MyKey::Simple);
set.insert(MyKey::Composite(Part::One));
set.insert(MyKey::String("foo"));
set.insert(MyKey::Number(1));
set.insert(MyKey::Singleton(()));
set.insert(MyKey::Option(None));
set.insert(MyKey::Option(Some(Part::One)));
set.insert(MyKey::Boolean(true));
assert!(set.contains(MyKey::Simple));
assert!(set.contains(MyKey::Composite(Part::One)));
assert!(!set.contains(MyKey::Composite(Part::Two)));
assert!(set.contains(MyKey::String("foo")));
assert!(!set.contains(MyKey::String("bar")));
assert!(set.contains(MyKey::Number(1)));
assert!(!set.contains(MyKey::Number(2)));
assert!(set.contains(MyKey::Singleton(())));
assert!(set.contains(MyKey::Option(None)));
assert!(set.contains(MyKey::Option(Some(Part::One))));
assert!(!set.contains(MyKey::Option(Some(Part::Two))));
assert!(set.contains(MyKey::Boolean(true)));
assert!(!set.contains(MyKey::Boolean(false)));Implementations§
Source§impl<T> Set<T>where
T: Key,
A set implementation that uses fixed storage.
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 MyKey {
First,
Second,
}
let mut m = Set::new();
m.insert(MyKey::First);
assert_eq!(m.contains(MyKey::First), true);
assert_eq!(m.contains(MyKey::Second), false);Using a composite key:
use fixed_map::{Key, Set};
#[derive(Clone, Copy, Key)]
enum Part {
A,
B,
}
#[derive(Clone, Copy, Key)]
enum MyKey {
Simple,
Composite(Part),
}
let mut m = Set::new();
m.insert(MyKey::Simple);
m.insert(MyKey::Composite(Part::A));
assert_eq!(m.contains(MyKey::Simple), true);
assert_eq!(m.contains(MyKey::Composite(Part::A)), true);
assert_eq!(m.contains(MyKey::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, Key, PartialEq, Eq)]
enum MyKey {
One,
Two,
Three,
}
let mut set = Set::new();
set.insert(MyKey::One);
set.insert(MyKey::Two);
assert_eq!(set.iter().collect::<Vec<_>>(), vec![MyKey::One, MyKey::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 MyKey {
One,
Two,
}
let mut set = Set::new();
set.insert(MyKey::One);
assert_eq!(set.contains(MyKey::One), true);
assert_eq!(set.contains(MyKey::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 MyKey {
One,
Two,
}
let mut set = Set::new();
assert!(set.insert(MyKey::One));
assert!(!set.is_empty());
set.insert(MyKey::Two);
assert!(!set.insert(MyKey::Two));
assert!(set.contains(MyKey::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 MyKey {
One,
Two,
}
let mut set = Set::new();
set.insert(MyKey::One);
assert_eq!(set.remove(MyKey::One), true);
assert_eq!(set.remove(MyKey::One), false);Sourcepub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
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 MyKey {
First,
Second,
}
let mut set = Set::new();
set.insert(MyKey::First);
set.insert(MyKey::Second);
set.retain(|k| matches!(k, MyKey::First));
assert_eq!(set.len(), 1);
assert_eq!(set.contains(MyKey::First), true);
assert_eq!(set.contains(MyKey::Second), false);Using a composite key:
use fixed_map::{Key, Set};
#[derive(Clone, Copy, Key)]
enum MyKey {
First(bool),
Second(bool),
}
let mut set = Set::new();
set.insert(MyKey::First(true));
set.insert(MyKey::First(false));
set.insert(MyKey::Second(true));
set.insert(MyKey::Second(false));
let mut other = set.clone();
assert_eq!(set.len(), 4);
set.retain(|k| matches!(k, MyKey::First(true) | MyKey::Second(true)));
assert_eq!(set.len(), 2);
assert_eq!(set.contains(MyKey::First(true)), true);
assert_eq!(set.contains(MyKey::First(false)), false);
assert_eq!(set.contains(MyKey::Second(true)), true);
assert_eq!(set.contains(MyKey::Second(false)), false);
other.retain(|k| matches!(k, MyKey::First(_)));
assert_eq!(other.len(), 2);
assert_eq!(other.contains(MyKey::First(true)), true);
assert_eq!(other.contains(MyKey::First(false)), true);
assert_eq!(other.contains(MyKey::Second(true)), false);
assert_eq!(other.contains(MyKey::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 MyKey {
One,
Two,
}
let mut set = Set::new();
set.insert(MyKey::One);
set.clear();
assert!(set.is_empty());Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the set contains no elements.
§Examples
use fixed_map::{Key, Set};
#[derive(Clone, Copy, Key)]
enum MyKey {
One,
Two,
}
let mut set = Set::new();
assert!(set.is_empty());
set.insert(MyKey::One);
assert!(!set.is_empty());Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the set.
§Examples
use fixed_map::{Key, Set};
#[derive(Clone, Copy, Key)]
enum MyKey {
First,
Second,
}
let mut set = Set::new();
assert_eq!(set.len(), 0);
set.insert(MyKey::First);
assert_eq!(set.len(), 1);Sourcepub fn intersection<'a>(&'a self, other: &'a Set<T>) -> Intersection<'a, T> ⓘ
pub fn intersection<'a>(&'a self, other: &'a Set<T>) -> Intersection<'a, T> ⓘ
Visits the values representing the intersection, i.e., the values that
are both in self and other.
When an equal element is present in self and other then the
resulting Intersection may yield references to one or the other. This
can be relevant if T contains fields which are not compared by its
Eq implementation, and may hold different value between the two equal
copies of T in the two sets.
§Examples
use fixed_map::{Key, Set};
#[derive(Clone, Copy, Key, Debug)]
enum K {
One,
Two,
Three,
}
let a = Set::from([K::One]);
let b = Set::from([K::One, K::Two, K::Two]);
let intersection = a.intersection(&b).collect::<Set<_>>();
assert_eq!(intersection, Set::from([K::One]));Source§impl<T> Set<T>
impl<T> Set<T>
Sourcepub fn as_raw(&self) -> <T::SetStorage as RawStorage>::Value
pub fn as_raw(&self) -> <T::SetStorage as RawStorage>::Value
Get the raw value of the set.
§Examples
use fixed_map::{Key, Set};
#[derive(Debug, Clone, Copy, Key)]
#[key(bitset)]
enum MyKey {
First,
Second,
}
let mut set = Set::new();
assert!(set.as_raw() == 0);
set.insert(MyKey::First);
assert!(set.as_raw() != 0);
let set2 = Set::from_raw(set.as_raw());
assert_eq!(set, set2);Sourcepub fn from_raw(raw: <T::SetStorage as RawStorage>::Value) -> Self
pub fn from_raw(raw: <T::SetStorage as RawStorage>::Value) -> Self
Construct the set from a raw value.
§Examples
use fixed_map::{Key, Set};
#[derive(Debug, Clone, Copy, Key)]
#[key(bitset)]
enum MyKey {
First,
Second,
}
let mut set = Set::new();
assert!(set.as_raw() == 0);
set.insert(MyKey::First);
assert!(set.as_raw() != 0);
let set2 = Set::from_raw(set.as_raw());
assert_eq!(set, set2);Trait Implementations§
Source§impl<T> Clone for Set<T>
impl<T> Clone for Set<T>
§Examples
use fixed_map::{Key, Set};
#[derive(Debug, Clone, Copy, Key)]
enum MyKey {
First(bool),
Second,
}
let mut a = Set::new();
a.insert(MyKey::First(true));
let mut b = a.clone();
b.insert(MyKey::Second);
assert_ne!(a, b);
assert!(a.contains(MyKey::First(true)));
assert!(!a.contains(MyKey::Second));
assert!(b.contains(MyKey::First(true)));
assert!(b.contains(MyKey::Second));impl<T> Copy for Set<T>
§Examples
use fixed_map::{Key, Set};
#[derive(Debug, Clone, Copy, Key)]
enum MyKey {
First,
Second,
}
let mut a = Set::new();
a.insert(MyKey::First);
let mut b = a;
b.insert(MyKey::Second);
assert_ne!(a, b);
assert!(a.contains(MyKey::First));
assert!(!a.contains(MyKey::Second));
assert!(b.contains(MyKey::First));
assert!(b.contains(MyKey::Second));Source§impl<T> Debug for Set<T>
impl<T> Debug for Set<T>
§Examples
use fixed_map::{Key, Set};
#[derive(Debug, Clone, Copy, Key)]
enum MyKey {
First,
Second,
}
let mut a = Set::new();
a.insert(MyKey::First);
assert_eq!("{First}", format!("{:?}", a));Source§impl<'de, M, A, T> Decode<'de, M, A> for Set<T>
Available on crate feature musli02 only.
impl<'de, M, A, T> Decode<'de, M, A> for Set<T>
musli02 only.Source§fn decode<D>(decoder: D) -> Result<Self, D::Error>where
D: Decoder<'de, Mode = M, Allocator = A>,
fn decode<D>(decoder: D) -> Result<Self, D::Error>where
D: Decoder<'de, Mode = M, Allocator = A>,
Source§const IS_BITWISE_DECODE: bool = false
const IS_BITWISE_DECODE: bool = false
Source§impl<T> Default for Set<T>where
T: Key,
impl<T> Default for Set<T>where
T: Key,
§Examples
use fixed_map::{Key, Set};
#[derive(Debug, Clone, Copy, Key)]
enum MyKey {
First,
Second,
}
let a = Set::<MyKey>::default();
let b = Set::<MyKey>::new();
assert_eq!(a, b);Source§impl<'de, T> Deserialize<'de> for Set<T>where
T: Key + Deserialize<'de>,
Available on crate feature serde only.
impl<'de, T> Deserialize<'de> for Set<T>where
T: Key + Deserialize<'de>,
serde only.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<M, T> Encode<M> for Set<T>
Available on crate feature musli02 only.
impl<M, T> Encode<M> for Set<T>
musli02 only.Source§fn encode<E>(&self, encoder: E) -> Result<(), E::Error>where
E: Encoder<Mode = M>,
fn encode<E>(&self, encoder: E) -> Result<(), E::Error>where
E: Encoder<Mode = M>,
Source§const IS_BITWISE_ENCODE: bool = false
const IS_BITWISE_ENCODE: bool = false
impl<T> Eq for Set<T>
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>
impl<T> Hash for Set<T>
§Examples
use std::collections::HashSet;
use fixed_map::{Key, Set};
#[derive(Debug, Clone, Copy, Key, Hash)]
enum MyKey {
First,
Second,
}
let mut a = Set::new();
a.insert(MyKey::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, Key, Hash)]
enum MyKey {
First(bool),
Second,
}
let mut a = Set::new();
a.insert(MyKey::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,
Produce an owning iterator which iterates over all elements in the set in
order.
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, Key, PartialEq, Eq)]
enum MyKey {
First,
Second,
Third,
}
let mut set = Set::new();
set.insert(MyKey::First);
set.insert(MyKey::Second);
assert_eq!(set.into_iter().collect::<Vec<_>>(), vec![MyKey::First, MyKey::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, Key, PartialEq, Eq)]
enum MyKey {
One,
Two,
Three,
}
let mut set = Set::new();
set.insert(MyKey::One);
set.insert(MyKey::Two);
assert_eq!(set.into_iter().collect::<Vec<_>>(), vec![MyKey::One, MyKey::Two]);Source§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>
impl<T> Ord for Set<T>
For more details on ordering, see the Key documentation.
§Examples
use fixed_map::{Key, Set};
#[derive(Debug, Clone, Copy, Key, Hash)]
enum MyKey {
First,
Second,
}
let mut a = Set::new();
a.insert(MyKey::First);
let mut b = Set::new();
b.insert(MyKey::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, Key, Hash)]
enum MyKey {
First(bool),
Second,
}
let mut a = Set::new();
a.insert(MyKey::First(true));
let mut b = Set::new();
b.insert(MyKey::Second);
// TODO: support this
// let mut list = vec![a, b];
// list.sort();Source§impl<T> PartialEq for Set<T>
impl<T> PartialEq for Set<T>
§Examples
use fixed_map::{Key, Set};
#[derive(Debug, Clone, Copy, Key)]
enum MyKey {
First,
Second,
}
let mut a = Set::new();
a.insert(MyKey::First);
// Note: `a` is Copy since it's using a simple key.
let mut b = a;
assert_eq!(a, b);
b.insert(MyKey::Second);
assert_ne!(a, b);Using a composite key:
use fixed_map::{Key, Set};
#[derive(Debug, Clone, Copy, Key)]
enum MyKey {
First(bool),
Second,
}
let mut a = Set::new();
a.insert(MyKey::First(true));
let mut b = a.clone();
assert_eq!(a, b);
b.insert(MyKey::Second);
assert_ne!(a, b);Source§impl<T> PartialOrd for Set<T>
PartialOrd implementation for a Set.
impl<T> PartialOrd for Set<T>
PartialOrd implementation for a Set.
For more details on ordering, see the Key documentation.
§Examples
use fixed_map::{Key, Set};
#[derive(Debug, Clone, Copy, Key, Hash)]
enum MyKey {
First,
Second,
Third,
}
let mut a = Set::new();
a.insert(MyKey::First);
let mut b = Set::new();
b.insert(MyKey::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, Key, Hash)]
enum MyKey {
First(bool),
Second,
}
let mut a = Set::new();
a.insert(MyKey::First(true));
let mut b = Set::new();
b.insert(MyKey::Second);
// TODO: support this
// assert!(a < b);Source§impl<T> ZeroCopy for Set<T>
Available on crate feature musli-zerocopy01 only.
impl<T> ZeroCopy for Set<T>
musli-zerocopy01 only.Source§const ANY_BITS: bool = <<T::SetStorage as RawStorage>::Value as musli_zerocopy01::ZeroCopy>::ANY_BITS
const ANY_BITS: bool = <<T::SetStorage as RawStorage>::Value as musli_zerocopy01::ZeroCopy>::ANY_BITS
size_of::<Self>() bytes.Source§const PADDED: bool = <<T::SetStorage as RawStorage>::Value as musli_zerocopy01::ZeroCopy>::PADDED
const PADDED: bool = <<T::SetStorage as RawStorage>::Value as musli_zerocopy01::ZeroCopy>::PADDED
Source§const CAN_SWAP_BYTES: bool = <<T::SetStorage as RawStorage>::Value as
musli_zerocopy01::ZeroCopy>::CAN_SWAP_BYTES
const CAN_SWAP_BYTES: bool = <<T::SetStorage as RawStorage>::Value as musli_zerocopy01::ZeroCopy>::CAN_SWAP_BYTES
Source§fn swap_bytes<E>(self) -> Selfwhere
E: ByteOrder,
fn swap_bytes<E>(self) -> Selfwhere
E: ByteOrder,
self using the specified byte ordering to match the
native byte ordering. Read moreSource§fn initialize_padding(&mut self)
fn initialize_padding(&mut self)
Source§fn to_bytes(&self) -> &[u8] ⓘ
fn to_bytes(&self) -> &[u8] ⓘ
ZeroCopy type into bytes. Read moreSource§fn to_bytes_padded(&mut self) -> &[u8] ⓘ
fn to_bytes_padded(&mut self) -> &[u8] ⓘ
Source§fn to_bytes_mut(&mut self) -> &mut [u8] ⓘ
fn to_bytes_mut(&mut self) -> &mut [u8] ⓘ
Source§fn to_bytes_mut_padded(&mut self) -> &mut [u8] ⓘ
fn to_bytes_mut_padded(&mut self) -> &mut [u8] ⓘ
ZeroCopy type into mutable bytes
assuming it might be Self::PADDED. Read moreSource§unsafe fn to_bytes_mut_unchecked(&mut self) -> &mut [u8] ⓘ
unsafe fn to_bytes_mut_unchecked(&mut self) -> &mut [u8] ⓘ
ZeroCopy type into mutable bytes. Read moreSource§fn from_bytes(bytes: &[u8]) -> Result<&Self, Error>
fn from_bytes(bytes: &[u8]) -> Result<&Self, Error>
Self. Read moreSource§fn from_bytes_mut(bytes: &mut [u8]) -> Result<&mut Self, Error>
fn from_bytes_mut(bytes: &mut [u8]) -> Result<&mut Self, Error>
Self. Read moreAuto Trait Implementations§
impl<T> Freeze for Set<T>
impl<T> RefUnwindSafe for Set<T>
impl<T> Send for Set<T>
impl<T> Sync for Set<T>
impl<T> Unpin for Set<T>
impl<T> UnsafeUnpin for Set<T>
impl<T> UnwindSafe for Set<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
impl<M, D, A> DecodeOwned<M, A> for D
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.