Simplify deposit_event declaration in decl_module (#3506)

* simplify module deposit_event declaration

* fix

* bump version

* fix

* fix test

* fix doc
This commit is contained in:
thiolliere
2019-08-29 14:34:02 +02:00
committed by Bastian Köcher
parent f830db9642
commit b07fd450e2
22 changed files with 43 additions and 67 deletions
+10 -37
View File
@@ -203,7 +203,7 @@ impl<T> Parameter for T where T: Codec + Clone + Eq {}
/// * `deposit_event`: Helper function for depositing an [event](https://docs.substrate.dev/docs/event-enum).
/// The default behavior is to call `deposit_event` from the [System module](../srml_system/index.html).
/// However, you can write your own implementation for events in your runtime. To use the default behavior,
/// add `fn deposit_event<T>() = default;` to your `Module`.
/// add `fn deposit_event() = default;` to your `Module`.
///
/// The following reserved functions also take the block number (with type `T::BlockNumber`) as an optional input:
///
@@ -288,7 +288,7 @@ macro_rules! decl_module {
{ $( $constants:tt )* }
[ $( $dispatchables:tt )* ]
$(#[doc = $doc_attr:tt])*
$vis:vis fn deposit_event $(<$dpeg:ident $(, $dpeg_instance:ident)?>)* () = default;
$vis:vis fn deposit_event() = default;
$($rest:tt)*
) => {
$crate::decl_module!(@normalize
@@ -296,7 +296,7 @@ macro_rules! decl_module {
pub struct $mod_type<$trait_instance: $trait_name$(<I>, I: $instantiable $(= $module_default_instance)?)?>
for enum $call_type where origin: $origin_type, system = $system
{ $( $other_where_bounds )* }
{ $vis fn deposit_event $(<$dpeg $(, $dpeg_instance)?>)* () = default; }
{ $vis fn deposit_event() = default; }
{ $( $on_initialize )* }
{ $( $on_finalize )* }
{ $( $offchain )* }
@@ -317,23 +317,11 @@ macro_rules! decl_module {
{ $( $constants:tt )* }
[ $( $dispatchables:tt )* ]
$(#[doc = $doc_attr:tt])*
$vis:vis fn deposit_event $(<$dpeg:ident $(, $dpeg_instance:ident)?>)* (
$($param_name:ident : $param:ty),*
) { $( $impl:tt )* }
$vis:vis fn deposit_event
$($rest:tt)*
) => {
$crate::decl_module!(@normalize
$(#[$attr])*
pub struct $mod_type<$trait_instance: $trait_name$(<I>, I: $instantiable $(= $module_default_instance)?)?>
for enum $call_type where origin: $origin_type, system = $system
{ $( $other_where_bounds )* }
{ $vis fn deposit_event $(<$dpeg $(, $dpeg_instance)?>)* ($( $param_name: $param ),* ) { $( $impl )* } }
{ $( $on_initialize )* }
{ $( $on_finalize )* }
{ $( $offchain )* }
{ $( $constants )* }
[ $( $dispatchables )* ]
$($rest)*
compile_error!(
"`deposit_event` function is reserved and must follow the syntax: `$vis:vis fn deposit_event() = default;`"
);
};
(@normalize
@@ -685,25 +673,10 @@ macro_rules! decl_module {
impl<$trait_instance: $trait_name$(<I>, $instance: $instantiable)?> $module<$trait_instance $(, $instance)?>
where $( $other_where_bounds )*
{
$vis fn deposit_event(event: Event$(<$event_trait_instance $(, $event_instance)?>)?) {
<$system::Module<$trait_instance>>::deposit_event(
<$trait_instance as $trait_name$(<$instance>)?>::Event::from(event).into()
);
}
}
};
(@impl_deposit_event
$module:ident<$trait_instance:ident: $trait_name:ident$(<I>, $instance:ident: $instantiable:path)?>;
$system:ident;
{ $( $other_where_bounds:tt )* }
$vis:vis fn deposit_event($param:ident : $param_ty:ty) { $( $impl:tt )* }
) => {
impl<$trait_instance: $trait_name$(<I>, $instance: $instantiable)?> $module<$trait_instance $(, $instance)?>
where $( $other_where_bounds )*
{
$vis fn deposit_event($param: $param_ty) {
$( $impl )*
$vis fn deposit_event(
event: impl Into<< $trait_instance as $trait_name $(<$instance>)? >::Event>
) {
<$system::Module<$trait_instance>>::deposit_event(event.into())
}
}
};
@@ -55,7 +55,7 @@ mod module1 {
{
fn offchain_worker() {}
fn deposit_event<T, I>() = default;
fn deposit_event() = default;
fn one(origin) {
system::ensure_root(origin)?;
@@ -132,7 +132,7 @@ mod module2 {
pub struct Module<T: Trait<I>, I: Instance=DefaultInstance> for enum Call where
origin: <T as system::Trait>::Origin
{
fn deposit_event<T, I>() = default;
fn deposit_event() = default;
}
}
@@ -38,7 +38,7 @@ error: Invalid call fn name: `offchain_worker`, name is reserved and doesn't mat
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
error: Invalid call fn name: `deposit_event`, name is reserved and doesn't match expected signature, please refer to `decl_module!` documentation to see the appropriate usage, or rename it to an unreserved keyword.
error: `deposit_event` function is reserved and must follow the syntax: `$vis:vis fn deposit_event() = default;`
--> $DIR/on_initialize.rs:30:1
|
30 | reserved!(on_finalize on_initialize on_finalise on_initialise offchain_worker deposit_event);
+6 -3
View File
@@ -12,8 +12,11 @@ pub trait Trait: 'static + Eq + Clone {
srml_support::decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
pub fn deposit_event(_event: T::Event) {
}
}
}
impl<T: Trait> Module<T> {
pub fn deposit_event(_event: impl Into<T::Event>) {
}
}
@@ -49,4 +52,4 @@ pub fn ensure_root<OuterOrigin, AccountId>(o: OuterOrigin) -> Result<(), &'stati
where OuterOrigin: Into<Result<RawOrigin<AccountId>, OuterOrigin>>
{
o.into().map(|_| ()).map_err(|_| "bad origin: expected to be a root origin")
}
}