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.

§Panics

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

source

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

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

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.

source

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

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.

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§

fn encoded_size(&self) -> usize

The exact number of bytes this type consumes in the encoded form.
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> Freeze for Lazy<V, KeyType>

§

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 T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where 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 T
where 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
§

impl<T> SaturatedConversion for T

§

fn saturated_from<T>(t: T) -> Self
where Self: UniqueSaturatedFrom<T>,

Convert from a value of T into an equivalent instance of Self. Read more
§

fn saturated_into<T>(self) -> T
where Self: UniqueSaturatedInto<T>,

Consume self to return an equivalent value of T. Read more
source§

impl<T, U> TryFrom<U> for T
where 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 T
where 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<T, S> UniqueSaturatedInto<T> for S
where T: Bounded, S: TryInto<T>,

§

fn unique_saturated_into(self) -> T

Consume self to return an equivalent value of T.
§

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

§

fn vzip(self) -> V

§

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

§

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