SpellSet

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

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, AttributeSet};
 
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, 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

Source§

const MAX_COUNT: usize = 2usize

Max number of items.

Source§

const NONE: Self

An empty SpellSet.

Source§

type Item = Spell

The item type.

Source§

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

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

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>

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

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>

Converts each element to an ItemAttribute.

Source§

fn as_slice(&self) -> &[Option<Spell>]

Returns the inner storage as a slice.

Source§

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>

Same as 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

Adds an item to the first available slot. Replaces the last item in the set if the set is full. Returns false if the set already contains the value.
Source§

fn get(&self, index: usize) -> Option<&Self::Item>

Gets an item from the set by index.
Source§

fn len(&self) -> usize

Returns the number of elements in the set.
Source§

fn contains(&self, item: &Self::Item) -> bool

Returns true if the set contains the given item.
Source§

fn is_empty(&self) -> bool

Returns true if the set is empty.
Source§

fn is_full(&self) -> bool

Returns true if the set is full, i.e., it contains the maximum number of elements.
Source§

fn capacity(&self) -> usize

Gets the capacity of the set.
Source§

fn first(&self) -> Option<&Self::Item>

Gets the first item in the set.
Source§

fn last(&self) -> Option<&Self::Item>

Gets the last item in the set.
Source§

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

Returns the items that are in self but not in other.
Source§

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

Returns the items that are both in self and other.
Source§

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

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

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.
Source§

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.
Source§

fn iter(&self) -> impl Iterator<Item = &Self::Item>

Returns an iterator over the set.
Source§

fn iter_mut(&mut self) -> impl Iterator<Item = &mut Self::Item>

Returns a mutable iterator over the set.
Source§

fn retain<F>(&mut self, f: F)
where F: FnMut(&Self::Item) -> bool,

Retains only the items specified by the predicate.
Source§

fn extend<I: IntoIterator<Item = Self::Item>>(&mut self, iter: I)

Extends items from an iterator into the set.
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<'de> Deserialize<'de> for SpellSet

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. 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<&SpellSet> for Vec<Spell>

Source§

fn from(spell_set: &SpellSet) -> Self

Converts to this type from the input type.
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 From<SpellSet> for Vec<Spell>

Source§

fn from(spell_set: SpellSet) -> Self

Converts to this type from the input type.
Source§

impl<'a> FromIterator<&'a Option<Spell>> for SpellSet

Source§

fn from_iter<I: IntoIterator<Item = &'a Option<Spell>>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<'a> FromIterator<&'a Spell> for SpellSet

Source§

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

Creates a value from an iterator. Read more
Source§

impl FromIterator<Option<Spell>> for SpellSet

Source§

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

Creates a value from an iterator. Read more
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 Ord for SpellSet

Source§

fn cmp(&self, other: &SpellSet) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. 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 PartialOrd for SpellSet

Source§

fn partial_cmp(&self, other: &SpellSet) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for SpellSet

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,