Skip to main content

EnumMap

Struct EnumMap 

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

Source

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    }
Source

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    }
Source

pub fn get(&self, key: K) -> Option<&V>

Look up a value by key. Returns None if the key is not present.

Examples found in repository?
examples/hello_dotzuki.rs (line 136)
132    fn calculate_damage(
133        &self, move_: &MoveKind, attacker: &BattlerState<Self>,
134        defender: &BattlerState<Self>, _random: u8, _is_critical: bool,
135    ) -> DamageResult {
136        let atk = attacker.stats.get(StatId::ATK).copied().unwrap_or(10);
137        let def = defender.stats.get(StatId::DEF).copied().unwrap_or(5).max(1);
138        let eff = HelloConfig::effectiveness(&move_.element(), &[defender.species.element()]);
139        let raw = (move_.power() as f32 * eff * atk as f32 / def as f32) as u16;
140        DamageResult { damage: raw.max(1), effectiveness: eff, is_miss: false }
141    }
Source

pub fn get_mut(&mut self, key: K) -> Option<&mut V>

Look up a value by key. Returns None if the key is not present.

Source

pub fn len(&self) -> usize

Number of entries in the map.

Source

pub fn is_empty(&self) -> bool

Returns true if the map contains no entries.

Source

pub fn iter(&self) -> impl Iterator<Item = (&K, &V)>

Iterate over (key, value) pairs in insertion order. Used by the turn-event log to diff stat-stage maps before/after an action.

Trait Implementations§

Source§

impl<K: Clone + PartialEq, V: Clone> Clone for EnumMap<K, V>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Debug + PartialEq, V: Debug> Debug for EnumMap<K, V>

Source§

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

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

impl<K: PartialEq, V> Default for EnumMap<K, V>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<K, V> Freeze for EnumMap<K, V>

§

impl<K, V> RefUnwindSafe for EnumMap<K, V>

§

impl<K, V> Send for EnumMap<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for EnumMap<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for EnumMap<K, V>
where K: Unpin, V: Unpin,

§

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