Type Alias sp_runtime::offchain::storage::StorageValue

source ·
pub type StorageValue = StorageValueRef<'static>;
Expand description

A storage value with a static key.

Aliased Type§

struct StorageValue { /* private fields */ }

Implementations

source§

impl<'a> StorageValueRef<'a>

source

pub fn persistent(key: &'a [u8]) -> Self

Create a new reference to a value in the persistent local storage.

source

pub fn local(key: &'a [u8]) -> Self

Create a new reference to a value in the fork-aware local storage.

source

pub fn set(&self, value: &impl Encode)

Set the value of the storage to encoding of given parameter.

Note that the storage may be accessed by workers running concurrently, if you happen to write a get-check-set pattern you should most likely be using mutate instead.

source

pub fn clear(&mut self)

Remove the associated value from the storage.

source

pub fn get<T: Decode>(&self) -> Result<Option<T>, StorageRetrievalError>

Retrieve & decode the value from storage.

Note that if you want to do some checks based on the value and write changes after that, you should rather be using mutate.

Returns the value if stored. Returns an error if the value could not be decoded.

source

pub fn mutate<T, E, F>( &self, mutate_val: F, ) -> Result<T, MutateStorageError<T, E>>
where T: Codec, F: FnOnce(Result<Option<T>, StorageRetrievalError>) -> Result<T, E>,

Retrieve & decode the current value and set it to a new value atomically.

Function mutate_val takes as input the current value and should return a new value that is attempted to be written to storage.

This function returns:

  1. Ok(T) in case the value has been successfully set.
  2. Err(MutateStorageError::ConcurrentModification(T)) in case the value was calculated by the passed closure mutate_val, but it could not be stored.
  3. Err(MutateStorageError::ValueFunctionFailed(_)) in case mutate_val returns an error.