Macro anagolay_runtime::parameter_types
source · [−]macro_rules! parameter_types {
(
$( #[ $attr:meta ] )*
$vis:vis const $name:ident: $type:ty = $value:expr;
$( $rest:tt )*
) => { ... };
(
$( #[ $attr:meta ] )*
$vis:vis $name:ident: $type:ty = $value:expr;
$( $rest:tt )*
) => { ... };
(
$( #[ $attr:meta ] )*
$vis:vis storage $name:ident: $type:ty = $value:expr;
$( $rest:tt )*
) => { ... };
() => { ... };
(IMPL_CONST $name:ident, $type:ty, $value:expr) => { ... };
(IMPL $name:ident, $type:ty, $value:expr) => { ... };
(IMPL_STORAGE $name:ident, $type:ty, $value:expr) => { ... };
(
$(
$( #[ $attr:meta ] )*
$vis:vis static $name:ident: $type:ty = $value:expr;
)*
) => { ... };
}
Expand description
Create new implementations of the Get
trait.
The so-called parameter type can be created in four different ways:
-
Using
const
to create a parameter type that provides aconst
getter. It is required that thevalue
is const. -
Declare the parameter type without
const
to have more freedom when creating the value. -
Using
storage
to create a storage parameter type. This type is special as it tries to load the value from the storage under a fixed key. If the value could not be found in the storage, the given default value will be returned. It is required that the value implementsEncode
andDecode
. The key for looking up the value in the storage is built using the following formula:twox_128(":" ++ NAME ++ ":")
whereNAME
is the name that is passed as type name. -
Using
static
to create a static parameter type. Its value is being provided by a static variable with the equivalent name inUPPER_SNAKE_CASE
. An additionalset
function is provided in this case to alter the static variable. This is intended for testing ONLY and is ONLY available whenstd
is enabled.
Examples
// This function cannot be used in a const context.
fn non_const_expression() -> u64 { 99 }
const FIXED_VALUE: u64 = 10;
parameter_types! {
pub const Argument: u64 = 42 + FIXED_VALUE;
/// Visibility of the type is optional
OtherArgument: u64 = non_const_expression();
pub storage StorageArgument: u64 = 5;
pub static StaticArgument: u32 = 7;
}
trait Config {
type Parameter: Get<u64>;
type OtherParameter: Get<u64>;
type StorageParameter: Get<u64>;
type StaticParameter: Get<u32>;
}
struct Runtime;
impl Config for Runtime {
type Parameter = Argument;
type OtherParameter = OtherArgument;
type StorageParameter = StorageArgument;
type StaticParameter = StaticArgument;
}
// In testing, `StaticArgument` can be altered later: `StaticArgument::set(8)`.
Invalid example:
// This function cannot be used in a const context.
fn non_const_expression() -> u64 { 99 }
parameter_types! {
pub const Argument: u64 = non_const_expression();
}