Trait frame_support::storage::types::QueryKindTrait
source · pub trait QueryKindTrait<Value, OnEmpty> {
type Query: FullCodec + 'static;
const METADATA: StorageEntryModifierIR;
// Required methods
fn from_optional_value_to_query(v: Option<Value>) -> Self::Query;
fn from_query_to_optional_value(v: Self::Query) -> Option<Value>;
}
Expand description
Trait implementing how the storage optional value is converted into the queried type.
It is implemented most notable by:
OptionQuery
which converts an optional value to an optional value, used when querying storage returns an optional value.ResultQuery
which converts an optional value to a result value, used when querying storage returns a result value.ValueQuery
which converts an optional value to a value, used when querying storage returns a value.
§Example
ⓘ
#[test]
pub fn value_query_examples() {
/// Custom default impl to be used with `ValueQuery`.
struct UniverseSecret;
impl Get<u32> for UniverseSecret {
fn get() -> u32 {
42
}
}
/// Custom default impl to be used with `ResultQuery`.
struct GetDefaultForResult;
impl Get<Result<u32, ()>> for GetDefaultForResult {
fn get() -> Result<u32, ()> {
Err(())
}
}
type A = StorageValue<Prefix, u32, ValueQuery>;
type B = StorageValue<Prefix, u32, OptionQuery>;
type C = StorageValue<Prefix, u32, ResultQuery<()>, GetDefaultForResult>;
type D = StorageValue<Prefix, u32, ValueQuery, UniverseSecret>;
TestExternalities::default().execute_with(|| {
// normal value query returns default
assert_eq!(A::get(), 0);
// option query returns none
assert_eq!(B::get(), None);
// result query returns error
assert_eq!(C::get(), Err(()));
// value query with custom on empty returns 42
assert_eq!(D::get(), 42);
});
}
Required Associated Types§
Required Associated Constants§
sourceconst METADATA: StorageEntryModifierIR
const METADATA: StorageEntryModifierIR
Metadata for the storage kind.
Required Methods§
sourcefn from_optional_value_to_query(v: Option<Value>) -> Self::Query
fn from_optional_value_to_query(v: Option<Value>) -> Self::Query
Convert an optional value (i.e. some if trie contains the value or none otherwise) to the query.
sourcefn from_query_to_optional_value(v: Self::Query) -> Option<Value>
fn from_query_to_optional_value(v: Self::Query) -> Option<Value>
Convert a query to an optional value.
Object Safety§
This trait is not object safe.