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>
impl<'a> StorageValueRef<'a>
sourcepub fn persistent(key: &'a [u8]) -> Self
pub fn persistent(key: &'a [u8]) -> Self
Create a new reference to a value in the persistent local storage.
sourcepub fn local(key: &'a [u8]) -> Self
pub fn local(key: &'a [u8]) -> Self
Create a new reference to a value in the fork-aware local storage.
sourcepub fn set(&self, value: &impl Encode)
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.
sourcepub fn get<T: Decode>(&self) -> Result<Option<T>, StorageRetrievalError>
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.
sourcepub fn mutate<T, E, F>(
&self,
mutate_val: F,
) -> Result<T, MutateStorageError<T, E>>
pub fn mutate<T, E, F>( &self, mutate_val: F, ) -> Result<T, MutateStorageError<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:
Ok(T)
in case the value has been successfully set.Err(MutateStorageError::ConcurrentModification(T))
in case the value was calculated by the passed closuremutate_val
, but it could not be stored.Err(MutateStorageError::ValueFunctionFailed(_))
in casemutate_val
returns an error.