Introduce the Get trait and type_parameter. (#2565)

This commit is contained in:
Gavin Wood
2019-05-13 20:58:57 +02:00
committed by GitHub
parent 21773b3a07
commit 20e809668b
2 changed files with 42 additions and 3 deletions
+39
View File
@@ -22,6 +22,45 @@ use crate::runtime_primitives::traits::{
MaybeSerializeDebug, SimpleArithmetic
};
/// New trait for querying a single fixed value from a type.
pub trait Get<T> {
/// Return a constant value.
fn get() -> T;
}
/// Macro for easily creating a new implementation of the `Get` trait. Use similarly to
/// how you would declare a `const`:
///
/// ```no_compile
/// parameter_types! {
/// pub const Argument: u64 = 42;
/// }
/// trait Config {
/// type Parameter: Get<u64>;
/// }
/// struct Runtime;
/// impl Config for Runtime {
/// type Parameter = Argument;
/// }
/// ```
#[macro_export]
macro_rules! parameter_types {
(pub const $name:ident: $type:ty = $value:expr; $( $rest:tt )*) => (
pub struct $name;
parameter_types!{IMPL $name , $type , $value}
parameter_types!{ $( $rest )* }
);
(const $name:ident: $type:ty = $value:expr; $( $rest:tt )*) => (
struct $name;
parameter_types!{IMPL $name , $type , $value}
parameter_types!{ $( $rest )* }
);
() => ();
(IMPL $name:ident , $type:ty , $value:expr) => {
impl $crate::traits::Get<$type> for $name { fn get() -> $type { $value } }
}
}
/// The account with the given id was killed.
pub trait OnFreeBalanceZero<AccountId> {
/// The account was the given id was killed.