Struct near_sdk::store::Lazy

source ·
pub struct Lazy<T>where
    T: BorshSerialize,
{ /* private fields */ }
Expand description

An persistent lazily loaded value, that stores a value in the storage.

This will only write to the underlying store if the value has changed, and will only read the existing value from storage once.

Examples

use near_sdk::store::Lazy;

let mut a = Lazy::new(b"a", "test string".to_string());
assert_eq!(*a, "test string");

*a = "new string".to_string();
assert_eq!(a.get(), "new string");

Implementations

Initializes new lazily loaded value with a given storage prefix and the value to initialize it with.

This prefix can be anything that implements IntoStorageKey. The prefix is used when storing and looking up values in storage to ensure no collisions with other collections.

Updates the value with a new value. This does not load the current value from storage.

Writes any changes to the value to storage. This will automatically be done when the value is dropped through Drop so this should only be used when the changes need to be reflected in the underlying storage before then.

Returns a reference to the lazily loaded storage value. The load from storage only happens once, and if the value is already cached, it will not be reloaded.

This function will panic if the cache is not loaded and the value at the key does not exist.

Returns a reference to the lazily loaded storage value. The load from storage only happens once, and if the value is already cached, it will not be reloaded.

This function will panic if the cache is not loaded and the value at the key does not exist.

Trait Implementations

Converts this type into a mutable reference of the (usually inferred) input type.
Converts this type into a shared reference of the (usually inferred) input type.
Deserializes this instance from a given slice of bytes. Updates the buffer to point at the remaining bytes. Read more
Deserialize this instance from a slice of bytes.
Serialize this instance into a vector of bytes.
Formats the value using the given formatter. Read more
The resulting type after dereferencing.
Dereferences the value.
Mutably dereferences the value.
Executes the destructor for this type. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.