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§

source§

impl<T> Lazy<T>
where T: BorshSerialize,

source

pub fn new<S>(key: S, value: T) -> Self
where S: IntoStorageKey,

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.

source

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

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

source

pub fn flush(&mut self)

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.

source§

impl<T> Lazy<T>

source

pub fn get(&self) -> &T

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.

source

pub fn get_mut(&mut self) -> &mut T

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§

source§

impl<T> AsMut<T> for Lazy<T>

source§

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

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl<T> AsRef<T> for Lazy<T>

source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<T> BorshDeserialize for Lazy<T>
where T: BorshSerialize,

source§

fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self, Error>

source§

fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>

Deserializes this instance from a given slice of bytes. Updates the buffer to point at the remaining bytes.
source§

fn try_from_slice(v: &[u8]) -> Result<Self, Error>

Deserialize this instance from a slice of bytes.
source§

fn try_from_reader<R>(reader: &mut R) -> Result<Self, Error>
where R: Read,

source§

impl<T> BorshSerialize for Lazy<T>
where T: BorshSerialize,

source§

fn serialize<W: Write>(&self, writer: &mut W) -> Result<(), Error>

source§

impl<T> Debug for Lazy<T>

source§

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

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

impl<T> Deref for Lazy<T>

§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<T> DerefMut for Lazy<T>

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
source§

impl<T> Drop for Lazy<T>
where T: BorshSerialize,

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<T> Ord for Lazy<T>

source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<T> PartialEq for Lazy<T>

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T> PartialOrd for Lazy<T>

source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
source§

fn lt(&self, other: &Self) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
source§

fn le(&self, other: &Self) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
source§

fn ge(&self, other: &Self) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

fn gt(&self, other: &Self) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
source§

impl<T> Eq for Lazy<T>

Auto Trait Implementations§

§

impl<T> !Freeze for Lazy<T>

§

impl<T> RefUnwindSafe for Lazy<T>

§

impl<T> Send for Lazy<T>
where T: Send,

§

impl<T> !Sync for Lazy<T>

§

impl<T> Unpin for Lazy<T>
where T: Unpin,

§

impl<T> UnwindSafe for Lazy<T>
where T: UnwindSafe,

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