pub struct EnumMap<K, V> { /* private fields */ }Expand description
A map from enum-like keys to values, backed by a Vec of (K, V) pairs.
Designed for use with small, game-specific enums (e.g. stat IDs, type IDs). Lookups are O(n) linear scan — acceptable for N ≤ 15.
§Type Parameters
K— Key type, typically a copyable game-enum variant (Copy + PartialEq).V— Value type.
Implementations§
Source§impl<K: PartialEq, V> EnumMap<K, V>
impl<K: PartialEq, V> EnumMap<K, V>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create an empty map.
Examples found in repository?
examples/hello_dotzuki.rs (line 174)
169 fn create_monster(&self, species: Species, level: u8) -> BattlerState<Self> {
170 let (hp, atk, def, spd, moves) = match species {
171 Species::Warrior => (100u16, 15u16, 10u16, 8u16, vec![MoveKind::Slash]),
172 Species::Mage => (80u16, 20u16, 5u16, 12u16, vec![MoveKind::Fireball]),
173 };
174 let mut stats = EnumMap::new();
175 stats.set(StatId::HP, hp);
176 stats.set(StatId::ATK, atk);
177 stats.set(StatId::DEF, def);
178 stats.set(StatId::SPD, spd + level as u16);
179 BattlerState::new(species, hp, hp, stats, moves)
180 }Sourcepub fn set(&mut self, key: K, value: V)
pub fn set(&mut self, key: K, value: V)
Insert or update a key-value pair.
Examples found in repository?
examples/hello_dotzuki.rs (line 175)
169 fn create_monster(&self, species: Species, level: u8) -> BattlerState<Self> {
170 let (hp, atk, def, spd, moves) = match species {
171 Species::Warrior => (100u16, 15u16, 10u16, 8u16, vec![MoveKind::Slash]),
172 Species::Mage => (80u16, 20u16, 5u16, 12u16, vec![MoveKind::Fireball]),
173 };
174 let mut stats = EnumMap::new();
175 stats.set(StatId::HP, hp);
176 stats.set(StatId::ATK, atk);
177 stats.set(StatId::DEF, def);
178 stats.set(StatId::SPD, spd + level as u16);
179 BattlerState::new(species, hp, hp, stats, moves)
180 }Sourcepub fn get(&self, key: K) -> Option<&V>
pub fn get(&self, key: K) -> Option<&V>
Look up a value by key. Returns None if the key is not present.
Trait Implementations§
Auto Trait Implementations§
impl<K, V> Freeze for EnumMap<K, V>
impl<K, V> RefUnwindSafe for EnumMap<K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Send for EnumMap<K, V>
impl<K, V> Sync for EnumMap<K, V>
impl<K, V> Unpin for EnumMap<K, V>
impl<K, V> UnsafeUnpin for EnumMap<K, V>
impl<K, V> UnwindSafe for EnumMap<K, V>where
K: UnwindSafe,
V: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more