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§

source

type Query: FullCodec + 'static

Type returned on query

Required Associated Constants§

source

const METADATA: StorageEntryModifierIR

Metadata for the storage kind.

Required Methods§

source

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.

source

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.

Implementors§

source§

impl<Value> QueryKindTrait<Value, GetDefault> for OptionQuery
where Value: FullCodec + 'static,

source§

const METADATA: StorageEntryModifierIR = StorageEntryModifierIR::Optional

§

type Query = Option<Value>

source§

impl<Value, Error, OnEmpty> QueryKindTrait<Value, OnEmpty> for ResultQuery<Error>
where Value: FullCodec + 'static, Error: FullCodec + 'static, OnEmpty: Get<Result<Value, Error>>,

source§

const METADATA: StorageEntryModifierIR = StorageEntryModifierIR::Optional

§

type Query = Result<Value, Error>

source§

impl<Value, OnEmpty> QueryKindTrait<Value, OnEmpty> for ValueQuery
where Value: FullCodec + 'static, OnEmpty: Get<Value>,

source§

const METADATA: StorageEntryModifierIR = StorageEntryModifierIR::Default

§

type Query = Value