[−][src]Struct mantle::exe::Lazy
Container for service state that is lazily loaded from storage.
Currently can only be used as a top-level type (e.g., Lazy<Vec<T>>
, not Vec<Lazy<T>>
).
where the entire Vec will be lazily instantiated (as opposed to each individual element).
Example
mantle::service! { #[derive(Service)] pub struct SinglePlayerRPG { player_name: String, inventory: Vec<InventoryItem>, bank: Lazy<HashMap<InventoryItem, u64>>, } impl SinglePlayerRPG { pub fn new(_ctx: &Context, player_name: String) -> Self { Self { player_name, inventory: Vec::new(), bank: lazy!(HashMap::new()), } } pub fn get_inventory(&self, _ctx: &Context) -> Vec<InventoryItem> { self.inventory.clone() } pub fn get_bank(&self, _ctx: &Context) -> Vec<InventoryItem> { self.bank.get().clone() } pub fn move_item_to_inventory(&mut self, _ctx: &Context, item: InventoryItem) { self.bank.get_mut().entry(&item).and_modify(|count| { if count > 0 { self.inventory.push(item); *count -= 1 } }); } } }
Methods
impl<T: Storage> Lazy<T>
[src]
pub fn get(&self) -> &T
[src]
Returns a reference to the value loaded from Storage.
pub fn get_mut(&mut self) -> &mut T
[src]
Returns a mutable reference to the value loaded from Storage.
pub fn is_initialized(&self) -> bool
[src]
Trait Implementations
Auto Trait Implementations
Blanket Implementations
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,