Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 5c056ce

Browse files
committed
Make decl_event! more flexible with the automatic naming of generic parameters
The macro will now automatically derive the name of a generic parameter from the trait type name, if no explicit name is given. `where Balance = <T as Trait>::Balance` can be simplified to `where <T as Trait>::Balance`.
1 parent 510287c commit 5c056ce

File tree

10 files changed

+205
-47
lines changed

10 files changed

+205
-47
lines changed

substrate/runtime-support/src/event.rs

Lines changed: 193 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,60 +14,217 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
1616

17-
/// Implement an `Event`/`RawEvent` for a module.
17+
/// Implement the `Event` for a module.
18+
///
19+
/// # Simple Event Example:
20+
///
21+
/// ```rust
22+
/// #[macro_use]
23+
/// extern crate substrate_runtime_support;
24+
/// extern crate substrate_codec as codec;
25+
/// #[macro_use]
26+
/// extern crate substrate_codec_derive;
27+
/// #[macro_use]
28+
/// extern crate serde_derive;
29+
///
30+
/// decl_event!(
31+
/// pub enum Event {
32+
/// Success,
33+
/// Failure(String),
34+
/// }
35+
/// );
36+
///# fn main() {}
37+
/// ```
38+
///
39+
/// # Generic Event Example:
40+
///
41+
/// ```rust
42+
/// #[macro_use]
43+
/// extern crate substrate_runtime_support;
44+
/// extern crate substrate_codec as codec;
45+
/// #[macro_use]
46+
/// extern crate substrate_codec_derive;
47+
/// #[macro_use]
48+
/// extern crate serde_derive;
49+
///
50+
/// trait Trait {
51+
/// type Balance;
52+
/// type Token;
53+
/// }
54+
///
55+
/// mod event1 {
56+
/// // Event that specifies the generic parameter explicitly (`Balance`).
57+
/// decl_event!(
58+
/// pub enum Event<T> where Balance = <T as super::Trait>::Balance {
59+
/// Message(Balance),
60+
/// }
61+
/// );
62+
/// }
63+
///
64+
/// mod event2 {
65+
/// // Event that uses the generic parameter `Balance`.
66+
/// // If no name for the generic parameter is speciefied explicitly,
67+
/// // the name will be taken from the type name of the trait.
68+
/// decl_event!(
69+
/// pub enum Event<T> where <T as super::Trait>::Balance {
70+
/// Message(Balance),
71+
/// }
72+
/// );
73+
/// }
74+
///
75+
/// mod event3 {
76+
/// // And we even support declaring multiple generic parameters!
77+
/// decl_event!(
78+
/// pub enum Event<T> where <T as super::Trait>::Balance, <T as super::Trait>::Token {
79+
/// Message(Balance, Token),
80+
/// }
81+
/// );
82+
/// }
83+
///# fn main() {}
84+
/// ```
85+
///
86+
/// The syntax for generic events requires the `where`.
1887
#[macro_export]
1988
macro_rules! decl_event {
2089
(
2190
$(#[$attr:meta])*
22-
pub enum Event<$evt_generic_param:ident>
23-
where $($generic_param:ident = <$generic:ident as $trait:path>::$trait_type:ident),*
91+
pub enum Event<$evt_generic_param:ident> where
92+
$( $( $generic_rename:ident = )* <$generic:ident as $trait:path>::$trait_type:ident ),*
2493
{
2594
$(
2695
$events:tt
2796
)*
2897
}
2998
) => {
30-
pub type Event<$evt_generic_param> = RawEvent<$( <$generic as $trait>::$trait_type ),*>;
99+
__decl_generic_event!(
100+
$( #[ $attr ] )*;
101+
$evt_generic_param;
102+
$( $( $generic_rename = )* <$generic as $trait>::$trait_type ),*;
103+
Events { $( $events )* };
104+
);
105+
};
106+
(
107+
$(#[$attr:meta])*
108+
pub enum Event {
109+
$(
110+
$events:tt
111+
)*
112+
}
113+
) => {
31114
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
32115
#[derive(Clone, PartialEq, Eq, Encode, Decode)]
33116
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
34117
$(#[$attr])*
35-
pub enum RawEvent<$( $generic_param ),*> {
118+
pub enum Event {
36119
$(
37120
$events
38121
)*
39122
}
40-
impl<$( $generic_param ),*> From<RawEvent<$( $generic_param ),*>> for () {
41-
fn from(_: RawEvent<$( $generic_param ),*>) -> () { () }
123+
impl From<Event> for () {
124+
fn from(_: Event) -> () { () }
42125
}
43-
impl<$( $generic_param ),*> RawEvent<$( $generic_param ),*> {
126+
impl Event {
44127
#[allow(dead_code)]
45128
pub fn event_json_metadata() -> &'static str {
46129
concat!("{", __events_to_json!(""; $( $events )* ), " }")
47130
}
48131
}
132+
}
133+
}
134+
135+
#[macro_export]
136+
#[doc(hidden)]
137+
macro_rules! __decl_generic_event {
138+
(
139+
$(#[$attr:meta])*;
140+
$event_generic_param:ident;
141+
$generic_rename:ident = <$generic:ident as $trait:path>::$trait_type:ident
142+
$(, $( $rest_gen_rename:ident = )* <$rest_gen:ident as $rest_trait:path>::$rest_trait_type:ident )*;
143+
Events { $( $events:tt )* };
144+
) => {
145+
__decl_generic_event!(
146+
$( #[ $attr ] )*;
147+
$event_generic_param;
148+
$( $( $rest_gen_rename = )* <$rest_gen as $rest_trait>::$rest_trait_type ),*;
149+
Events { $( $events )* };
150+
$generic_rename;
151+
<$generic as $trait>::$trait_type;
152+
);
49153
};
50154
(
51-
$(#[$attr:meta])*
52-
pub enum Event {
53-
$(
54-
$events:tt
55-
)*
56-
}
155+
$(#[$attr:meta])*;
156+
$event_generic_param:ident;
157+
$generic_rename:ident = <$generic:ident as $trait:path>::$trait_type:ident
158+
$(, $( $rest_gen_rename:ident = )* <$rest_gen:ident as $rest_trait:path>::$rest_trait_type:ident )*;
159+
Events { $( $events:tt )* };
160+
$( $parsed_generic_params:ident ),*;
161+
$( <$parsed_generic:ident as $parsed_trait:path>::$parsed_trait_type:ident ),*;
162+
) => {
163+
__decl_generic_event!(
164+
$( #[ $attr ] )*;
165+
$event_generic_param;
166+
$( $( $rest_gen_rename = )* <$rest_gen as $rest_trait>::$rest_trait_type ),*;
167+
Events { $( $events )* };
168+
$( $parsed_generic_params ),*, $generic_rename;
169+
$( <$parsed_generic as $parsed_trait>::$parsed_trait_type ),*, <$generic as $trait>::$trait_type;
170+
);
171+
};
172+
(
173+
$(#[$attr:meta])*;
174+
$event_generic_param:ident;
175+
<$generic:ident as $trait:path>::$trait_type:ident
176+
$(, $( $rest_gen_rename:ident = )* <$rest_gen:ident as $rest_trait:path>::$rest_trait_type:ident )*;
177+
Events { $( $events:tt )* };
178+
) => {
179+
__decl_generic_event!(
180+
$( #[ $attr ] )*;
181+
$event_generic_param;
182+
$( $( $rest_gen_rename = )* <$rest_gen as $rest_trait>::$rest_trait_type ),*;
183+
Events { $( $events )* };
184+
$trait_type;
185+
<$generic as $trait>::$trait_type;
186+
);
187+
};
188+
(
189+
$(#[$attr:meta])*;
190+
$event_generic_param:ident;
191+
<$generic:ident as $trait:path>::$trait_type:ident
192+
$(, $( $rest_gen_rename:ident = )* <$rest_gen:ident as $rest_trait:path>::$rest_trait_type:ident )*;
193+
Events { $( $events:tt )* };
194+
$( $parsed_generic_params:ident ),*;
195+
$( <$parsed_generic:ident as $parsed_trait:path>::$parsed_trait_type:ident ),*;
196+
) => {
197+
__decl_generic_event!(
198+
$( #[ $attr ] )*;
199+
$event_generic_param;
200+
$( $( $rest_gen_rename = )* <$rest_gen as $rest_trait>::$rest_trait_type ),*;
201+
Events { $( $events )* };
202+
$( $parsed_generic_params ),*, $trait_type;
203+
$( <$parsed_generic as $parsed_trait>::$parsed_trait_type ),*, <$generic as $trait>::$trait_type;
204+
);
205+
};
206+
(
207+
$(#[$attr:meta])*;
208+
$event_generic_param:ident;
209+
;
210+
Events { $( $events:tt )* };
211+
$( $generic_param:ident ),*;
212+
$( <$generic:ident as $trait:path>::$trait_type:ident ),*;
57213
) => {
214+
pub type Event<$event_generic_param> = RawEvent<$( <$generic as $trait>::$trait_type ),*>;
58215
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
59216
#[derive(Clone, PartialEq, Eq, Encode, Decode)]
60217
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
61218
$(#[$attr])*
62-
pub enum Event {
219+
pub enum RawEvent<$( $generic_param ),*> {
63220
$(
64221
$events
65222
)*
66223
}
67-
impl From<Event> for () {
68-
fn from(_: Event) -> () { () }
224+
impl<$( $generic_param ),*> From<RawEvent<$( $generic_param ),*>> for () {
225+
fn from(_: RawEvent<$( $generic_param ),*>) -> () { () }
69226
}
70-
impl Event {
227+
impl<$( $generic_param ),*> RawEvent<$( $generic_param ),*> {
71228
#[allow(dead_code)]
72229
pub fn event_json_metadata() -> &'static str {
73230
concat!("{", __events_to_json!(""; $( $events )* ), " }")
@@ -203,10 +360,11 @@ mod tests {
203360
}
204361

205362
decl_event!(
206-
pub enum Event<T> where Balance = <T as Trait>::Balance
363+
/// Event without renaming the generic parameter `Balance` and `Origin`.
364+
pub enum Event<T> where <T as Trait>::Balance, <T as Trait>::Origin
207365
{
208366
/// Hi, I am a comment.
209-
TestEvent(Balance),
367+
TestEvent(Balance, Origin),
210368
/// Dog
211369
EventWithoutParams,
212370
}
@@ -224,9 +382,13 @@ mod tests {
224382
}
225383

226384
decl_event!(
227-
pub enum Event<T> where Balance = <T as Trait>::Balance
385+
/// Event with renamed generic parameter
386+
pub enum Event<T> where
387+
BalanceRenamed = <T as Trait>::Balance,
388+
OriginRenamed = <T as Trait>::Origin
228389
{
229-
TestEvent(Balance),
390+
TestEvent(BalanceRenamed),
391+
TestOrigin(OriginRenamed),
230392
}
231393
);
232394
}
@@ -260,12 +422,19 @@ mod tests {
260422
("event_module",
261423
concat!(
262424
"{",
263-
r#" "TestEvent": { "params": [ "Balance" ], "description": [ " Hi, I am a comment." ] },"#,
425+
r#" "TestEvent": { "params": [ "Balance", "Origin" ], "description": [ " Hi, I am a comment." ] },"#,
264426
r#" "EventWithoutParams": { "params": null, "description": [ " Dog" ] }"#,
265427
" }"
266428
)
267429
),
268-
("event_module2", r#"{ "TestEvent": { "params": [ "Balance" ], "description": [ ] } }"#),
430+
("event_module2",
431+
concat!(
432+
"{",
433+
r#" "TestEvent": { "params": [ "BalanceRenamed" ], "description": [ ] },"#,
434+
r#" "TestOrigin": { "params": [ "OriginRenamed" ], "description": [ ] }"#,
435+
" }"
436+
)
437+
),
269438
]
270439
);
271440

substrate/runtime-support/src/metadata.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ mod tests {
312312
}
313313

314314
decl_event!(
315-
pub enum Event<T> where Balance = <T as Trait>::Balance
315+
pub enum Event<T> where <T as Trait>::Balance
316316
{
317317
/// Hi, I am a comment.
318318
TestEvent(Balance),
@@ -339,7 +339,7 @@ mod tests {
339339
}
340340

341341
decl_event!(
342-
pub enum Event<T> where Balance = <T as Trait>::Balance
342+
pub enum Event<T> where <T as Trait>::Balance
343343
{
344344
TestEvent(Balance),
345345
}

substrate/runtime/balances/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ decl_module! {
137137

138138
decl_event!(
139139
pub enum Event<T> where
140-
AccountId = <T as system::Trait>::AccountId,
141-
AccountIndex = <T as Trait>::AccountIndex,
142-
Balance = <T as Trait>::Balance
140+
<T as system::Trait>::AccountId,
141+
<T as Trait>::AccountIndex,
142+
<T as Trait>::Balance
143143
{
144144
/// A new account was created.
145145
NewAccount(AccountId, AccountIndex, NewAccountOutcome),

substrate/runtime/council/src/motions.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ pub enum Origin {
4949

5050
/// Event for this module.
5151
decl_event!(
52-
pub enum Event<T>
53-
where Hash = <T as system::Trait>::Hash, AccountId = <T as system::Trait>::AccountId
54-
{
52+
pub enum Event<T> where <T as system::Trait>::Hash, <T as system::Trait>::AccountId {
5553
/// A motion (given hash) has been proposed (by given account) with a threshold (given u32).
5654
Proposed(AccountId, ProposalIndex, Hash, u32),
5755
/// A motion (given hash) has been voted on by given account, leaving

substrate/runtime/council/src/seats.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ decl_storage! {
156156

157157
decl_event!(
158158
/// An event in this module.
159-
pub enum Event<T> where AccountId = <T as system::Trait>::AccountId {
159+
pub enum Event<T> where <T as system::Trait>::AccountId {
160160
/// reaped voter, reaper
161161
VoterReaped(AccountId, AccountId),
162162
/// slashed reaper

substrate/runtime/council/src/voting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ decl_storage! {
5555

5656
/// An event in this module.
5757
decl_event!(
58-
pub enum Event<T> where Hash = <T as system::Trait>::Hash {
58+
pub enum Event<T> where <T as system::Trait>::Hash {
5959
/// A voting tally has happened for a referendum cancelation vote.
6060
/// Last three are yes, no, abstain counts.
6161
TallyCancelation(Hash, u32, u32, u32),

substrate/runtime/democracy/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,7 @@ decl_storage! {
110110

111111
decl_event!(
112112
/// An event in this module.
113-
pub enum Event<T> where
114-
Balance = <T as balances::Trait>::Balance,
115-
AccountId = <T as system::Trait>::AccountId
116-
{
113+
pub enum Event<T> where <T as balances::Trait>::Balance, <T as system::Trait>::AccountId {
117114
Tabled(PropIndex, Balance, Vec<AccountId>),
118115
Started(ReferendumIndex, VoteThreshold),
119116
Passed(ReferendumIndex),

substrate/runtime/session/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ decl_module! {
8484

8585
/// An event in this module.
8686
decl_event!(
87-
pub enum Event<T> where BlockNumber = <T as system::Trait>::BlockNumber {
87+
pub enum Event<T> where <T as system::Trait>::BlockNumber {
8888
/// New session has happened. Note that the argument is the session index, not the block
8989
/// number as the type might suggest.
9090
NewSession(BlockNumber),

substrate/runtime/staking/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,7 @@ decl_module! {
120120

121121
/// An event in this module.
122122
decl_event!(
123-
pub enum Event<T> where
124-
Balance = <T as balances::Trait>::Balance,
125-
AccountId = <T as system::Trait>::AccountId
126-
{
123+
pub enum Event<T> where <T as balances::Trait>::Balance, <T as system::Trait>::AccountId {
127124
/// All validators have been rewarded by the given balance.
128125
Reward(Balance),
129126
/// One validator (and their nominators) has been given a offline-warning (they're still

substrate/runtime/treasury/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,7 @@ decl_storage! {
136136

137137
/// An event in this module.
138138
decl_event!(
139-
pub enum Event<T> where
140-
Balance = <T as balances::Trait>::Balance,
141-
AccountId = <T as system::Trait>::AccountId
142-
{
139+
pub enum Event<T> where <T as balances::Trait>::Balance, <T as system::Trait>::AccountId {
143140
/// New proposal.
144141
Proposed(ProposalIndex),
145142
/// We have ended a spend period and will now allocate funds.

0 commit comments

Comments
 (0)