diff --git a/srml/balances/src/lib.rs b/srml/balances/src/lib.rs index c14cd6235787d..ee4fb6ceaa97f 100644 --- a/srml/balances/src/lib.rs +++ b/srml/balances/src/lib.rs @@ -136,8 +136,11 @@ decl_module! { } decl_event!( - pub enum Event with RawEvent - where ::AccountId, ::AccountIndex, ::Balance { + pub enum Event where + ::AccountId, + ::AccountIndex, + ::Balance + { /// A new account was created. NewAccount(AccountId, AccountIndex, NewAccountOutcome), /// An account was reaped. diff --git a/srml/council/src/motions.rs b/srml/council/src/motions.rs index babbec78347b9..4e34cd1518df2 100644 --- a/srml/council/src/motions.rs +++ b/srml/council/src/motions.rs @@ -49,9 +49,7 @@ pub enum Origin { /// Event for this module. decl_event!( - pub enum Event with RawEvent - where ::Hash, ::AccountId - { + pub enum Event where ::Hash, ::AccountId { /// A motion (given hash) has been proposed (by given account) with a threshold (given u32). Proposed(AccountId, ProposalIndex, Hash, u32), /// A motion (given hash) has been voted on by given account, leaving diff --git a/srml/council/src/seats.rs b/srml/council/src/seats.rs index 8941dbeccb919..145d440a8a455 100644 --- a/srml/council/src/seats.rs +++ b/srml/council/src/seats.rs @@ -156,9 +156,7 @@ decl_storage! { decl_event!( /// An event in this module. - pub enum Event with RawEvent - where ::AccountId - { + pub enum Event where ::AccountId { /// reaped voter, reaper VoterReaped(AccountId, AccountId), /// slashed reaper diff --git a/srml/council/src/voting.rs b/srml/council/src/voting.rs index 886adfc7bf664..c53b4005860ee 100644 --- a/srml/council/src/voting.rs +++ b/srml/council/src/voting.rs @@ -55,9 +55,7 @@ decl_storage! { /// An event in this module. decl_event!( - pub enum Event with RawEvent - where ::Hash - { + pub enum Event where ::Hash { /// A voting tally has happened for a referendum cancelation vote. /// Last three are yes, no, abstain counts. TallyCancelation(Hash, u32, u32, u32), diff --git a/srml/democracy/src/lib.rs b/srml/democracy/src/lib.rs index f74f37094d8ae..e8f67e05a4d99 100644 --- a/srml/democracy/src/lib.rs +++ b/srml/democracy/src/lib.rs @@ -110,9 +110,7 @@ decl_storage! { decl_event!( /// An event in this module. - pub enum Event with RawEvent - where ::Balance, ::AccountId - { + pub enum Event where ::Balance, ::AccountId { Tabled(PropIndex, Balance, Vec), Started(ReferendumIndex, VoteThreshold), Passed(ReferendumIndex), diff --git a/srml/example/src/lib.rs b/srml/example/src/lib.rs index 77e77ef1ad4df..2918a74270422 100644 --- a/srml/example/src/lib.rs +++ b/srml/example/src/lib.rs @@ -121,9 +121,7 @@ decl_module! { /// circumstances that have happened that users, Dapps and/or chain explorers would find /// interesting and otherwise difficult to detect. decl_event!( - pub enum Event with RawEvent - where ::Balance - { + pub enum Event where B = ::Balance { // Just a normal `enum`, here's a dummy event to ensure it compiles. /// Dummy event, just here so there's a generic type that's used. Dummy(B), diff --git a/srml/session/src/lib.rs b/srml/session/src/lib.rs index 61cf47a095717..23cae40b5f40f 100644 --- a/srml/session/src/lib.rs +++ b/srml/session/src/lib.rs @@ -84,9 +84,7 @@ decl_module! { /// An event in this module. decl_event!( - pub enum Event with RawEvent - where ::BlockNumber - { + pub enum Event where ::BlockNumber { /// New session has happened. Note that the argument is the session index, not the block /// number as the type might suggest. NewSession(BlockNumber), diff --git a/srml/staking/src/lib.rs b/srml/staking/src/lib.rs index 1e0ec4b7b2078..5a7e151dffa23 100644 --- a/srml/staking/src/lib.rs +++ b/srml/staking/src/lib.rs @@ -120,9 +120,7 @@ decl_module! { /// An event in this module. decl_event!( - pub enum Event with RawEvent - where ::Balance, ::AccountId - { + pub enum Event where ::Balance, ::AccountId { /// All validators have been rewarded by the given balance. Reward(Balance), /// One validator (and their nominators) has been given a offline-warning (they're still diff --git a/srml/support/src/event.rs b/srml/support/src/event.rs index a9767f51bf74e..6f0e1760a037b 100644 --- a/srml/support/src/event.rs +++ b/srml/support/src/event.rs @@ -14,84 +14,220 @@ // You should have received a copy of the GNU General Public License // along with Substrate. If not, see . -/// Implement an `Event`/`RawEvent` for a module. +/// Implement the `Event` for a module. +/// +/// # Simple Event Example: +/// +/// ```rust +/// #[macro_use] +/// extern crate srml_support; +/// extern crate parity_codec as codec; +/// #[macro_use] +/// extern crate parity_codec_derive; +/// #[macro_use] +/// extern crate serde_derive; +/// +/// decl_event!( +/// pub enum Event { +/// Success, +/// Failure(String), +/// } +/// ); +///# fn main() {} +/// ``` +/// +/// # Generic Event Example: +/// +/// ```rust +/// #[macro_use] +/// extern crate srml_support; +/// extern crate parity_codec as codec; +/// #[macro_use] +/// extern crate parity_codec_derive; +/// #[macro_use] +/// extern crate serde_derive; +/// +/// trait Trait { +/// type Balance; +/// type Token; +/// } +/// +/// mod event1 { +/// // Event that specifies the generic parameter explicitly (`Balance`). +/// decl_event!( +/// pub enum Event where Balance = ::Balance { +/// Message(Balance), +/// } +/// ); +/// } +/// +/// mod event2 { +/// // Event that uses the generic parameter `Balance`. +/// // If no name for the generic parameter is speciefied explicitly, +/// // the name will be taken from the type name of the trait. +/// decl_event!( +/// pub enum Event where ::Balance { +/// Message(Balance), +/// } +/// ); +/// } +/// +/// mod event3 { +/// // And we even support declaring multiple generic parameters! +/// decl_event!( +/// pub enum Event where ::Balance, ::Token { +/// Message(Balance, Token), +/// } +/// ); +/// } +///# fn main() {} +/// ``` +/// +/// The syntax for generic events requires the `where`. #[macro_export] macro_rules! decl_event { ( $(#[$attr:meta])* - pub enum Event<$( $evt_generic_param:ident )*> with RawEvent<$( $generic_param:ident ),*> - where $( <$generic:ident as $trait:path>::$trait_type:ident),* { + pub enum Event<$evt_generic_param:ident> where + $( $( $generic_rename:ident = )* <$generic:ident as $trait:path>::$trait_type:ident ),* + { $( - $(#[doc = $doc_attr:tt])* - $event:ident( $( $param:path ),* ), + $events:tt + )* + } + ) => { + __decl_generic_event!( + $( #[ $attr ] )*; + $evt_generic_param; + $( $( $generic_rename = )* <$generic as $trait>::$trait_type ),*; + Events { $( $events )* }; + ); + }; + ( + $(#[$attr:meta])* + pub enum Event { + $( + $events:tt )* } ) => { - pub type Event<$( $evt_generic_param )*> = RawEvent<$( <$generic as $trait>::$trait_type ),*>; // 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))] $(#[$attr])* - pub enum RawEvent<$( $generic_param ),*> { + pub enum Event { $( - $( #[doc = $doc_attr] )* - $event($( $param ),*), + $events )* } - impl<$( $generic_param ),*> From> for () { - fn from(_: RawEvent<$( $generic_param ),*>) -> () { () } + impl From for () { + fn from(_: Event) -> () { () } } - impl<$( $generic_param ),*> RawEvent<$( $generic_param ),*> { + impl Event { #[allow(dead_code)] pub fn event_json_metadata() -> &'static str { - concat!( - "{", - __impl_event_json_metadata!(""; - $( - $event ( $( $param ),* ); - __function_doc_to_json!(""; $($doc_attr)*); - )* - ), - " }" - ) + concat!("{", __events_to_json!(""; $( $events )* ), " }") } } + } +} + +#[macro_export] +#[doc(hidden)] +macro_rules! __decl_generic_event { + ( + $(#[$attr:meta])*; + $event_generic_param:ident; + $generic_rename:ident = <$generic:ident as $trait:path>::$trait_type:ident + $(, $( $rest_gen_rename:ident = )* <$rest_gen:ident as $rest_trait:path>::$rest_trait_type:ident )*; + Events { $( $events:tt )* }; + ) => { + __decl_generic_event!( + $( #[ $attr ] )*; + $event_generic_param; + $( $( $rest_gen_rename = )* <$rest_gen as $rest_trait>::$rest_trait_type ),*; + Events { $( $events )* }; + $generic_rename; + <$generic as $trait>::$trait_type; + ); }; ( - $(#[$attr:meta])* - pub enum Event { - $( - $(#[doc = $doc_attr:tt])* - $event:ident, - )* - } + $(#[$attr:meta])*; + $event_generic_param:ident; + $generic_rename:ident = <$generic:ident as $trait:path>::$trait_type:ident + $(, $( $rest_gen_rename:ident = )* <$rest_gen:ident as $rest_trait:path>::$rest_trait_type:ident )*; + Events { $( $events:tt )* }; + $( $parsed_generic_params:ident ),*; + $( <$parsed_generic:ident as $parsed_trait:path>::$parsed_trait_type:ident ),*; + ) => { + __decl_generic_event!( + $( #[ $attr ] )*; + $event_generic_param; + $( $( $rest_gen_rename = )* <$rest_gen as $rest_trait>::$rest_trait_type ),*; + Events { $( $events )* }; + $( $parsed_generic_params ),*, $generic_rename; + $( <$parsed_generic as $parsed_trait>::$parsed_trait_type ),*, <$generic as $trait>::$trait_type; + ); + }; + ( + $(#[$attr:meta])*; + $event_generic_param:ident; + <$generic:ident as $trait:path>::$trait_type:ident + $(, $( $rest_gen_rename:ident = )* <$rest_gen:ident as $rest_trait:path>::$rest_trait_type:ident )*; + Events { $( $events:tt )* }; + ) => { + __decl_generic_event!( + $( #[ $attr ] )*; + $event_generic_param; + $( $( $rest_gen_rename = )* <$rest_gen as $rest_trait>::$rest_trait_type ),*; + Events { $( $events )* }; + $trait_type; + <$generic as $trait>::$trait_type; + ); + }; + ( + $(#[$attr:meta])*; + $event_generic_param:ident; + <$generic:ident as $trait:path>::$trait_type:ident + $(, $( $rest_gen_rename:ident = )* <$rest_gen:ident as $rest_trait:path>::$rest_trait_type:ident )*; + Events { $( $events:tt )* }; + $( $parsed_generic_params:ident ),*; + $( <$parsed_generic:ident as $parsed_trait:path>::$parsed_trait_type:ident ),*; ) => { + __decl_generic_event!( + $( #[ $attr ] )*; + $event_generic_param; + $( $( $rest_gen_rename = )* <$rest_gen as $rest_trait>::$rest_trait_type ),*; + Events { $( $events )* }; + $( $parsed_generic_params ),*, $trait_type; + $( <$parsed_generic as $parsed_trait>::$parsed_trait_type ),*, <$generic as $trait>::$trait_type; + ); + }; + ( + $(#[$attr:meta])*; + $event_generic_param:ident; + ; + Events { $( $events:tt )* }; + $( $generic_param:ident ),*; + $( <$generic:ident as $trait:path>::$trait_type:ident ),*; + ) => { + pub type Event<$event_generic_param> = RawEvent<$( <$generic as $trait>::$trait_type ),*>; // 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))] $(#[$attr])* - pub enum Event { + pub enum RawEvent<$( $generic_param ),*> { $( - $( #[doc = $doc_attr] )* - $event, + $events )* } - impl From for () { - fn from(_: Event) -> () { () } + impl<$( $generic_param ),*> From> for () { + fn from(_: RawEvent<$( $generic_param ),*>) -> () { () } } - impl Event { + impl<$( $generic_param ),*> RawEvent<$( $generic_param ),*> { #[allow(dead_code)] pub fn event_json_metadata() -> &'static str { - concat!( - "{", - __impl_event_json_metadata!(""; - $( - $event; - __function_doc_to_json!(""; $($doc_attr)*); - )* - ), - " }" - ) + concat!("{", __events_to_json!(""; $( $events )* ), " }") } } } @@ -99,29 +235,30 @@ macro_rules! decl_event { #[macro_export] #[doc(hidden)] -macro_rules! __impl_event_json_metadata { +macro_rules! __events_to_json { ( $prefix_str:expr; - $event:ident( $first_param:path $(, $param:path )* ); - $event_doc:expr; + $( #[doc = $doc_attr:tt] )* + $event:ident( $first_param:path $(, $param:path )* ), $( $rest:tt )* ) => { concat!($prefix_str, " ", "\"", stringify!($event), r#"": { "params": [ ""#, stringify!($first_param), "\"" $(, concat!(", \"", stringify!($param), "\"") )*, r#" ], "description": ["#, - $event_doc, " ] }", - __impl_event_json_metadata!(","; $( $rest )*) + __function_doc_to_json!(""; $( $doc_attr )*), " ] }", + __events_to_json!(","; $( $rest )*) ) }; ( $prefix_str:expr; - $event:ident; - $event_doc:expr; + $( #[doc = $doc_attr:tt] )* + $event:ident, $( $rest:tt )* ) => { concat!($prefix_str, " ", "\"", stringify!($event), - r#"": { "params": null, "description": ["#, $event_doc, " ] }", - __impl_event_json_metadata!(","; $( $rest )*) + r#"": { "params": null, "description": ["#, + __function_doc_to_json!(""; $( $doc_attr )*), " ] }", + __events_to_json!(","; $( $rest )*) ) }; ( @@ -223,11 +360,13 @@ mod tests { } decl_event!( - pub enum Event with RawEvent - where ::Balance + /// Event without renaming the generic parameter `Balance` and `Origin`. + pub enum Event where ::Balance, ::Origin { /// Hi, I am a comment. - TestEvent(Balance), + TestEvent(Balance, Origin), + /// Dog + EventWithoutParams, } ); } @@ -243,10 +382,13 @@ mod tests { } decl_event!( - pub enum Event with RawEvent - where ::Balance + /// Event with renamed generic parameter + pub enum Event where + BalanceRenamed = ::Balance, + OriginRenamed = ::Origin { - TestEvent(Balance), + TestEvent(BalanceRenamed), + TestOrigin(OriginRenamed), } ); } @@ -277,8 +419,22 @@ mod tests { const EXPECTED_METADATA: (&str, &[(&str, &str)]) = ( "TestEvent", &[ ("system", r#"{ "SystemEvent": { "params": null, "description": [ ] } }"#), - ("event_module", r#"{ "TestEvent": { "params": [ "Balance" ], "description": [ " Hi, I am a comment." ] } }"#), - ("event_module2", r#"{ "TestEvent": { "params": [ "Balance" ], "description": [ ] } }"#), + ("event_module", + concat!( + "{", + r#" "TestEvent": { "params": [ "Balance", "Origin" ], "description": [ " Hi, I am a comment." ] },"#, + r#" "EventWithoutParams": { "params": null, "description": [ " Dog" ] }"#, + " }" + ) + ), + ("event_module2", + concat!( + "{", + r#" "TestEvent": { "params": [ "BalanceRenamed" ], "description": [ ] },"#, + r#" "TestOrigin": { "params": [ "OriginRenamed" ], "description": [ ] }"#, + " }" + ) + ), ] ); diff --git a/srml/support/src/metadata.rs b/srml/support/src/metadata.rs index 5fc57636c3f7a..280583b989b9c 100644 --- a/srml/support/src/metadata.rs +++ b/srml/support/src/metadata.rs @@ -151,8 +151,7 @@ mod tests { } decl_event!( - pub enum Event with RawEvent - where ::Balance + pub enum Event where ::Balance { /// Hi, I am a comment. TestEvent(Balance), @@ -179,8 +178,7 @@ mod tests { } decl_event!( - pub enum Event with RawEvent - where ::Balance + pub enum Event where ::Balance { TestEvent(Balance), } diff --git a/srml/treasury/src/lib.rs b/srml/treasury/src/lib.rs index 867cd631bcace..4b1e55a7db283 100644 --- a/srml/treasury/src/lib.rs +++ b/srml/treasury/src/lib.rs @@ -136,9 +136,7 @@ decl_storage! { /// An event in this module. decl_event!( - pub enum Event with RawEvent - where ::Balance, ::AccountId - { + pub enum Event where ::Balance, ::AccountId { /// New proposal. Proposed(ProposalIndex), /// We have ended a spend period and will now allocate funds.