Struct StrangePartSet

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

Contains up to 3 strange parts. 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 3 strange parts.
  • An item cannot have duplicate strange parts.
  • Comparing strange parts for equality is order-agnostic.
  • Hashing is order-agnostic.
  • The type is Copy, allowing for cheap and easy duplication.

§Examples

use tf2_enum::{StrangePartSet, StrangePart};
 
// Create a set for strange parts with two strange parts.
let mut strange_parts = StrangePartSet::double(
    StrangePart::CriticalKills,
    StrangePart::DamageDealt,
);
 
// Check that strange parts contains Damage Dealt.
assert!(strange_parts.contains(&StrangePart::DamageDealt));
assert_eq!(strange_parts.len(), 2);
 
// Add a strange part.
strange_parts.insert(StrangePart::EngineersKilled);
 
assert_eq!(strange_parts.len(), 3);
 
// If a strange part is added when strange parts are full, the insert will fail.
assert!(!strange_parts.insert(StrangePart::MedicsKilled));
assert!(!strange_parts.contains(&StrangePart::MedicsKilled));
 
// Iterate over strange parts.
for strange_part in strange_parts {
    println!("{strange_part}");
}

Implementations§

Source§

impl StrangePartSet

Source

pub fn new() -> Self

Creates a set for strange parts.

§Examples
use tf2_enum::StrangePartSet;
 
let strange_parts = StrangePartSet::new();
Source

pub fn single(strange_part: StrangePart) -> Self

Creates a set for strange parts with one strange part.

§Examples
use tf2_enum::{StrangePartSet, StrangePart};
 
let strange_parts = StrangePartSet::single(
    StrangePart::DamageDealt,
);
 
assert_eq!(strange_parts.len(), 1);
Source

pub fn double(strange_part1: StrangePart, strange_part2: StrangePart) -> Self

Creates a set for strange parts with two strange parts.

If the same strange part is added multiple times, only one will be kept.

§Examples
use tf2_enum::{StrangePartSet, StrangePart};
 
let strange_parts = StrangePartSet::double(
    StrangePart::DamageDealt,
    StrangePart::CriticalKills,
);
 
assert_eq!(strange_parts.len(), 2);
Source

pub fn triple( strange_part1: StrangePart, strange_part2: StrangePart, strange_part3: StrangePart, ) -> Self

Creates a set for strange parts with two strange parts.

If the same strange part is added multiple times, only one will be kept.

§Examples
use tf2_enum::{StrangePartSet, StrangePart};
 
let strange_parts = StrangePartSet::triple(
    StrangePart::DamageDealt,
    StrangePart::CriticalKills,
    StrangePart::EngineersKilled,
);
 
assert_eq!(strange_parts.len(), 3);
Source

pub fn clear(&mut self)

Clears the set, removing all strange parts.

§Examples
use tf2_enum::{StrangePartSet, StrangePart};
 
let mut strange_parts = StrangePartSet::double(
    StrangePart::CriticalKills,
    StrangePart::DamageDealt,
);
 
strange_parts.clear();
 
assert_eq!(strange_parts.len(), 0);
Source

pub fn insert(&mut self, strange_part: StrangePart) -> bool

Adds a strange part to the first available slot. If no slots are available, the new strange part will be ignored.

Returns false if:

  • The strange part is already in the set.
  • The set is full.
§Examples
use tf2_enum::{StrangePartSet, StrangePart};
 
let mut strange_parts = StrangePartSet::double(
    StrangePart::CriticalKills,
    StrangePart::DamageDealt,
);
 
assert_eq!(strange_parts.len(), 2);
 
strange_parts.insert(StrangePart::EngineersKilled);
 
assert_eq!(strange_parts.len(), 3);
 
// Strange parts are full.
assert!(!strange_parts.insert(StrangePart::MedicsKilled));
Source

pub fn remove(&mut self, strange_part: &StrangePart) -> bool

Removes a strange part.

§Examples
use tf2_enum::{StrangePartSet, StrangePart};
 
let mut strange_parts = StrangePartSet::single(StrangePart::CriticalKills);
 
assert!(strange_parts.remove(&StrangePart::CriticalKills));
assert!(!strange_parts.contains(&StrangePart::CriticalKills));
Source

pub fn take(&mut self, strange_part: &StrangePart) -> Option<StrangePart>

Removes and returns the strange part 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 strange parts.

Source

pub fn contains(&self, strange_part: &StrangePart) -> bool

Returns true if the set contains a strange part.

§Examples
use tf2_enum::{StrangePartSet, StrangePart};
 
