Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Improve and simplify decl_event!
- RawEvent is not required anymore to be written in the defintion
- Events with and without parameters are now supported everywhere
  • Loading branch information
bkchr committed Sep 12, 2018
commit b0bc40bb41786aed12fd8218b287da660a8d3ffd
7 changes: 5 additions & 2 deletions srml/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,11 @@ decl_module! {
}

decl_event!(
pub enum Event<T> with RawEvent<AccountId, AccountIndex, Balance>
where <T as system::Trait>::AccountId, <T as Trait>::AccountIndex, <T as Trait>::Balance {
pub enum Event<T> where
AccountId = <T as system::Trait>::AccountId,
AccountIndex = <T as Trait>::AccountIndex,
Balance = <T as Trait>::Balance
{
/// A new account was created.
NewAccount(AccountId, AccountIndex, NewAccountOutcome),
/// An account was reaped.
Expand Down
4 changes: 2 additions & 2 deletions srml/council/src/motions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ pub enum Origin {

/// Event for this module.
decl_event!(
pub enum Event<T> with RawEvent<Hash, AccountId>
where <T as system::Trait>::Hash, <T as system::Trait>::AccountId
pub enum Event<T>
where Hash = <T as system::Trait>::Hash, AccountId = <T as system::Trait>::AccountId
{
/// A motion (given hash) has been proposed (by given account) with a threshold (given u32).
Proposed(AccountId, ProposalIndex, Hash, u32),
Expand Down
4 changes: 1 addition & 3 deletions srml/council/src/seats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,7 @@ decl_storage! {

decl_event!(
/// An event in this module.
pub enum Event<T> with RawEvent<AccountId>
where <T as system::Trait>::AccountId
{
pub enum Event<T> where AccountId = <T as system::Trait>::AccountId {
/// reaped voter, reaper
VoterReaped(AccountId, AccountId),
/// slashed reaper
Expand Down
4 changes: 1 addition & 3 deletions srml/council/src/voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ decl_storage! {

/// An event in this module.
decl_event!(
pub enum Event<T> with RawEvent<Hash>
where <T as system::Trait>::Hash
{
pub enum Event<T> where Hash = <T as system::Trait>::Hash {
/// A voting tally has happened for a referendum cancelation vote.
/// Last three are yes, no, abstain counts.
TallyCancelation(Hash, u32, u32, u32),
Expand Down
5 changes: 3 additions & 2 deletions srml/democracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ decl_storage! {

decl_event!(
/// An event in this module.
pub enum Event<T> with RawEvent<Balance, AccountId>
where <T as balances::Trait>::Balance, <T as system::Trait>::AccountId
pub enum Event<T> where
Balance = <T as balances::Trait>::Balance,
AccountId = <T as system::Trait>::AccountId
{
Tabled(PropIndex, Balance, Vec<AccountId>),
Started(ReferendumIndex, VoteThreshold),
Expand Down
4 changes: 1 addition & 3 deletions srml/example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> with RawEvent<B>
where <T as balances::Trait>::Balance
{
pub enum Event<T> where B = <T as balances::Trait>::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),
Expand Down
4 changes: 1 addition & 3 deletions srml/session/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ decl_module! {

/// An event in this module.
decl_event!(
pub enum Event<T> with RawEvent<BlockNumber>
where <T as system::Trait>::BlockNumber
{
pub enum Event<T> where BlockNumber = <T as system::Trait>::BlockNumber {
/// New session has happened. Note that the argument is the session index, not the block
/// number as the type might suggest.
NewSession(BlockNumber),
Expand Down
5 changes: 3 additions & 2 deletions srml/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ decl_module! {

/// An event in this module.
decl_event!(
pub enum Event<T> with RawEvent<Balance, AccountId>
where <T as balances::Trait>::Balance, <T as system::Trait>::AccountId
pub enum Event<T> where
Balance = <T as balances::Trait>::Balance,
AccountId = <T as system::Trait>::AccountId
{
/// All validators have been rewarded by the given balance.
Reward(Balance),
Expand Down
77 changes: 32 additions & 45 deletions srml/support/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,22 @@
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_param:ident = <$generic:ident as $trait:path>::$trait_type:ident),*
{
$(
$(#[doc = $doc_attr:tt])*
$event:ident( $( $param:path ),* ),
$events:tt
)*
}
) => {
pub type Event<$( $evt_generic_param )*> = RawEvent<$( <$generic as $trait>::$trait_type ),*>;
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 ),*> {
$(
$( #[doc = $doc_attr] )*
$event($( $param ),*),
$events
)*
}
impl<$( $generic_param ),*> From<RawEvent<$( $generic_param ),*>> for () {
Expand All @@ -44,25 +43,15 @@ macro_rules! decl_event {
impl<$( $generic_param ),*> RawEvent<$( $generic_param ),*> {
#[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 )* ), " }")
}
}
};
(
$(#[$attr:meta])*
pub enum Event {
$(
$(#[doc = $doc_attr:tt])*
$event:ident,
$events:tt
)*
}
) => {
Expand All @@ -72,8 +61,7 @@ macro_rules! decl_event {
$(#[$attr])*
pub enum Event {
$(
$( #[doc = $doc_attr] )*
$event,
$events
)*
}
impl From<Event> for () {
Expand All @@ -82,46 +70,38 @@ macro_rules! decl_event {
impl Event {
#[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 )* ), " }")
}
}
}
}

#[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 )*)
)
};
(
Expand Down Expand Up @@ -223,11 +203,12 @@ mod tests {
}

decl_event!(
pub enum Event<T> with RawEvent<Balance>
where <T as Trait>::Balance
pub enum Event<T> where Balance = <T as Trait>::Balance
{
/// Hi, I am a comment.
TestEvent(Balance),
/// Dog
EventWithoutParams,
}
);
}
Expand All @@ -243,8 +224,7 @@ mod tests {
}

decl_event!(
pub enum Event<T> with RawEvent<Balance>
where <T as Trait>::Balance
pub enum Event<T> where Balance = <T as Trait>::Balance
{
TestEvent(Balance),
}
Expand Down Expand Up @@ -277,7 +257,14 @@ 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_module",
concat!(
"{",
r#" "TestEvent": { "params": [ "Balance" ], "description": [ " Hi, I am a comment." ] },"#,
r#" "EventWithoutParams": { "params": null, "description": [ " Dog" ] }"#,
" }"
)
),
("event_module2", r#"{ "TestEvent": { "params": [ "Balance" ], "description": [ ] } }"#),
]
);
Expand Down
6 changes: 2 additions & 4 deletions srml/support/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ mod tests {
}

decl_event!(
pub enum Event<T> with RawEvent<Balance>
where <T as Trait>::Balance
pub enum Event<T> where Balance = <T as Trait>::Balance
{
/// Hi, I am a comment.
TestEvent(Balance),
Expand All @@ -179,8 +178,7 @@ mod tests {
}

decl_event!(
pub enum Event<T> with RawEvent<Balance>
where <T as Trait>::Balance
pub enum Event<T> where Balance = <T as Trait>::Balance
{
TestEvent(Balance),
}
Expand Down
5 changes: 3 additions & 2 deletions srml/treasury/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ decl_storage! {

/// An event in this module.
decl_event!(
pub enum Event<T> with RawEvent<Balance, AccountId>
where <T as balances::Trait>::Balance, <T as system::Trait>::AccountId
pub enum Event<T> where
Balance = <T as balances::Trait>::Balance,
AccountId = <T as system::Trait>::AccountId
{
/// New proposal.
Proposed(ProposalIndex),
Expand Down