[][src]Struct mantle::exe::Lazy

pub struct Lazy<T: Storage> { /* fields omitted */ }

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

impl<T: Debug + Storage> Debug for Lazy<T>[src]

Auto Trait Implementations

impl<T> Send for Lazy<T> where
    T: Send

impl<T> !Sync for Lazy<T>

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]