From 20e809668b3624ed87cf1255f2fae003622edcc0 Mon Sep 17 00:00:00 2001 From: Gavin Wood Date: Mon, 13 May 2019 20:58:57 +0200 Subject: [PATCH] Introduce the `Get` trait and type_parameter. (#2565) --- substrate/srml/support/src/lib.rs | 6 ++--- substrate/srml/support/src/traits.rs | 39 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/substrate/srml/support/src/lib.rs b/substrate/srml/support/src/lib.rs index 9d1bbff73a..cdaec09632 100644 --- a/substrate/srml/support/src/lib.rs +++ b/substrate/srml/support/src/lib.rs @@ -61,7 +61,7 @@ pub use self::storage::{StorageList, StorageValue, StorageMap, EnumerableStorage pub use self::hashable::Hashable; pub use self::dispatch::{Parameter, Dispatchable, Callable, IsSubType}; pub use self::double_map::StorageDoubleMapWithHasher; -pub use runtime_io::print; +pub use runtime_io::{print, storage_root}; #[doc(inline)] pub use srml_support_procedural::decl_storage; @@ -96,9 +96,9 @@ macro_rules! ensure { #[cfg(feature = "std")] macro_rules! assert_noop { ( $x:expr , $y:expr ) => { - let h = runtime_io::storage_root(); + let h = $crate::storage_root(); $crate::assert_err!($x, $y); - assert_eq!(h, runtime_io::storage_root()); + assert_eq!(h, $crate::storage_root()); } } diff --git a/substrate/srml/support/src/traits.rs b/substrate/srml/support/src/traits.rs index 67b1f973ce..a8db5a946e 100644 --- a/substrate/srml/support/src/traits.rs +++ b/substrate/srml/support/src/traits.rs @@ -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 { + /// 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; +/// } +/// 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 { /// The account was the given id was killed.