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
,
with a few differences.
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.
Most methods are implemented under the AttributeSet
trait, make sure to import it to make
use of them.
§Examples
use tf2_enum::{SpellSet, Spell, AttributeSet};
// 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, AttributeSet};
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, AttributeSet};
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);
Trait Implementations§
Source§impl AttributeSet for SpellSet
impl AttributeSet for SpellSet
Source§fn clear(&mut self)
fn clear(&mut self)
Clears the set, removing all spells.
§Examples
use tf2_enum::{SpellSet, Spell, AttributeSet};
let mut spells = SpellSet::single(Spell::HeadlessHorseshoes);
spells.clear();
assert_eq!(spells.len(), 0);
Source§fn insert(&mut self, spell: Spell) -> bool
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, AttributeSet};
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§fn remove(&mut self, spell: &Spell) -> bool
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, AttributeSet};
let mut spells = SpellSet::single(Spell::HeadlessHorseshoes);
assert!(spells.remove(&Spell::HeadlessHorseshoes));
assert!(!spells.contains(&Spell::HeadlessHorseshoes));
Source§fn take(&mut self, spell: &Spell) -> Option<Spell>
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§fn replace(&mut self, spell: &Spell, new_spell: Spell) -> bool
fn replace(&mut self, spell: &Spell, new_spell: Spell) -> bool
Replaces a spell in the set with a new spell. false
if the spell was not present.
Source§fn iter_attributes(&self) -> impl Iterator<Item = ItemAttribute>
fn iter_attributes(&self) -> impl Iterator<Item = ItemAttribute>
Converts each element to an ItemAttribute
.
Source§fn as_mut_slice(&mut self) -> &mut [Option<Spell>]
fn as_mut_slice(&mut self) -> &mut [Option<Spell>]
Returns the inner storage as a mutable slice.
Source§fn try_insert(&mut self, spell: Spell) -> Result<(), InsertError>
fn try_insert(&mut self, spell: Spell) -> Result<(), InsertError>
insert
, but returns a std::result::Result
with descriptive error to identify
why the insert failed.Source§fn insert_or_replace_last(&mut self, spell: Spell) -> bool
fn insert_or_replace_last(&mut self, spell: Spell) -> bool
false
if the set already contains the value.Source§fn contains(&self, item: &Self::Item) -> bool
fn contains(&self, item: &Self::Item) -> bool
Source§fn is_full(&self) -> bool
fn is_full(&self) -> bool
Source§fn difference(&self, other: &Self) -> Self
fn difference(&self, other: &Self) -> Self
self
but not in other
.Source§fn intersection(&self, other: &Self) -> Self
fn intersection(&self, other: &Self) -> Self
self
and other
.Source§fn is_disjoint(&self, other: &Self) -> bool
fn is_disjoint(&self, other: &Self) -> bool
true
if self
has no items in common with other
. This is equivalent to
checking for an empty intersection.