ink_storage

Struct 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>,

Source§

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

Storable type with storage key inside.
Source§

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,

Source§

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

Source§

type Output = T

Should always be Self
Source§

impl<T> SaturatedConversion for T

Source§

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

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>,

Source§

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>,

Source§

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

impl<T, S> UniqueSaturatedInto<T> for S
where T: Bounded, S: TryInto<T>,

Source§

fn unique_saturated_into(self) -> T

Consume self to return an equivalent value of T.
Source§

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

Source§

fn vzip(self) -> V

Source§

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

Source§

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