Struct SpellSet

Source
pub struct SpellSet { /* private fields */ }
Expand description

Contains up to 2 spells. Although the underlying data structure is an array, this structure behaves like a set. Most methods mimic those of HashSet.

This struct solves the following problems:

  • An item can only hold up to 2 spells.
  • An item cannot have duplicate spells or multiple spells of the same type.
  • Comparing spells for equality is order-agnostic.
  • Hashing is order-agnostic.
  • The type is Copy, allowing for cheap and easy duplication.

§Examples

use tf2_enum::{SpellSet, Spell};
 
// Create a set for spells with one spell.
let mut spells = SpellSet::single(Spell::HeadlessHorseshoes);
 
// Check that spells contains Headless Horseshoes.
assert!(spells.contains(&Spell::HeadlessHorseshoes));
assert_eq!(spells.len(), 1);
 
// Add a spell.
spells.insert(Spell::VoicesFromBelow);
assert_eq!(spells.len(), 2);
 
// If a spell is added when spells are full, the insert will fail.
assert!(!spells.insert(Spell::PumpkinBombs));
assert!(!spells.contains(&Spell::PumpkinBombs));
 
// Iterate over spells.
for spell in spells {
    println!("{}", spell);
}

Implementations§

Source§

impl SpellSet

Source

pub fn new() -> Self

Creates a set for spells.

§Examples
use tf2_enum::SpellSet;
 
let spells = SpellSet::new();
Source

pub fn single(spell: Spell) -> Self

Creates a set for spells with one spell.

§Examples
use tf2_enum::{SpellSet, Spell};
 
let spells = SpellSet::single(Spell::HeadlessHorseshoes);
 
assert_eq!(spells.len(), 1);
Source

pub fn double(spell1: Spell, spell2: Spell) -> Self

Creates a set for spells with two spells.

If the same spell is added multiple times, only one will be kept. This is also true for spells of the same type. In cases of multiple spells of the same type, the first occuring spell will be prioritized.

§Examples
use tf2_enum::{SpellSet, Spell};
 
let spells = SpellSet::double(Spell::HeadlessHorseshoes, Spell::VoicesFromBelow);
 
assert_eq!(spells.len(), 2);
 
let spells = SpellSet::double(Spell::HeadlessHorseshoes, Spell::TeamSpiritFootprints);
 
assert_eq!(spells.len(), 1);
assert_eq!(SpellSet::single(Spell::HeadlessHorseshoes), spells);
Source

pub fn clear(&mut self)

Clears the set, removing all spells.

§Examples
use tf2_enum::{SpellSet, Spell};
 
let mut spells = SpellSet::single(Spell::HeadlessHorseshoes);
 
spells.clear();
 
assert_eq!(spells.len(), 0);
Source

pub fn insert(&mut self, spell: Spell) -> bool

Adds a spell to the first available slot.

Returns false if:

  • The spell is already in the set.
  • The set is full.
§Examples
use tf2_enum::{SpellSet, Spell};
 
let mut spells = SpellSet::single(Spell::HeadlessHorseshoes);
 
assert_eq!(spells.len(), 1);
 
spells.insert(Spell::VoicesFromBelow);
 
assert_eq!(spells.len(), 2);
 
// Spells are full.
assert!(!spells.insert(Spell::PumpkinBombs));
Source

pub fn remove(&mut self, spell: &Spell) -> bool

Removes a spell from the set. Returns whether the value was present in the set.

§Examples
use tf2_enum::{SpellSet, Spell};
 
let mut spells = SpellSet::single(Spell::HeadlessHorseshoes);
 
assert!(spells.remove(&Spell::HeadlessHorseshoes));
assert!(!spells.contains(&Spell::HeadlessHorseshoes));
Source

pub fn take(&mut self, spell: &Spell) -> Option<Spell>

Removes and returns the spell in the set, if any, that is equal to the given one.

Source

pub fn is_empty(&self) -> bool

Returns true if the set contains no spells.

Source

pub fn contains(&self, spell: &Spell) -> bool

Returns true if the set contains a spell.

§Examples
use tf2_enum::{SpellSet, Spell};
 
let mut spells = SpellSet::single(Spell::HeadlessHorseshoes);
 
assert!(spells.contains(&Spell::HeadlessHorseshoes));
Source

pub fn len(&self) -> usize

Returns the number of spells in the set.

§Examples
use tf2_enum::{SpellSet, Spell};
 
let spells = SpellSet::single(Spell::HeadlessHorseshoes);
 
assert_eq!(spells.len(), 1);
Source