let strange_parts = StrangePartSet::from([
    Some(StrangePart::CriticalKills),
    Some(StrangePart::DamageDealt),
    None,
]);
 
assert!(strange_parts.contains(&StrangePart::CriticalKills));
Source

pub fn len(&self) -> usize

Returns the number of strange parts in the set.

§Examples
use tf2_enum::{StrangePartSet, StrangePart};
 
let strange_parts = StrangePartSet::double(StrangePart::DamageDealt, StrangePart::CriticalKills);
 
assert_eq!(strange_parts.len(), 2);
Source

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

Returns the strange parts that are in self but not in other.

§Examples
use tf2_enum::{StrangePartSet, StrangePart};
 
let strange_parts1 = StrangePartSet::double(StrangePart::DamageDealt, StrangePart::CriticalKills);
let strange_parts2 = StrangePartSet::double(StrangePart::DamageDealt, StrangePart::EngineersKilled);
let difference = strange_parts1.difference(&strange_parts2);
 
assert_eq!(difference, StrangePartSet::single(StrangePart::CriticalKills));
 
let difference = strange_parts2.difference(&strange_parts1);
 
assert_eq!(difference, StrangePartSet::single(StrangePart::EngineersKilled));
Source

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

Returns the strange parts that are both in self and other.

§Examples
use tf2_enum::{StrangePartSet, StrangePart};
 
let strange_parts1 = StrangePartSet::double(StrangePart::DamageDealt, StrangePart::CriticalKills);
let strange_parts2 = StrangePartSet::double(StrangePart::DamageDealt, StrangePart::EngineersKilled);
let intersection = strange_parts1.intersection(&strange_parts2);
 
assert_eq!(intersection, StrangePartSet::single(StrangePart::DamageDealt));
Source

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

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

§Examples
use tf2_enum::{StrangePartSet, StrangePart};
 
let strange_parts1 = StrangePartSet::double(StrangePart::DamageDealt, StrangePart::CriticalKills);
let strange_parts2 = StrangePartSet::double(StrangePart::DamageDealt, StrangePart::EngineersKilled);
 
assert!(!strange_parts1.is_disjoint(&strange_parts2));
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::{StrangePartSet, StrangePart};

let sup = StrangePartSet::double(StrangePart::DamageDealt, StrangePart::CriticalKills);
let mut strange_parts = StrangePartSet::single(StrangePart::DamageDealt);

assert!(strange_parts.is_subset(&sup));
 
strange_parts.insert(StrangePart::EngineersKilled);
 
assert!(!strange_parts.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::{StrangePartSet, StrangePart};

let sub = StrangePartSet::double(StrangePart::DamageDealt, StrangePart::CriticalKills);
let mut strange_parts = StrangePartSet::new();

assert!(!strange_parts.is_superset(&sub));
 
strange_parts.insert(StrangePart::DamageDealt);
 
assert!(!strange_parts.is_superset(&sub));
 
strange_parts.insert(StrangePart::CriticalKills);
 
assert!(strange_parts.is_superset(&sub));
Source

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

Returns an iterator over the strange parts in the set.

Trait Implementations§

Source§

impl BitAnd for &StrangePartSet

Source§

type Output = StrangePartSet

The resulting type after applying the & operator.
Source§

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

Performs the & operation. Read more
Source§

impl BitAnd for StrangePartSet

Source§

type Output = StrangePartSet

The resulting type after applying the & operator.
Source§

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

Performs the & operation. Read more
Source§

impl Clone for StrangePartSet

Source§

fn clone(&self) -> StrangePartSet

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 StrangePartSet

Source§

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

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

impl Default for StrangePartSet

Source§

fn default() -> StrangePartSet

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

impl Display for StrangePartSet

Source§

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

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

impl From<[Option<StrangePart>; 3]> for StrangePartSet

Source§

fn from(inner: [Option<StrangePart>; 3]) -> Self

Converts to this type from the input type.
Source§

impl FromIterator<StrangePart> for StrangePartSet

Source§

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

Creates a value from an iterator. Read more
Source§

impl Hash for StrangePartSet

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 &StrangePartSet

Source§

type Item = StrangePart

The type of the elements being iterated over.
Source§

type IntoIter = StrangePartSetIterator

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 StrangePartSet

Source§

type Item = StrangePart

The type of the elements being iterated over.
Source§

type IntoIter = StrangePartSetIterator

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 StrangePartSet

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 &StrangePartSet

Source§

type Output = StrangePartSet

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Sub for StrangePartSet

Source§

type Output = StrangePartSet

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Copy for StrangePartSet

Source§

impl Eq for StrangePartSet

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.