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
impl SpellSet
Sourcepub fn single(spell: Spell) -> Self
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);
Sourcepub fn double(spell1: Spell, spell2: Spell) -> Self
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);
Sourcepub fn clear(&mut self)
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);
Sourcepub fn insert(&mut self, spell: Spell) -> bool
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));
Sourcepub fn remove(&mut self, spell: &Spell) -> bool
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));
Sourcepub fn take(&mut self, spell: &Spell) -> Option<Spell>
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.
Sourcepub fn contains(&self, spell: &Spell) -> bool
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));
Sourcepub fn len(&self) -> usize
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);
Sourcepub fn difference(&self, other: &Self) -> Self
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));
Sourcepub fn intersection(&self, other: &Self) -> Self
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));
Sourcepub fn is_disjoint(&self, other: &Self) -> bool
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));
Sourcepub fn is_subset(&self, other: &Self) -> bool
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));
Sourcepub fn is_superset(&self, other: &Self) -> bool
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));