Struct ink::storage::Lazy

pub struct Lazy<V, KeyType = AutoKey>where
    KeyType: StorageKey,{ /* private fields */ }
Expand description

A simple wrapper around a type to store it in a separate storage cell under its own storage key. If you want to update the value, first you need to get it, update the value, and then call set with the new value.

Important

The wrapper requires its own pre-defined storage key in order to determine where it stores value. By default, the is automatically calculated using AutoKey during compilation. However, anyone can specify a storage key using ManualKey. Specifying the storage key can be helpful for upgradeable contracts or you want to be resistant to future changes of storage key calculation strategy.

Note

If the contract has two or more Lazy with the same storage key, modifying the value of one of them will modify others.

This is an example of how you can do this:


use ink::storage::{
    traits::ManualKey,
    Lazy,
};

#[ink(storage)]
#[derive(Default)]
pub struct MyContract {
    owner: Lazy<AccountId>,
    balance: Lazy<Balance, ManualKey<123>>,
}

impl MyContract {
    #[ink(constructor)]
    pub fn new() -> Self {
        let mut instance = Self::default();
        let caller = Self::env().caller();
        instance.owner.set(&caller);
        instance.balance.set(&123456);
        instance
    }

}

Implementations§

§

impl<V, KeyType> Lazy<V, KeyType>where KeyType: StorageKey,

pub const fn new() -> Lazy<V, KeyType>

Creates a new empty Lazy.

§

impl<V, KeyType> Lazy<V, KeyType>where V: Storable, KeyType: StorageKey,

pub fn get(&self) -> Option<V>

Reads the value from the contract storage, if it exists.

Panics

Traps if the encoded value doesn’t fit into the static buffer.

pub fn try_get(&self) -> Option<Result<V, Error>>

Try to read the value from the contract storage.

To successfully retrieve the value, the encoded key and value must both fit into the static buffer together.

Returns:

  • Some(Ok(_)) if value was received from storage and could be decoded.
  • Some(Err(_)) if retrieving the value would exceed the static buffer size.
  • None if there was no value under this storage key.

pub fn set(&mut self, value: &V)

Writes the given value to the contract storage.

Panics

Traps if the encoded value doesn’t fit into the static buffer.

pub fn try_set(&mut self, value: &V) -> Result<(), Error>

Try to set the given value to the contract storage.

To successfully store the value, the encoded key and value must fit into the static buffer together.

§

impl<V, KeyType> Lazy<V, KeyType>where V: Storable + Default, KeyType: StorageKey,

pub fn get_or_default(&self) -> V

Reads the value from the contract storage.

Returns the default value for the storage type if no value exists.

Trait Implementations§

§

impl<V, KeyType> Debug for Lazy<V, KeyType>where KeyType: StorageKey,

§

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

Formats the value using the given formatter. Read more
§

impl<V, KeyType> Default for Lazy<V, KeyType>where KeyType: StorageKey,

We implement this manually because the derived implementation adds trait bounds.

§

fn default() -> Lazy<V, KeyType>

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

impl<V, KeyType> Storable for Lazy<V, KeyType>where KeyType: StorageKey,

§

fn encode<T>(&self, _dest: &mut T)where T: Output + ?Sized,

Convert self to a slice and append it to the destination.
§

fn decode<I>(_input: &mut I) -> Result<Lazy<V, KeyType>, Error>where I: Input,

Attempt to deserialize the value from input.
§

fn encoded_size(&self) -> usize

The exact number of bytes this type consumes in the encoded form.
§

impl<V, Key, InnerKey> StorableHint<Key> for Lazy<V, InnerKey>where Key: StorageKey, InnerKey: StorageKey, V: StorableHint<Key>,

§

type Type = Lazy<<V as StorableHint<Key>>::Type, Key>

Storable type with storage key inside.
§

type PreferredKey = InnerKey

The storage key that the type prefers. It can be overwritten by an auto-generated storage key.
§

impl<V, KeyType> StorageKey for Lazy<V, KeyType>where KeyType: StorageKey,

§

const KEY: u32 = KeyType::KEY

Storage key of the type.
§

fn key(&self) -> u32

Returns the storage key.
§

impl<V, KeyType> StorageLayout for Lazy<V, KeyType>where V: StorageLayout + TypeInfo + 'static, KeyType: StorageKey + TypeInfo + 'static,

§

fn layout(_: &u32) -> Layout

Returns the static storage layout of Self. Read more
§

impl<V, KeyType> TypeInfo for Lazy<V, KeyType>where KeyType: StorageKey + TypeInfo + 'static, PhantomData<fn() -> (V, KeyType)>: TypeInfo + 'static, V: TypeInfo + 'static,

§

type Identity = Lazy<V, KeyType>

The type identifying for which type info is provided. Read more
§

fn type_info() -> Type

Returns the static type identifier for Self.

Auto Trait Implementations§

§

impl<V, KeyType> RefUnwindSafe for Lazy<V, KeyType>

§

impl<V, KeyType> Send for Lazy<V, KeyType>

§

impl<V, KeyType> Sync for Lazy<V, KeyType>

§

impl<V, KeyType> Unpin for Lazy<V, KeyType>

§

impl<V, KeyType> UnwindSafe for Lazy<V, KeyType>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. 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 Twhere 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> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> ImpliesReturn<T> for T

§

impl<T> StaticTypeInfo for Twhere T: TypeInfo + 'static,

§

impl<T> TypeId for Twhere T: Debug + Default,