Trait frame_support::traits::StoredMap
source · pub trait StoredMap<K, T: Default> {
fn get(k: &K) -> T;
fn try_mutate_exists<R, E: From<DispatchError>>(
k: &K,
f: impl FnOnce(&mut Option<T>) -> Result<R, E>
) -> Result<R, E>;
fn mutate<R>(k: &K, f: impl FnOnce(&mut T) -> R) -> Result<R, DispatchError> { ... }
fn mutate_exists<R>(
k: &K,
f: impl FnOnce(&mut Option<T>) -> R
) -> Result<R, DispatchError> { ... }
fn insert(k: &K, t: T) -> Result<(), DispatchError> { ... }
fn remove(k: &K) -> Result<(), DispatchError> { ... }
}
Expand description
An abstraction of a value stored within storage, but possibly as part of a larger composite item.
Required Methods§
sourcefn get(k: &K) -> T
fn get(k: &K) -> T
Get the item, or its default if it doesn’t yet exist; we make no distinction between the two.
sourcefn try_mutate_exists<R, E: From<DispatchError>>(
k: &K,
f: impl FnOnce(&mut Option<T>) -> Result<R, E>
) -> Result<R, E>
fn try_mutate_exists<R, E: From<DispatchError>>(
k: &K,
f: impl FnOnce(&mut Option<T>) -> Result<R, E>
) -> Result<R, E>
Maybe mutate the item only if an Ok
value is returned from f
. Do nothing if an Err
is
returned. It is removed or reset to default value if it has been mutated to None
.
f
will always be called with an option representing if the storage item exists (Some<V>
)
or if the storage item does not exist (None
), independent of the QueryType
.
Provided Methods§
sourcefn mutate_exists<R>(
k: &K,
f: impl FnOnce(&mut Option<T>) -> R
) -> Result<R, DispatchError>
fn mutate_exists<R>(
k: &K,
f: impl FnOnce(&mut Option<T>) -> R
) -> Result<R, DispatchError>
Mutate the item, removing or resetting to default value if it has been mutated to None
.
This is infallible as long as the value does not get destroyed.
Examples found in repository?
src/traits/stored_map.rs (lines 44-52)
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
fn mutate<R>(k: &K, f: impl FnOnce(&mut T) -> R) -> Result<R, DispatchError> {
Self::mutate_exists(k, |maybe_account| match maybe_account {
Some(ref mut account) => f(account),
x @ None => {
let mut account = Default::default();
let r = f(&mut account);
*x = Some(account);
r
},
})
}
/// Mutate the item, removing or resetting to default value if it has been mutated to `None`.
///
/// This is infallible as long as the value does not get destroyed.
fn mutate_exists<R>(k: &K, f: impl FnOnce(&mut Option<T>) -> R) -> Result<R, DispatchError> {
Self::try_mutate_exists(k, |x| -> Result<R, DispatchError> { Ok(f(x)) })
}
/// Set the item to something new.
fn insert(k: &K, t: T) -> Result<(), DispatchError> {
Self::mutate(k, |i| *i = t)
}
/// Remove the item or otherwise replace it with its default value; we don't care which.
fn remove(k: &K) -> Result<(), DispatchError> {
Self::mutate_exists(k, |x| *x = None)
}