From a3c1192e4e4e3240a8c91add4f053408b4f41188 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Wed, 12 Sep 2018 15:24:42 +0200 Subject: [PATCH] Make `impl_outer_event!` aware of required generic parameters --- node/runtime/src/lib.rs | 16 ++--- srml/council/src/lib.rs | 4 +- srml/executive/src/lib.rs | 2 +- srml/support/src/event.rs | 113 +++++++++++++++++++++++++++++++---- srml/support/src/metadata.rs | 3 +- 5 files changed, 116 insertions(+), 22 deletions(-) diff --git a/node/runtime/src/lib.rs b/node/runtime/src/lib.rs index 4eee9a24495cb..75578ef2b09ce 100644 --- a/node/runtime/src/lib.rs +++ b/node/runtime/src/lib.rs @@ -215,15 +215,15 @@ pub type Contract = contract::Module; impl_outer_event! { pub enum Event for Runtime { //consensus, - balances, + balances, //timetstamp, - session, - staking, - democracy, - council, - council_voting, - council_motions, - treasury + session, + staking, + democracy, + council, + council_voting, + council_motions, + treasury, } } diff --git a/srml/council/src/lib.rs b/srml/council/src/lib.rs index 1ca42835748eb..9fcfbe576eab9 100644 --- a/srml/council/src/lib.rs +++ b/srml/council/src/lib.rs @@ -144,7 +144,7 @@ mod tests { impl_outer_event! { pub enum Event for Test { - balances, democracy, seats, voting, motions + balances, democracy, seats, voting, motions, } } @@ -235,4 +235,4 @@ mod tests { pub type Council = seats::Module; pub type CouncilVoting = voting::Module; pub type CouncilMotions = motions::Module; -} \ No newline at end of file +} diff --git a/srml/executive/src/lib.rs b/srml/executive/src/lib.rs index a1086e0881534..f469e5971b264 100644 --- a/srml/executive/src/lib.rs +++ b/srml/executive/src/lib.rs @@ -242,7 +242,7 @@ mod tests { impl_outer_event!{ pub enum MetaEvent for Runtime { - balances + balances, } } diff --git a/srml/support/src/event.rs b/srml/support/src/event.rs index 6f0e1760a037b..ddce345c38e89 100644 --- a/srml/support/src/event.rs +++ b/srml/support/src/event.rs @@ -270,7 +270,79 @@ macro_rules! __events_to_json { #[macro_export] macro_rules! impl_outer_event { - ($(#[$attr:meta])* pub enum $name:ident for $runtime:ident { $( $module:ident ),* }) => { + ( + $(#[$attr:meta])* + pub enum $name:ident for $runtime:ident { + $module:ident, + $( $rest:tt $( <$t:ident> )*, )* + } + ) => { + impl_outer_event!( + $( #[$attr] )*; + $name; + $runtime; + Modules { $( $rest $(<$t>)*, )* }; + $module::Event<$runtime>,; + ); + }; + ( + $(#[$attr:meta])* + pub enum $name:ident for $runtime:ident { + $module:ident, + $( $rest:tt $( <$t:ident> )*, )* + } + ) => { + impl_outer_event!( + $( #[$attr] )*; + $name; + $runtime; + Modules { $( $rest $(<$t>)*, )* }; + $module::Event,; + ); + }; + ( + $(#[$attr:meta])*; + $name:ident; + $runtime:ident; + Modules { + $module:ident, + $( $rest:tt $( <$t:ident> )*, )* + }; + $( $module_name:ident::Event $( <$generic_param:ident> )*, )*; + ) => { + impl_outer_event!( + $( #[$attr] )*; + $name; + $runtime; + Modules { $( $rest $(<$t>)*, )* }; + $( $module_name::Event $( <$generic_param> )*, )* $module::Event<$runtime>,; + ); + }; + ( + $(#[$attr:meta])*; + $name:ident; + $runtime:ident; + Modules { + $module:ident, + $( $rest:tt, )* + }; + $( $module_name:ident::Event $( <$generic_param:ident> )*, )*; + ) => { + impl_outer_event!( + $( #[$attr] )*; + $name; + $runtime; + Modules { $( $rest, )* }; + $( $module_name::Event $( <$generic_param> )*, )* $module::Event,; + ); + }; + ( + $(#[$attr:meta])*; + $name:ident; + $runtime:ident; + Modules {}; + $( $module_name:ident::Event $( <$generic_param:ident> )*, )*; + ) => { // Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted. #[derive(Clone, PartialEq, Eq, Encode, Decode)] #[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))] @@ -279,7 +351,7 @@ macro_rules! impl_outer_event { pub enum $name { system(system::Event), $( - $module($module::Event<$runtime>), + $module_name( $module_name::Event $( <$generic_param> )* ), )* } impl From for $name { @@ -288,13 +360,17 @@ macro_rules! impl_outer_event { } } $( - impl From<$module::Event<$runtime>> for $name { - fn from(x: $module::Event<$runtime>) -> Self { - $name::$module(x) + impl From<$module_name::Event $( <$generic_param> )*> for $name { + fn from(x: $module_name::Event $( <$generic_param> )*) -> Self { + $name::$module_name(x) } } )* - __impl_outer_event_json_metadata!($runtime; $name; $( $module )*); + __impl_outer_event_json_metadata!( + $runtime; + $name; + $( $module_name::Event $( <$generic_param> )*, )*; + ); } } @@ -304,7 +380,7 @@ macro_rules! __impl_outer_event_json_metadata { ( $runtime:ident; $event_name:ident; - $( $module:ident )* + $( $module_name:ident::Event $( <$generic_param:ident> )*, )*; ) => { impl $runtime { #[allow(dead_code)] @@ -313,8 +389,8 @@ macro_rules! __impl_outer_event_json_metadata { ("system", system::Event::event_json_metadata) $( , ( - stringify!($module), - $module::Event::<$runtime>::event_json_metadata + stringify!($module_name), + $module_name::Event $( ::<$generic_param> )*::event_json_metadata ) )* ]; @@ -393,12 +469,22 @@ mod tests { ); } + mod event_module3 { + decl_event!( + pub enum Event { + HiEvent, + } + ); + } + #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, Deserialize, Serialize)] pub struct TestRuntime; impl_outer_event! { pub enum TestEvent for TestRuntime { - event_module, event_module2 + event_module, + event_module2, + event_module3, } } @@ -435,6 +521,13 @@ mod tests { " }" ) ), + ("event_module3", + concat!( + "{", + r#" "HiEvent": { "params": null, "description": [ ] }"#, + " }" + ) + ), ] ); diff --git a/srml/support/src/metadata.rs b/srml/support/src/metadata.rs index 280583b989b9c..868d4005947ed 100644 --- a/srml/support/src/metadata.rs +++ b/srml/support/src/metadata.rs @@ -200,7 +200,8 @@ mod tests { impl_outer_event! { pub enum TestEvent for TestRuntime { - event_module, event_module2 + event_module, + event_module2, } }