pub struct EquipmentSlots<I: Copy + Eq + Hash + Debug, S: Copy + Eq + Hash + Debug> { /* private fields */ }Expand description
A collection of equipment slots that tracks which item (if any) is equipped in each slot.
§Type parameters
I— Item identifier type.S— Slot identifier type (typicallyEquipSlot<Id>or a game-specific enum).
Implementations§
Source§impl<I: Copy + Eq + Hash + Debug, S: Copy + Eq + Hash + Debug> EquipmentSlots<I, S>
impl<I: Copy + Eq + Hash + Debug, S: Copy + Eq + Hash + Debug> EquipmentSlots<I, S>
Sourcepub fn new(slots: &[S]) -> Selfwhere
S: Clone,
pub fn new(slots: &[S]) -> Selfwhere
S: Clone,
Create an empty equipment set from the given slots.
All slots start with no item equipped.
Sourcepub fn from_pairs(pairs: Vec<(S, I)>) -> Self
pub fn from_pairs(pairs: Vec<(S, I)>) -> Self
Create an equipment set from a list of slot-item pairs.
If a slot appears multiple times the last associated item wins. Any
slot not present in pairs will not exist in the resulting set.
Source§impl<I: Copy + Eq + Hash + Debug, S: Copy + Eq + Hash + Debug> EquipmentSlots<I, S>
impl<I: Copy + Eq + Hash + Debug, S: Copy + Eq + Hash + Debug> EquipmentSlots<I, S>
Sourcepub fn equipped_in(&self, slot: &S) -> Option<&I>
pub fn equipped_in(&self, slot: &S) -> Option<&I>
Returns a reference to the item equipped in slot, or None if the
slot is empty or does not exist.
Sourcepub fn all_equipped(&self) -> Vec<(S, I)>
pub fn all_equipped(&self) -> Vec<(S, I)>
Returns all occupied slot-item pairs.
Sourcepub fn is_equipped(&self, item: &I) -> bool
pub fn is_equipped(&self, item: &I) -> bool
Returns true if item is equipped in any slot.
Sourcepub fn slot_count(&self) -> usize
pub fn slot_count(&self) -> usize
Returns the total number of slots (both occupied and empty).
Source§impl<I: Copy + Eq + Hash + Debug, S: Copy + Eq + Hash + Debug> EquipmentSlots<I, S>
impl<I: Copy + Eq + Hash + Debug, S: Copy + Eq + Hash + Debug> EquipmentSlots<I, S>
Sourcepub fn equip(&mut self, slot: S, item: I) -> Result<(), EquipError>
pub fn equip(&mut self, slot: S, item: I) -> Result<(), EquipError>
Equip item into slot.
§Errors
EquipError::SlotFull— the slot already has an item.EquipError::InvalidSlot—slotdoes not exist in this set.
Sourcepub fn unequip(&mut self, slot: &S) -> Option<I>
pub fn unequip(&mut self, slot: &S) -> Option<I>
Remove and return the item equipped in slot, or None if the slot is
empty or does not exist.
Sourcepub fn swap(&mut self, a: &S, b: &S) -> Result<(), EquipError>
pub fn swap(&mut self, a: &S, b: &S) -> Result<(), EquipError>
Swap the items in two slots (including empty ↔ occupied).
§Errors
EquipError::InvalidSlot— eitheraorbdoes not exist.
Trait Implementations§
Source§impl<I: Clone + Copy + Eq + Hash + Debug, S: Clone + Copy + Eq + Hash + Debug> Clone for EquipmentSlots<I, S>
impl<I: Clone + Copy + Eq + Hash + Debug, S: Clone + Copy + Eq + Hash + Debug> Clone for EquipmentSlots<I, S>
Source§fn clone(&self) -> EquipmentSlots<I, S>
fn clone(&self) -> EquipmentSlots<I, S>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more