pub fn difference(&self, other: &Self) -> Self

Returns the spells that are in self but not in other.

§Examples
use tf2_enum::{SpellSet, Spell};
 
let spells1 = SpellSet::double(Spell::HalloweenFire, Spell::Exorcism);
let spells2 = SpellSet::double(Spell::HalloweenFire, Spell::VoicesFromBelow);
let difference = spells1.difference(&spells2);
 
assert_eq!(difference, SpellSet::single(Spell::Exorcism));
 
let difference = spells2.difference(&spells1);
 
assert_eq!(difference, SpellSet::single(Spell::VoicesFromBelow));
Source

pub fn intersection(&self, other: &Self) -> Self

Returns the spells that are both in self and other.

§Examples
use tf2_enum::{SpellSet, Spell};
 
let spells1 = SpellSet::double(Spell::HalloweenFire, Spell::Exorcism);
let spells2 = SpellSet::double(Spell::HalloweenFire, Spell::VoicesFromBelow);
let intersection = spells1.intersection(&spells2);
 
assert_eq!(intersection, SpellSet::single(Spell::HalloweenFire));
Source

pub fn is_disjoint(&self, other: &Self) -> bool

Returns true if self has no spells in common with other. This is equivalent to checking for an empty intersection.

§Examples
use tf2_enum::{SpellSet, Spell};
 
let spells1 = SpellSet::double(Spell::HalloweenFire, Spell::Exorcism);
let spells2 = SpellSet::double(Spell::HalloweenFire, Spell::VoicesFromBelow);
 
assert!(!spells1.is_disjoint(&spells2));
Source

pub fn is_subset(&self, other: &Self) -> bool

Returns true if the set is a subset of another, i.e., other contains at least all the values in self.

§Examples
use tf2_enum::{SpellSet, Spell};

let sup = SpellSet::double(Spell::HalloweenFire, Spell::Exorcism);
let mut spells = SpellSet::single(Spell::HalloweenFire);

assert!(spells.is_subset(&sup));
 
spells.insert(Spell::HeadlessHorseshoes);
 
assert!(!spells.is_subset(&sup));
Source

pub fn is_superset(&self, other: &Self) -> bool

Returns true if the set is a superset of another, i.e., self contains at least all the values in other.

§Examples
use tf2_enum::{SpellSet, Spell};

let sub = SpellSet::double(Spell::HalloweenFire, Spell::Exorcism);
let mut spells = SpellSet::new();

assert!(!spells.is_superset(&sub));
 
spells.insert(Spell::HalloweenFire);
 
assert!(!spells.is_superset(&sub));
 
spells.insert(Spell::Exorcism);
 
assert!(spells.is_superset(&sub));
Source

pub fn iter(&self) -> impl Iterator<Item = &Spell>

Returns an iterator over the spells in the set.

Trait Implementations§

Source§

impl BitAnd for &SpellSet

Source§

type Output = SpellSet

The resulting type after applying the & operator.
Source§

fn bitand(self, other: &SpellSet) -> Self::Output

Performs the & operation. Read more
Source§

impl BitAnd for SpellSet

Source§

type Output = SpellSet

The resulting type after applying the & operator.
Source§

fn bitand(self, other: Self) -> Self::Output

Performs the & operation. Read more
Source§

impl Clone for SpellSet

Source§

fn clone(&self) -> SpellSet

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SpellSet

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SpellSet

Source§

fn default() -> SpellSet

Returns the “default value” for a type. Read more
Source§

impl Display for SpellSet

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<[Option<Spell>; 2]> for SpellSet

Source§

fn from(inner: [Option<Spell>; 2]) -> Self

Converts to this type from the input type.
Source§

impl FromIterator<Spell> for SpellSet

Source§

fn from_iter<I: IntoIterator<Item = Spell>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl Hash for SpellSet

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl IntoIterator for &SpellSet

Source§

type Item = Spell

The type of the elements being iterated over.
Source§

type IntoIter = SpellSetIterator

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl IntoIterator for SpellSet

Source§

type Item = Spell

The type of the elements being iterated over.
Source§

type IntoIter = SpellSetIterator

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl PartialEq for SpellSet

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
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 Sub for &SpellSet

Source§

type Output = SpellSet

The resulting type after applying the - operator.
Source§

fn sub(self, other: &SpellSet) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub for SpellSet

Source§

type Output = SpellSet

The resulting type after applying the - operator.
Source§

fn sub(self, other: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl Copy for SpellSet

Source§

impl Eq for SpellSet

Auto Trait Implementations§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.