use alloc::rc::Rc; #[repr(u8)] #[derive(Eq, PartialEq, Debug, Copy, Clone)] pub enum Statistic { HP = 0, Attack = 1, Defense = 2, SpecialAttack = 3, SpecialDefense = 4, Speed = 5, } pub trait ImmutableStatisticSetTrait { fn hp(&self) -> u16; fn attack(&self) -> u16; fn defense(&self) -> u16; fn special_attack(&self) -> u16; fn special_defense(&self) -> u16; fn speed(&self) -> u16; } pub type ImmutableStatisticSet = Rc; #[cfg(not(feature = "mock_data"))] mod implementation { use super::*; use crate::cached_value; use crate::handling::cached_value::CachedValue; use crate::handling::extern_ref::{ExternRef, ExternalReferenceType}; use crate::handling::Cacheable; pub struct ImmutableStatisticSetInner { reference: ExternRef, /// The health point stat value. hp: CachedValue, /// The physical attack stat value. attack: CachedValue, /// The physical defense stat value. defense: CachedValue, /// The special attack stat value. special_attack: CachedValue, /// The special defense stat value. special_defense: CachedValue, /// The speed stat value. speed: CachedValue, } #[derive(Clone)] pub struct ImmutableStatisticSetImpl { inner: Rc, } impl ImmutableStatisticSetImpl { pub(crate) fn new(reference: ExternRef) -> Self { Self::from_ref(reference, &|reference| Self { inner: Rc::new(ImmutableStatisticSetInner { reference, hp: cached_value!({ static_statistics_set_get_hp(reference) }), attack: cached_value!({ static_statistics_set_get_attack(reference) }), defense: cached_value!({ static_statistics_set_get_defense(reference) }), special_attack: cached_value!({ static_statistics_set_get_special_attack(reference) }), special_defense: cached_value!({ static_statistics_set_get_special_defense(reference) }), speed: cached_value!({ static_statistics_set_get_speed(reference) }), }), }) } } impl ImmutableStatisticSetTrait for ImmutableStatisticSetImpl { fn hp(&self) -> u16 { self.inner.hp.value() } fn attack(&self) -> u16 { self.inner.attack.value() } fn defense(&self) -> u16 { self.inner.defense.value() } fn special_attack(&self) -> u16 { self.inner.special_attack.value() } fn special_defense(&self) -> u16 { self.inner.special_defense.value() } fn speed(&self) -> u16 { self.inner.speed.value() } } impl ExternalReferenceType for ImmutableStatisticSetImpl { fn from_extern_value(reference: ExternRef) -> Self { Self::new(reference) } } crate::handling::cacheable::cacheable!(ImmutableStatisticSetImpl); extern "wasm" { fn static_statistics_set_get_hp(r: ExternRef) -> u16; fn static_statistics_set_get_attack(r: ExternRef) -> u16; fn static_statistics_set_get_defense(r: ExternRef) -> u16; fn static_statistics_set_get_special_attack(r: ExternRef) -> u16; fn static_statistics_set_get_special_defense( r: ExternRef, ) -> u16; fn static_statistics_set_get_speed(r: ExternRef) -> u16; } } #[cfg(not(feature = "mock_data"))] pub use implementation::*;