QueryKindTrait

Trait 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 Constants§

Source

const METADATA: StorageEntryModifierIR

Metadata for the storage kind.

Required Associated Types§

Source

type Query: FullCodec + 'static

Type returned on query

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.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so 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

Source§

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

Source§

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

Source§

type Query = Value