pub trait StorageValue<T>
where T: FullCodec,
{ type Query;
Show 17 methods // Required methods fn hashed_key() -> [u8; 32]; fn exists() -> bool; fn get() -> Self::Query; fn try_get() -> Result<T, ()>; fn translate<O, F>(f: F) -> Result<Option<T>, ()> where O: Decode, F: FnOnce(Option<O>) -> Option<T>; fn put<Arg>(val: Arg) where Arg: EncodeLike<T>; fn set(val: Self::Query); fn mutate<R, F>(f: F) -> R where F: FnOnce(&mut Self::Query) -> R; fn try_mutate<R, E, F>(f: F) -> Result<R, E> where F: FnOnce(&mut Self::Query) -> Result<R, E>; fn mutate_exists<R, F>(f: F) -> R where F: FnOnce(&mut Option<T>) -> R; fn try_mutate_exists<R, E, F>(f: F) -> Result<R, E> where F: FnOnce(&mut Option<T>) -> Result<R, E>; fn kill(); fn take() -> Self::Query; fn append<Item, EncodeLikeItem>(item: EncodeLikeItem) where Item: Encode, EncodeLikeItem: EncodeLike<Item>, T: StorageAppend<Item>; // Provided methods fn mutate_extant<R, F>(f: F) -> R where R: Default, F: FnOnce(&mut T) -> R { ... } fn decode_len() -> Option<usize> where T: StorageDecodeLength { ... } fn decode_non_dedup_len() -> Option<usize> where T: StorageDecodeNonDedupLength { ... }
}
Expand description

A trait for working with macro-generated storage values under the substrate storage API.

Details on implementation can be found at [generator::StorageValue].

Required Associated Types§

type Query

The type that get/take return.

Required Methods§

fn hashed_key() -> [u8; 32]

Get the storage key.

fn exists() -> bool

Does the value (explicitly) exist in storage?

fn get() -> Self::Query

Load the value from the provided storage instance.

fn try_get() -> Result<T, ()>

Try to get the underlying value from the provided storage instance.

Returns Ok if it exists, Err if not.

fn translate<O, F>(f: F) -> Result<Option<T>, ()>
where O: Decode, F: FnOnce(Option<O>) -> Option<T>,

Translate a value from some previous type (O) to the current type.

f: F is the translation function.

Returns Err if the storage item could not be interpreted as the old type, and Ok, along with the new value if it could.

NOTE: This operates from and to Option<_> types; no effort is made to respect the default value of the original type.

§Warning

This function must be used with care, before being updated the storage still contains the old type, thus other calls (such as get) will fail at decoding it.

§Usage

This would typically be called inside the module implementation of on_runtime_upgrade, while ensuring no usage of this storage are made before the call to on_runtime_upgrade. (More precisely prior initialized modules doesn’t make use of this storage).

fn put<Arg>(val: Arg)
where Arg: EncodeLike<T>,

Store a value under this key into the provided storage instance.

fn set(val: Self::Query)

Store a value under this key into the provided storage instance; this uses the query type rather than the underlying value.

fn mutate<R, F>(f: F) -> R
where F: FnOnce(&mut Self::Query) -> R,

Mutate the value

fn try_mutate<R, E, F>(f: F) -> Result<R, E>
where F: FnOnce(&mut Self::Query) -> Result<R, E>,

Mutate the value if closure returns Ok

fn mutate_exists<R, F>(f: F) -> R
where F: FnOnce(&mut Option<T>) -> R,

Mutate the value. Deletes the item if mutated to a None.

fn try_mutate_exists<R, E, F>(f: F) -> Result<R, E>
where F: FnOnce(&mut Option<T>) -> Result<R, E>,

Mutate the value if closure returns Ok. Deletes the item if mutated to a None.

fn kill()

Clear the storage value.

fn take() -> Self::Query

Take a value from storage, removing it afterwards.

fn append<Item, EncodeLikeItem>(item: EncodeLikeItem)
where Item: Encode, EncodeLikeItem: EncodeLike<Item>, T: StorageAppend<Item>,

Append the given item to the value in the storage.

T is required to implement [StorageAppend].

§Warning

If the storage item is not encoded properly, the storage item will be overwritten and set to [item]. Any default value set for the storage item will be ignored on overwrite.

Provided Methods§

fn mutate_extant<R, F>(f: F) -> R
where R: Default, F: FnOnce(&mut T) -> R,

Mutate the value under a key if the value already exists. Do nothing and return the default value if not.

fn decode_len() -> Option<usize>
where T: StorageDecodeLength,

Read the length of the storage value without decoding the entire value.

T is required to implement [StorageDecodeLength].

If the value does not exists or it fails to decode the length, None is returned. Otherwise Some(len) is returned.

§Warning

None does not mean that get() does not return a value. The default value is completly ignored by this function.

fn decode_non_dedup_len() -> Option<usize>
where T: StorageDecodeNonDedupLength,

Read the length of the storage value without decoding the entire value.

T is required to implement [StorageDecodeNonDedupLength].

If the value does not exists or it fails to decode the length, None is returned. Otherwise Some(len) is returned.

§Warning
  • The value returned is the non-deduplicated length of the underlying Vector in storage.This means that any duplicate items are included.

  • None does not mean that get() does not return a value. The default value is completely ignored by this function.

§Example
#[test]
fn btree_set_decode_non_dedup_len() {
	#[crate::storage_alias]
	type Store = StorageValue<Prefix, BTreeSet<u32>>;

	TestExternalities::default().execute_with(|| {
		Store::append(4);
		Store::append(4); // duplicate value
		Store::append(5);

		let length_with_dup_items = 3;

		assert_eq!(Store::decode_non_dedup_len().unwrap(), length_with_dup_items);
	});
}

This demonstrates how decode_non_dedup_len will count even the duplicate values in the storage (in this case, the number 4 is counted twice).

Object Safety§

This trait is not object safe.

Implementors§

§

impl<T, G> StorageValue<T> for G
where T: FullCodec, G: StorageValue<T>,

§

type Query = <G as StorageValue<T>>::Query