pub struct VariantSet<T>where
T: VariantEnum,{ /* private fields */ }Expand description
A set of values that are variants of an enum. The set can contain at most one value for each variant.
Functionally equivalent to a HashSet<T>, but the enum variants can contain complex data.
The enum must implement the VariantEnum trait, you will generally want to derive it using the VariantEnum derive macro.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum, Debug, PartialEq)]
enum MyEnum {
Variant1(String),
Variant2(u32),
Variant3(bool),
}
let mut set = VariantSet::new();
set.set(MyEnum::Variant1("Hello".to_string()));
set.set(MyEnum::Variant2(42));
set.set(MyEnum::Variant3(true));
assert!(set.contains(MyEnumVariant::Variant1));
assert!(set.contains(MyEnumVariant::Variant2));
assert!(set.contains(MyEnumVariant::Variant3));
assert_eq!(set.get(MyEnumVariant::Variant1), Some(&MyEnum::Variant1("Hello".to_string())));
assert_eq!(set.get(MyEnumVariant::Variant2), Some(&MyEnum::Variant2(42)));
assert_eq!(set.get(MyEnumVariant::Variant3), Some(&MyEnum::Variant3(true)));§Performance
The VariantSet is backed by a HashMap and provides constant time insertion, removal, and lookup.
Implementations§
Source§impl<T> VariantSet<T>where
T: VariantEnum,
impl<T> VariantSet<T>where
T: VariantEnum,
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new VariantSet with a default capacity and hasher.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let set: VariantSet<MyEnum> = VariantSet::new();Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new VariantSet with a specified capacity.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let set: VariantSet<MyEnum> = VariantSet::with_capacity(10);
assert!(set.capacity() >= 10);Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements this set can hold without reallocating.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let set: VariantSet<MyEnum> = VariantSet::with_capacity(10);
assert!(set.capacity() >= 10);Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the set, removing all values.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let mut set = VariantSet::new();
set.set(MyEnum::Variant1("Hello".to_string()));
set.clear();
assert!(set.is_empty());Sourcepub fn insert(&mut self, value: T) -> bool
pub fn insert(&mut self, value: T) -> bool
Adds a value to the set.
Returns whether the value was newly inserted. That is:
- If the set did not previously contain this value,
trueis returned. - If the set already contained this value,
falseis returned, and the set is not modified: original value is not replaced, and the value passed as argument is dropped.
Note that if the set already contains a value, but the contained value is not equal to the value passed as argument, the value passed as argument is dropped and false is returned.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let mut set = VariantSet::new();
assert!(set.insert(MyEnum::Variant1("Hello".to_string())));
assert!(!set.insert(MyEnum::Variant1("World".to_string())));Sourcepub fn set(&mut self, value: T) -> Option<T>
pub fn set(&mut self, value: T) -> Option<T>
Sets a value in the set. If a previous value existed, it is returned.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum, PartialEq, Debug)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let mut set = VariantSet::new();
let previous = set.set(MyEnum::Variant1("Hello".to_string()));
assert_eq!(previous, None);
let previous = set.set(MyEnum::Variant1("World".to_string()));
assert_eq!(previous, Some(MyEnum::Variant1("Hello".to_string())));Sourcepub fn contains(&self, value: T::Variant) -> bool
pub fn contains(&self, value: T::Variant) -> bool
Returns true if the set contains a value.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let mut set = VariantSet::new();
set.set(MyEnum::Variant1("Hello".to_string()));
assert!(set.contains(MyEnumVariant::Variant1));Sourcepub fn contains_exact(&self, value: &T) -> boolwhere
T: PartialEq,
pub fn contains_exact(&self, value: &T) -> boolwhere
T: PartialEq,
Returns true if the set contains a value that is equal to the given value.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum, Debug, PartialEq)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let mut set = VariantSet::new();
set.set(MyEnum::Variant1("Hello".to_string()));
assert!(set.contains_exact(&MyEnum::Variant1("Hello".to_string())));
assert!(!set.contains_exact(&MyEnum::Variant1("World".to_string())));Sourcepub fn drain(&mut self) -> impl Iterator<Item = T> + '_
pub fn drain(&mut self) -> impl Iterator<Item = T> + '_
Clears the set, returning all elements as an iterator. Keeps the allocated memory for reuse.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum, Debug, PartialEq)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let mut set = VariantSet::new();
set.set(MyEnum::Variant1("Hello".to_string()));
set.set(MyEnum::Variant2(42));
let values: Vec<_> = set.drain().collect();
assert_eq!(values.len(), 2);
assert!(values.contains(&MyEnum::Variant1("Hello".to_string())));
assert!(values.contains(&MyEnum::Variant2(42)));Sourcepub fn get(&self, value: T::Variant) -> Option<&T>
pub fn get(&self, value: T::Variant) -> Option<&T>
Returns a reference to the value in the set, if any, that is equal to the given value.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum, Debug, PartialEq)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let mut set = VariantSet::new();
set.set(MyEnum::Variant1("Hello".to_string()));
let value = set.get(MyEnumVariant::Variant1);
assert_eq!(value, Some(&MyEnum::Variant1("Hello".to_string())));Sourcepub fn get_or_insert(&mut self, default: T) -> &T
pub fn get_or_insert(&mut self, default: T) -> &T
Inserts the given value into the set if it is not present, then returns a reference to the value in the set.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum, Debug, PartialEq)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let mut set = VariantSet::new();
let value = set.get_or_insert(MyEnum::Variant1("Hello".to_string()));
assert_eq!(value, &MyEnum::Variant1("Hello".to_string()));Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the set contains no elements.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let set: VariantSet<MyEnum> = VariantSet::new();
assert!(set.is_empty());Sourcepub fn iter(&self) -> impl Iterator<Item = &T>
pub fn iter(&self) -> impl Iterator<Item = &T>
An iterator visiting all elements in arbitrary order. The iterator element type is &'a T.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum, Debug)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let mut set = VariantSet::new();
set.set(MyEnum::Variant1("Hello".to_string()));
set.set(MyEnum::Variant2(42));
for value in set.iter() {
println!("{:?}", value);
}Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the set.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let mut set = VariantSet::new();
set.set(MyEnum::Variant1("Hello".to_string()));
set.set(MyEnum::Variant2(42));
assert_eq!(set.len(), 2);Sourcepub fn remove(&mut self, value: T::Variant) -> Option<T>
pub fn remove(&mut self, value: T::Variant) -> Option<T>
Removes a variant from the set. Returns the value if it existed.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum, Debug, PartialEq)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let mut set = VariantSet::new();
set.set(MyEnum::Variant1("Hello".to_string()));
let value = set.remove(MyEnumVariant::Variant1);
assert_eq!(value, Some(MyEnum::Variant1("Hello".to_string())));Sourcepub fn remove_exact(&mut self, value: &T) -> Option<T>where
T: PartialEq,
pub fn remove_exact(&mut self, value: &T) -> Option<T>where
T: PartialEq,
Removes a variant from the set if it is equal to the given value. Returns the value if it existed.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum, Debug, PartialEq)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let mut set = VariantSet::new();
set.set(MyEnum::Variant1("Hello".to_string()));
let not_matching = set.remove_exact(&MyEnum::Variant1("World".to_string()));
assert_eq!(not_matching, None);
let matching = set.remove_exact(&MyEnum::Variant1("Hello".to_string()));
assert_eq!(matching, Some(MyEnum::Variant1("Hello".to_string())));Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional more elements to be inserted in the set.
The collection may reserve more space to avoid frequent reallocations.
After calling reserve, capacity will be greater than or equal to self.len() + additional.
Does nothing if the capacity is already sufficient.
Note that you can reserve more capacity than there are variants in the enum.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let mut set: VariantSet<MyEnum> = VariantSet::new();
set.reserve(10);
assert!(set.capacity() >= 10);Sourcepub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
Tries to reserve capacity for at least additional more elements to be inserted in the set.
The collection may reserve more space to avoid frequent reallocations.
After calling try_reserve, capacity will be greater than or equal to self.len() + additional if it returns Ok(())
Does nothing if the capacity is already sufficient.
Note that you can reserve more capacity than there are variants in the enum.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let mut set: VariantSet<MyEnum> = VariantSet::new();
set.try_reserve(10).unwrap();
assert!(set.capacity() >= 10);§Errors
Returns a std::collections::TryReserveError if the new capacity would overflow usize.
Sourcepub fn shrink_to(&mut self, min_capacity: usize)
pub fn shrink_to(&mut self, min_capacity: usize)
Shrinks the capacity of the set with a lower limit. It will drop down to no lower than the supplied limit while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.
If the current capacity is less than the lower limit, this is a no-op.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let mut set: VariantSet<MyEnum> = VariantSet::new();
set.reserve(10);
set.shrink_to(5);
assert!(set.capacity() >= 5);Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the set as much as possible. It will drop down as much as possible while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let mut set: VariantSet<MyEnum> = VariantSet::new();
set.reserve(10);
set.set(MyEnum::Variant1("Hello".to_string()));
set.shrink_to_fit();
assert!(set.capacity() >= 1);Sourcepub fn take(&mut self, value: T::Variant) -> Option<T>
pub fn take(&mut self, value: T::Variant) -> Option<T>
Removes and returns the value in the set, if any, that is equal to the given value.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum, Debug, PartialEq)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let mut set = VariantSet::new();
set.set(MyEnum::Variant1("Hello".to_string()));
let value = set.take(MyEnumVariant::Variant1);
assert_eq!(value, Some(MyEnum::Variant1("Hello".to_string())));Trait Implementations§
Source§impl<T> Clone for VariantSet<T>where
T: VariantEnum + Clone,
impl<T> Clone for VariantSet<T>where
T: VariantEnum + Clone,
Source§fn clone(&self) -> Self
fn clone(&self) -> Self
Clones the set. The values are cloned using their Clone implementation.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum, Debug, Clone, PartialEq)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let mut set = VariantSet::new();
set.set(MyEnum::Variant1("Hello".to_string()));
set.set(MyEnum::Variant2(42));
let cloned = set.clone();
assert_eq!(set, cloned);1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T> Debug for VariantSet<T>
impl<T> Debug for VariantSet<T>
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Formats the set as a map of variants to values.
The values are formatted using their Debug implementation.
The variants are formatted using their Debug implementation.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum, Debug)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let mut set = VariantSet::new();
set.set(MyEnum::Variant1("Hello".to_string()));
set.set(MyEnum::Variant2(42));
println!("{:?}", set);Source§impl<T> Default for VariantSet<T>where
T: VariantEnum,
impl<T> Default for VariantSet<T>where
T: VariantEnum,
Source§fn default() -> Self
fn default() -> Self
Creates a new VariantSet with a default capacity.
The default capacity is the capacity of a newly created HashMap.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let set: VariantSet<MyEnum> = Default::default();Source§impl<T> Extend<T> for VariantSet<T>where
T: VariantEnum,
impl<T> Extend<T> for VariantSet<T>where
T: VariantEnum,
Source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
Extends the set with the contents of an iterator. If the set already contains a value that maps to the same variant, the value will be replaced. If the iterator yields multiple values that map to the same variant, the last value will be used.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum, Debug, PartialEq)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let mut set = VariantSet::new();
set.set(MyEnum::Variant2(10));
set.extend(vec![MyEnum::Variant1("Hello".to_string()), MyEnum::Variant2(42), MyEnum::Variant1("World".to_string())]);
assert_eq!(set.len(), 2);
assert!(set.contains_exact(&MyEnum::Variant1("World".to_string())));
assert!(set.contains_exact(&MyEnum::Variant2(42)));Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<T, const N: usize> From<[T; N]> for VariantSet<T>where
T: VariantEnum,
impl<T, const N: usize> From<[T; N]> for VariantSet<T>where
T: VariantEnum,
Source§fn from(array: [T; N]) -> Self
fn from(array: [T; N]) -> Self
Creates a new VariantSet from an array.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let array = [MyEnum::Variant1("Hello".to_string()), MyEnum::Variant2(42)];
let set = VariantSet::from(array);
assert_eq!(set.len(), 2);Source§impl<T> FromIterator<T> for VariantSet<T>where
T: VariantEnum,
impl<T> FromIterator<T> for VariantSet<T>where
T: VariantEnum,
Source§fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
Creates a new VariantSet from an iterator.
If the iterator yields multiple values that map to the same variant, the last value will be used.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum, Debug, PartialEq)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let iter = vec![MyEnum::Variant1("Hello".to_string()), MyEnum::Variant2(42), MyEnum::Variant1("World".to_string())].into_iter();
let set = VariantSet::from_iter(iter);
assert_eq!(set.len(), 2);
assert!(set.contains_exact(&MyEnum::Variant1("World".to_string())));Source§impl<T> IntoIterator for VariantSet<T>where
T: VariantEnum,
impl<T> IntoIterator for VariantSet<T>where
T: VariantEnum,
Source§fn into_iter(self) -> Self::IntoIter
fn into_iter(self) -> Self::IntoIter
Consumes the set and returns an iterator over the values.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum, Debug, PartialEq)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let mut set = VariantSet::new();
set.set(MyEnum::Variant1("Hello".to_string()));
set.set(MyEnum::Variant2(42));
let values: Vec<_> = set.into_iter().collect();
assert_eq!(values.len(), 2);
assert!(values.contains(&MyEnum::Variant1("Hello".to_string())));
assert!(values.contains(&MyEnum::Variant2(42)));Source§type IntoIter = IntoValues<<T as VariantEnum>::Variant, T>
type IntoIter = IntoValues<<T as VariantEnum>::Variant, T>
Source§impl<T> PartialEq for VariantSet<T>where
T: VariantEnum + PartialEq,
impl<T> PartialEq for VariantSet<T>where
T: VariantEnum + PartialEq,
Source§fn eq(&self, other: &Self) -> bool
fn eq(&self, other: &Self) -> bool
Compares two sets for equality. Two sets are equal if they contain the same variants, regardless of the order. The values of the variants must be equal for the sets to be equal.
§Examples
use variant_set::{VariantSet, VariantEnum};
#[derive(VariantEnum, Debug, PartialEq)]
enum MyEnum {
Variant1(String),
Variant2(u32),
}
let mut set1 = VariantSet::new();
set1.set(MyEnum::Variant1("Hello".to_string()));
set1.set(MyEnum::Variant2(42));
let mut set2 = VariantSet::new();
set2.set(MyEnum::Variant2(42));
set2.set(MyEnum::Variant1("Hello".to_string()));
assert_eq!(set1, set2);