diff --git a/substrate/srml/session/src/lib.rs b/substrate/srml/session/src/lib.rs index 05381e6df9..ea33395021 100644 --- a/substrate/srml/session/src/lib.rs +++ b/substrate/srml/session/src/lib.rs @@ -42,6 +42,7 @@ use primitives::traits::{As, Zero, One, Convert}; use codec::HasCompact; use runtime_support::{StorageValue, StorageMap}; use runtime_support::dispatch::Result; +use runtime_support::for_each_tuple; use system::ensure_signed; use rstd::ops::Mul; @@ -51,17 +52,6 @@ pub trait OnSessionChange { fn on_session_change(time_elapsed: T, should_reward: bool); } -macro_rules! for_each_tuple { - ($m:ident) => { - for_each_tuple! { @IMPL $m !! A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, } - }; - (@IMPL $m:ident !!) => { $m! { } }; - (@IMPL $m:ident !! $h:ident, $($t:ident,)*) => { - $m! { $h $($t)* } - for_each_tuple! { @IMPL $m !! $($t,)* } - } -} - macro_rules! impl_session_change { () => ( impl OnSessionChange for () { @@ -80,7 +70,6 @@ macro_rules! impl_session_change { for_each_tuple!(impl_session_change); - pub trait Trait: timestamp::Trait { type ConvertAccountIdToSessionKey: Convert; type OnSessionChange: OnSessionChange; diff --git a/substrate/srml/support/src/lib.rs b/substrate/srml/support/src/lib.rs index 76cb70a063..6ab77d9b58 100644 --- a/substrate/srml/support/src/lib.rs +++ b/substrate/srml/support/src/lib.rs @@ -134,3 +134,18 @@ pub use mashup::*; #[cfg(feature = "std")] #[doc(hidden)] pub use serde_derive::*; + +/// Programatically create derivations for tuples of up to 19 elements. You provide a second macro +/// which is called once per tuple size, along with a number of identifiers, one for each element +/// of the tuple. +#[macro_export] +macro_rules! for_each_tuple { + ($m:ident) => { + for_each_tuple! { @IMPL $m !! A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, } + }; + (@IMPL $m:ident !!) => { $m! { } }; + (@IMPL $m:ident !! $h:ident, $($t:ident,)*) => { + $m! { $h $($t)* } + for_each_tuple! { @IMPL $m !! $($t,)* } + } +} diff --git a/substrate/srml/timestamp/src/lib.rs b/substrate/srml/timestamp/src/lib.rs index 0d9360b747..27dcc81bab 100644 --- a/substrate/srml/timestamp/src/lib.rs +++ b/substrate/srml/timestamp/src/lib.rs @@ -55,25 +55,31 @@ use runtime_primitives::traits::{ }; use system::ensure_inherent; use rstd::{result, ops::{Mul, Div}, vec::Vec}; +use runtime_support::for_each_tuple; /// A trait which is called when the timestamp is set. pub trait OnTimestampSet { fn on_timestamp_set(moment: Moment); } -impl OnTimestampSet for () { - fn on_timestamp_set(_moment: Moment) { } -} +macro_rules! impl_timestamp_set { + () => ( + impl OnTimestampSet for () { + fn on_timestamp_set(_: Moment) {} + } + ); -impl OnTimestampSet for (A, B) - where A: OnTimestampSet, B: OnTimestampSet -{ - fn on_timestamp_set(moment: Moment) { - A::on_timestamp_set(moment.clone()); - B::on_timestamp_set(moment); + ( $($t:ident)* ) => { + impl),*> OnTimestampSet for ($($t,)*) { + fn on_timestamp_set(moment: Moment) { + $($t::on_timestamp_set(moment.clone());)* + } + } } } +for_each_tuple!(impl_timestamp_set); + pub trait Trait: consensus::Trait + system::Trait { /// The position of the required timestamp-set extrinsic. const TIMESTAMP_SET_POSITION: u32;