Make decl_module! implement OnFinalise (#947)

This commit is contained in:
Bastian Köcher
2018-10-23 09:58:15 +02:00
committed by GitHub
parent 52093c4b7a
commit 1ba73e0e88
19 changed files with 158 additions and 117 deletions
+71 -4
View File
@@ -76,6 +76,7 @@ macro_rules! decl_module {
$(#[$attr])*
pub struct $mod_type<$trait_instance: $trait_name>
for enum $call_type where origin: $origin_type where system = system
{}
[]
$($t)*
);
@@ -91,6 +92,7 @@ macro_rules! decl_module {
$(#[$attr])*
pub struct $mod_type<$trait_instance: $trait_name>
for enum $call_type where origin: $origin_type where system = $system
{}
[]
$($t)*
);
@@ -100,6 +102,26 @@ macro_rules! decl_module {
$(#[$attr:meta])*
pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident>
for enum $call_type:ident where origin: $origin_type:ty where system = $system:ident
{ $( $on_finalise:tt )* }
[ $($t:tt)* ]
$(#[doc = $doc_attr:tt])*
fn on_finalise($($param_name:ident : $param:ty),* ) { $( $impl:tt )* }
$($rest:tt)*
) => {
decl_module!(@normalize
$(#[$attr])*
pub struct $mod_type<$trait_instance: $trait_name>
for enum $call_type where origin: $origin_type where system = $system
{ fn on_finalise( $( $param_name : $param ),* ) { $( $impl )* } }
[ $($t)* ]
$($rest)*
);
};
(@normalize
$(#[$attr:meta])*
pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident>
for enum $call_type:ident where origin: $origin_type:ty where system = $system:ident
{ $( $on_finalise:tt )* }
[ $($t:tt)* ]
$(#[doc = $doc_attr:tt])*
fn $fn_name:ident(origin $(, $param_name:ident : $param:ty)* ) -> $result:ty ;
@@ -109,6 +131,7 @@ macro_rules! decl_module {
$(#[$attr])*
pub struct $mod_type<$trait_instance: $trait_name>
for enum $call_type where origin: $origin_type where system = $system
{ $( $on_finalise )* }
[ $($t)* $(#[doc = $doc_attr])* fn $fn_name(origin $( , $param_name : $param )* ) -> $result; ]
$($rest)*
);
@@ -117,6 +140,7 @@ macro_rules! decl_module {
$(#[$attr:meta])*
pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident>
for enum $call_type:ident where origin: $origin_type:ty where system = $system:ident
{ $( $on_finalise:tt )* }
[ $($t:tt)* ]
$(#[doc = $doc_attr:tt])*
fn $fn_name:ident($( $param_name:ident : $param:ty),* ) -> $result:ty ;
@@ -126,6 +150,7 @@ macro_rules! decl_module {
$(#[$attr])*
pub struct $mod_type<$trait_instance: $trait_name>
for enum $call_type where origin: $origin_type where system = $system
{ $( $on_finalise )* }
[ $($t)* $(#[doc = $doc_attr])* fn $fn_name(root $( , $param_name : $param )* ) -> $result; ]
$($rest)*
);
@@ -134,6 +159,7 @@ macro_rules! decl_module {
$(#[$attr:meta])*
pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident>
for enum $call_type:ident where origin: $origin_type:ty where system = $system:ident
{ $( $on_finalise:tt )* }
[ $($t:tt)* ]
) => {
decl_module!(@imp
@@ -142,6 +168,7 @@ macro_rules! decl_module {
for enum $call_type where origin: $origin_type where system = $system {
$($t)*
}
{ $( $on_finalise )* }
);
};
@@ -161,14 +188,46 @@ macro_rules! decl_module {
}
};
(@impl_on_finalise
$module:ident<$trait_instance:ident: $trait_name:ident>;
fn on_finalise() { $( $impl:tt )* }
) => {
impl<$trait_instance: $trait_name>
$crate::runtime_primitives::traits::OnFinalise<$trait_instance::BlockNumber>
for $module<$trait_instance> {
fn on_finalise(_block_number_not_used: $trait_instance::BlockNumber) { $( $impl )* }
}
};
(@impl_on_finalise
$module:ident<$trait_instance:ident: $trait_name:ident>;
fn on_finalise($param:ident : $param_ty:ty) { $( $impl:tt )* }
) => {
impl<$trait_instance: $trait_name>
$crate::runtime_primitives::traits::OnFinalise<$trait_instance::BlockNumber>
for $module<$trait_instance> {
fn on_finalise($param: $param_ty) { $( $impl )* }
}
};
(@impl_on_finalise
$module:ident<$trait_instance:ident: $trait_name:ident>;
) => {
impl<$trait_instance: $trait_name>
$crate::runtime_primitives::traits::OnFinalise<$trait_instance::BlockNumber>
for $module<$trait_instance> {}
};
(@imp
$(#[$attr:meta])*
pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident>
for enum $call_type:ident where origin: $origin_type:ty where system = $system:ident {
$(
$(#[doc = $doc_attr:tt])*
fn $fn_name:ident($from:ident $( , $param_name:ident : $param:ty)*) -> $result:ty;
)*}
$(
$(#[doc = $doc_attr:tt])*
fn $fn_name:ident($from:ident $( , $param_name:ident : $param:ty)*) -> $result:ty;
)*
}
{ $( $on_finalise:tt )* }
) => {
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Clone, Copy, PartialEq, Eq)]
@@ -185,6 +244,12 @@ macro_rules! decl_module {
#[cfg(not(feature = "std"))]
pub struct $mod_type<$trait_instance: $trait_name>(::core::marker::PhantomData<$trait_instance>);
decl_module! {
@impl_on_finalise
$mod_type<$trait_instance: $trait_name>;
$( $on_finalise )*
}
#[cfg(feature = "std")]
$(#[$attr])*
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
@@ -615,6 +680,7 @@ mod tests {
pub trait Trait {
type Origin;
type BlockNumber;
}
pub mod system {
@@ -722,6 +788,7 @@ mod tests {
impl Trait for TraitImpl {
type Origin = u32;
type BlockNumber = u32;
}
#[test]
+10
View File
@@ -433,6 +433,7 @@ mod tests {
mod system {
pub trait Trait {
type Origin;
type BlockNumber;
}
decl_module! {
@@ -449,6 +450,7 @@ mod tests {
mod system_renamed {
pub trait Trait {
type Origin;
type BlockNumber;
}
decl_module! {
@@ -466,6 +468,7 @@ mod tests {
pub trait Trait {
type Origin;
type Balance;
type BlockNumber;
}
decl_module! {
@@ -488,6 +491,7 @@ mod tests {
pub trait Trait {
type Origin;
type Balance;
type BlockNumber;
}
decl_module! {
@@ -539,29 +543,35 @@ mod tests {
impl event_module::Trait for TestRuntime {
type Origin = u32;
type Balance = u32;
type BlockNumber = u32;
}
impl event_module2::Trait for TestRuntime {
type Origin = u32;
type Balance = u32;
type BlockNumber = u32;
}
impl system::Trait for TestRuntime {
type Origin = u32;
type BlockNumber = u32;
}
impl event_module::Trait for TestRuntime2 {
type Origin = u32;
type Balance = u32;
type BlockNumber = u32;
}
impl event_module2::Trait for TestRuntime2 {
type Origin = u32;
type Balance = u32;
type BlockNumber = u32;
}
impl system_renamed::Trait for TestRuntime2 {
type Origin = u32;
type BlockNumber = u32;
}
const EXPECTED_METADATA: OuterEventMetadata = OuterEventMetadata {
+6
View File
@@ -111,6 +111,7 @@ mod tests {
pub trait Trait {
type Origin: Into<Option<RawOrigin<Self::AccountId>>> + From<RawOrigin<Self::AccountId>>;
type AccountId;
type BlockNumber;
}
decl_module! {
@@ -148,6 +149,7 @@ mod tests {
pub trait Trait {
type Origin;
type Balance;
type BlockNumber;
}
decl_event!(
@@ -175,6 +177,7 @@ mod tests {
pub trait Trait {
type Origin;
type Balance;
type BlockNumber;
}
decl_event!(
@@ -226,16 +229,19 @@ mod tests {
impl event_module::Trait for TestRuntime {
type Origin = Origin;
type Balance = u32;
type BlockNumber = u32;
}
impl event_module2::Trait for TestRuntime {
type Origin = Origin;
type Balance = u32;
type BlockNumber = u32;
}
impl system::Trait for TestRuntime {
type Origin = Origin;
type AccountId = u32;
type BlockNumber = u32;
}
impl_runtime_metadata!(
@@ -2702,7 +2702,8 @@ mod tests {
}
pub trait Trait {
type Origin: codec::Encode + codec::Decode + ::std::default::Default;
type Origin: codec::Encode + codec::Decode + ::std::default::Default;
type BlockNumber;
}
decl_module! {
@@ -2754,6 +2755,7 @@ mod tests {
impl Trait for TraitImpl {
type Origin = u32;
type BlockNumber = u32;
}
const EXPECTED_METADATA: StorageMetadata = StorageMetadata {
@@ -2932,7 +2934,8 @@ mod tests {
#[allow(dead_code)]
mod test2 {
pub trait Trait {
type Origin;
type Origin;
type BlockNumber;
}
decl_module! {
@@ -2959,5 +2962,6 @@ mod test2 {
impl Trait for TraitImpl {
type Origin = u32;
type BlockNumber = u32;
}
}