Struct ink_storage::Lazy

source ·
pub struct Lazy<V, KeyType: StorageKey = AutoKey> { /* 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§

source§

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

source

pub const fn new() -> Self

Creates a new empty Lazy.

source§

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

source

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

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

source

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

Writes the given value to the contract storage.

source§

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

source

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§

source§

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

source§

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

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

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

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

source§

fn default() -> Self

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

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

source§

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

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

fn decode<I: Input>(_input: &mut I) -> Result<Self, Error>

Attempt to deserialize the value from input.
source§

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

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

source§

const KEY: Key = KeyType::KEY

Storage key of the type.
source§

fn key(&self) -> u32

Returns the storage key.
source§

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

source§

fn layout(_: &Key) -> Layout

Returns the static storage layout of Self. Read more
source§

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

§

type Identity = Lazy<V, KeyType>

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

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, const KEY: u32, ParentKey> AutoStorableHint<ManualKey<KEY, ParentKey>> for Twhere
    T: StorableHint<ParentKey> + StorableHint<ResolverKey<<T as StorableHint<ParentKey>>::PreferredKey, ManualKey<KEY, ParentKey>>>,
    ParentKey: StorageKey,

§

type Type = <T as StorableHint<ResolverKey<<T as StorableHint<ParentKey>>::PreferredKey, ManualKey<KEY, ParentKey>>>>::Type

Storable type with storage key inside.
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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<T> 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.
const: unstable · 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.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

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