Struct VariantSet

Source
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,

Source

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();
Source

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);
Source

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);
Source

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());
Source

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, true is returned.
  • If the set already contained this value, false is 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())));
Source

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())));
Source

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));
Source

pub fn contains_exact(&self, value: &T) -> bool
where 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())));
Source

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)));
Source

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())));
Source

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()));
Source

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());
Source

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);
}
Source

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);
Source

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())));
Source

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())));
Source

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);
Source

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.

Source

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);
Source

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);
Source

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,

Source§

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)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for VariantSet<T>
where T: VariantEnum + Debug, T::Variant: Debug,

Source§

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,

Source§

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,

Source§

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)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T, const N: usize> From<[T; N]> for VariantSet<T>
where T: VariantEnum,

Source§

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,

Source§

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,

Source§

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 Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoValues<<T as VariantEnum>::Variant, T>

Which kind of iterator are we turning this into?
Source§

impl<T> PartialEq for VariantSet<T>

Source§

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);
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Eq for VariantSet<T>
where T: VariantEnum + Eq,

Auto Trait Implementations§

§

impl<T> Freeze for VariantSet<T>

§

impl<T> RefUnwindSafe for VariantSet<T>

§

impl<T> Send for VariantSet<T>
where <T as VariantEnum>::Variant: Send, T: Send,

§

impl<T> Sync for VariantSet<T>
where <T as VariantEnum>::Variant: Sync, T: Sync,

§

impl<T> Unpin for VariantSet<T>
where <T as VariantEnum>::Variant: Unpin, T: Unpin,

§

impl<T> UnwindSafe for VariantSet<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.