Switch to new proc-macro crate for implementing traits for tuples (#3598)

* Switch to new proc-macro crate for implementing traits for tuples

* Switch back to stable `Cargo.lock` format

* Reduce tuple number to match rusts default implementation
This commit is contained in:
Bastian Köcher
2019-09-11 19:10:02 +02:00
committed by GitHub
parent 433d752831
commit d136b8eb81
16 changed files with 139 additions and 306 deletions
+1
View File
@@ -14,6 +14,7 @@ sr-primitives = { path = "../../core/sr-primitives", default-features = false }
support = { package = "srml-support", path = "../support", default-features = false }
system = { package = "srml-system", path = "../system", default-features = false }
runtime-io ={ package = "sr-io", path = "../../core/sr-io", default-features = false }
impl-trait-for-tuples = "0.1"
[features]
default = ["std"]
+2 -25
View File
@@ -22,7 +22,7 @@
use rstd::{result, prelude::*};
use rstd::collections::btree_set::BTreeSet;
use support::{decl_module, decl_storage, for_each_tuple, StorageValue};
use support::{decl_module, decl_storage, StorageValue};
use support::traits::{FindAuthor, VerifySeal, Get};
use support::dispatch::Result as DispatchResult;
use codec::{Encode, Decode};
@@ -112,6 +112,7 @@ pub trait Trait: system::Trait {
/// An event handler for the authorship module. There is a dummy implementation
/// for `()`, which does nothing.
#[impl_trait_for_tuples::impl_for_tuples(30)]
pub trait EventHandler<Author, BlockNumber> {
/// Note that the given account ID is the author of the current block.
fn note_author(author: Author);
@@ -121,30 +122,6 @@ pub trait EventHandler<Author, BlockNumber> {
fn note_uncle(author: Author, age: BlockNumber);
}
macro_rules! impl_event_handler {
() => (
impl<A, B> EventHandler<A, B> for () {
fn note_author(_author: A) { }
fn note_uncle(_author: A, _age: B) { }
}
);
( $($t:ident)* ) => {
impl<Author: Clone, BlockNumber: Clone, $($t: EventHandler<Author, BlockNumber>),*>
EventHandler<Author, BlockNumber> for ($($t,)*)
{
fn note_author(author: Author) {
$($t::note_author(author.clone());)*
}
fn note_uncle(author: Author, age: BlockNumber) {
$($t::note_uncle(author.clone(), age.clone());)*
}
}
}
}
for_each_tuple!(impl_event_handler);
/// Additional filtering on uncles that pass preliminary ancestry checks.
///
/// This should do work such as checking seals
@@ -12,6 +12,7 @@ rstd = { package = "sr-std", path = "../../core/sr-std", default-features = fals
sr-primitives = { path = "../../core/sr-primitives", default-features = false }
support = { package = "srml-support", path = "../support", default-features = false }
srml-system = { path = "../system", default-features = false }
impl-trait-for-tuples = "0.1"
[dev-dependencies]
primitives = { package = "substrate-primitives", path = "../../core/primitives", default-features = false }
+2 -19
View File
@@ -26,7 +26,7 @@ use support::StorageValue;
use sr_primitives::traits::{One, Zero, SaturatedConversion};
use rstd::{prelude::*, result, cmp, vec};
use codec::Decode;
use support::{decl_module, decl_storage, for_each_tuple};
use support::{decl_module, decl_storage};
use support::traits::Get;
use srml_system::{ensure_none, Trait as SystemTrait};
@@ -214,30 +214,13 @@ impl<T: Trait> Module<T> {
}
/// Called when finalization stalled at a given number.
#[impl_trait_for_tuples::impl_for_tuples(30)]
pub trait OnFinalizationStalled<N> {
/// The parameter here is how many more blocks to wait before applying
/// changes triggered by finality stalling.
fn on_stalled(further_wait: N, median: N);
}
macro_rules! impl_on_stalled {
() => (
impl<N> OnFinalizationStalled<N> for () {
fn on_stalled(_: N, _: N) {}
}
);
( $($t:ident)* ) => {
impl<NUM: Clone, $($t: OnFinalizationStalled<NUM>),*> OnFinalizationStalled<NUM> for ($($t,)*) {
fn on_stalled(further_wait: NUM, median: NUM) {
$($t::on_stalled(further_wait.clone(), median.clone());)*
}
}
}
}
for_each_tuple!(impl_on_stalled);
impl<T: Trait> ProvideInherent for Module<T> {
type Call = Call<T>;
type Error = MakeFatalError<()>;
+1
View File
@@ -16,6 +16,7 @@ system = { package = "srml-system", path = "../system", default-features = false
timestamp = { package = "srml-timestamp", path = "../timestamp", default-features = false }
substrate-trie = { path = "../../core/trie", default-features = false, optional = true }
runtime-io ={ package = "sr-io", path = "../../core/sr-io", default-features = false }
impl-trait-for-tuples = "0.1"
[dev-dependencies]
primitives = { package = "substrate-primitives", path = "../../core/primitives" }
+39 -50
View File
@@ -126,8 +126,8 @@ use sr_primitives::weights::SimpleDispatchInfo;
use sr_primitives::traits::{Convert, Zero, Member, OpaqueKeys};
use sr_staking_primitives::SessionIndex;
use support::{
dispatch::Result, ConsensusEngineId, StorageValue, StorageDoubleMap, for_each_tuple,
decl_module, decl_event, decl_storage,
dispatch::Result, ConsensusEngineId, StorageValue, StorageDoubleMap, decl_module, decl_event,
decl_storage,
};
use support::{ensure, traits::{OnFreeBalanceZero, Get, FindAuthor}, Parameter};
use system::{self, ensure_signed};
@@ -251,63 +251,52 @@ pub trait OneSessionHandler<ValidatorId> {
/// A validator got disabled. Act accordingly until a new session begins.
fn on_disabled(_validator_index: usize);
}
macro_rules! impl_session_handlers {
() => (
impl<AId> SessionHandler<AId> for () {
fn on_genesis_session<Ks: OpaqueKeys>(_: &[(AId, Ks)]) {}
fn on_new_session<Ks: OpaqueKeys>(_: bool, _: &[(AId, Ks)], _: &[(AId, Ks)]) {}
fn on_before_session_ending() {}
fn on_disabled(_: usize) {}
}
);
#[impl_trait_for_tuples::impl_for_tuples(30)]
#[tuple_types_no_default_trait_bound]
impl<AId> SessionHandler<AId> for Tuple {
for_tuples!( where #( Tuple: OneSessionHandler<AId> )* );
( $($t:ident)* ) => {
impl<AId, $( $t: OneSessionHandler<AId> ),*> SessionHandler<AId> for ( $( $t , )* ) {
fn on_genesis_session<Ks: OpaqueKeys>(validators: &[(AId, Ks)]) {
$(
let our_keys: Box<dyn Iterator<Item=_>> = Box::new(validators.iter()
.map(|k| (&k.0, k.1.get::<$t::Key>(<$t::Key as AppKey>::ID)
.unwrap_or_default())));
fn on_genesis_session<Ks: OpaqueKeys>(validators: &[(AId, Ks)]) {
for_tuples!(
#(
let our_keys: Box<dyn Iterator<Item=_>> = Box::new(validators.iter()
.map(|k| (&k.0, k.1.get::<Tuple::Key>(<Tuple::Key as AppKey>::ID)
.unwrap_or_default())));
$t::on_genesis_session(our_keys);
)*
}
fn on_new_session<Ks: OpaqueKeys>(
changed: bool,
validators: &[(AId, Ks)],
queued_validators: &[(AId, Ks)],
) {
$(
let our_keys: Box<dyn Iterator<Item=_>> = Box::new(validators.iter()
.map(|k| (&k.0, k.1.get::<$t::Key>(<$t::Key as AppKey>::ID)
.unwrap_or_default())));
let queued_keys: Box<dyn Iterator<Item=_>> = Box::new(queued_validators.iter()
.map(|k| (&k.0, k.1.get::<$t::Key>(<$t::Key as AppKey>::ID)
.unwrap_or_default())));
$t::on_new_session(changed, our_keys, queued_keys);
)*
}
Tuple::on_genesis_session(our_keys);
)*
)
}
fn on_before_session_ending() {
$(
$t::on_before_session_ending();
)*
}
fn on_new_session<Ks: OpaqueKeys>(
changed: bool,
validators: &[(AId, Ks)],
queued_validators: &[(AId, Ks)],
) {
for_tuples!(
#(
let our_keys: Box<dyn Iterator<Item=_>> = Box::new(validators.iter()
.map(|k| (&k.0, k.1.get::<Tuple::Key>(<Tuple::Key as AppKey>::ID)
.unwrap_or_default())));
let queued_keys: Box<dyn Iterator<Item=_>> = Box::new(queued_validators.iter()
.map(|k| (&k.0, k.1.get::<Tuple::Key>(<Tuple::Key as AppKey>::ID)
.unwrap_or_default())));
Tuple::on_new_session(changed, our_keys, queued_keys);
)*
)
}
fn on_disabled(i: usize) {
$(
$t::on_disabled(i);
)*
}
}
fn on_before_session_ending() {
for_tuples!( #( Tuple::on_before_session_ending(); )* )
}
fn on_disabled(i: usize) {
for_tuples!( #( Tuple::on_disabled(i); )* )
}
}
for_each_tuple!(impl_session_handlers);
/// Handler for selecting the genesis validator set.
pub trait SelectInitialValidators<ValidatorId> {
/// Returns the initial validator set. If `None` is returned
+1
View File
@@ -17,6 +17,7 @@ srml-support-procedural = { package = "srml-support-procedural", path = "./proce
paste = "0.1"
once_cell = { version = "0.1.6", default-features = false, optional = true }
bitmask = { version = "0.5", default-features = false }
impl-trait-for-tuples = "0.1"
[dev-dependencies]
pretty_assertions = "0.6.1"
-15
View File
@@ -266,21 +266,6 @@ pub enum Void {}
#[doc(hidden)]
pub use serde::{Serialize, Deserialize};
/// Programatically create derivations for tuples of up to 19 elements. You provide a second macro
/// which is called once per tuple size, along with a number of identifiers, one for each element
/// of the tuple.
#[macro_export]
macro_rules! for_each_tuple {
($m:ident) => {
for_each_tuple! { @IMPL $m !! A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, }
};
(@IMPL $m:ident !!) => { $m! { } };
(@IMPL $m:ident !! $h:ident, $($t:ident,)*) => {
$m! { $h $($t)* }
for_each_tuple! { @IMPL $m !! $($t,)* }
}
}
#[cfg(test)]
mod tests {
use super::*;
+1 -20
View File
@@ -24,8 +24,6 @@ use primitives::u32_trait::Value as U32;
use crate::sr_primitives::traits::{MaybeSerializeDebug, SimpleArithmetic, Saturating};
use crate::sr_primitives::ConsensusEngineId;
use super::for_each_tuple;
/// Anything that can have a `::len()` method.
pub trait Len {
/// Return the length of data type.
@@ -63,29 +61,12 @@ impl<V: PartialEq, T: Get<V>> Contains<V> for T {
}
/// The account with the given id was killed.
#[impl_trait_for_tuples::impl_for_tuples(30)]
pub trait OnFreeBalanceZero<AccountId> {
/// The account was the given id was killed.
fn on_free_balance_zero(who: &AccountId);
}
macro_rules! impl_on_free_balance_zero {
() => (
impl<AccountId> OnFreeBalanceZero<AccountId> for () {
fn on_free_balance_zero(_: &AccountId) {}
}
);
( $($t:ident)* ) => {
impl<AccountId, $($t: OnFreeBalanceZero<AccountId>),*> OnFreeBalanceZero<AccountId> for ($($t,)*) {
fn on_free_balance_zero(who: &AccountId) {
$($t::on_free_balance_zero(who);)*
}
}
}
}
for_each_tuple!(impl_on_free_balance_zero);
/// Trait for a hook to get called when some balance has been minted, causing dilution.
pub trait OnDilution<Balance> {
/// Some `portion` of the total balance just "grew" by `minted`. `portion` is the pre-growth
+1
View File
@@ -14,6 +14,7 @@ runtime-io ={ package = "sr-io", path = "../../core/sr-io", default-features = f
sr-primitives = { path = "../../core/sr-primitives", default-features = false }
sr-version = { path = "../../core/sr-version", default-features = false }
support = { package = "srml-support", path = "../support", default-features = false }
impl-trait-for-tuples = "0.1"
[dev-dependencies]
criterion = "0.2"
+2 -19
View File
@@ -112,7 +112,7 @@ use sr_primitives::{
use primitives::storage::well_known_keys;
use support::{
storage, decl_module, decl_event, decl_storage, StorageDoubleMap, StorageValue, StorageMap,
Parameter, for_each_tuple, traits::{Contains, Get}, decl_error,
Parameter, traits::{Contains, Get}, decl_error,
};
use safe_mix::TripletMix;
use codec::{Encode, Decode};
@@ -126,29 +126,12 @@ use primitives::ChangesTrieConfiguration;
pub mod offchain;
/// Handler for when a new account has been created.
#[impl_trait_for_tuples::impl_for_tuples(30)]
pub trait OnNewAccount<AccountId> {
/// A new account `who` has been registered.
fn on_new_account(who: &AccountId);
}
macro_rules! impl_on_new_account {
() => (
impl<AccountId> OnNewAccount<AccountId> for () {
fn on_new_account(_: &AccountId) {}
}
);
( $($t:ident)* ) => {
impl<AccountId, $($t: OnNewAccount<AccountId>),*> OnNewAccount<AccountId> for ($($t,)*) {
fn on_new_account(who: &AccountId) {
$($t::on_new_account(who);)*
}
}
}
}
for_each_tuple!(impl_on_new_account);
/// Determiner to say whether a given account is unused.
pub trait IsDeadAccount<AccountId> {
/// Is the given account dead?
+1
View File
@@ -12,6 +12,7 @@ sr-primitives = { path = "../../core/sr-primitives", default-features = false }
inherents = { package = "substrate-inherents", path = "../../core/inherents", default-features = false }
support = { package = "srml-support", path = "../support", default-features = false }
system = { package = "srml-system", path = "../system", default-features = false }
impl-trait-for-tuples = "0.1"
[dev-dependencies]
runtime-io ={ package = "sr-io", path = "../../core/sr-io" }
+2 -19
View File
@@ -96,7 +96,7 @@ use codec::Encode;
use codec::Decode;
#[cfg(feature = "std")]
use inherents::ProvideInherentData;
use support::{StorageValue, Parameter, decl_storage, decl_module, for_each_tuple};
use support::{StorageValue, Parameter, decl_storage, decl_module};
use support::traits::{Time, Get};
use sr_primitives::traits::{
SimpleArithmetic, Zero, SaturatedConversion, Scale
@@ -183,28 +183,11 @@ impl ProvideInherentData for InherentDataProvider {
}
/// A trait which is called when the timestamp is set.
#[impl_trait_for_tuples::impl_for_tuples(30)]
pub trait OnTimestampSet<Moment> {
fn on_timestamp_set(moment: Moment);
}
macro_rules! impl_timestamp_set {
() => (
impl<Moment> OnTimestampSet<Moment> for () {
fn on_timestamp_set(_: Moment) {}
}
);
( $($t:ident)* ) => {
impl<Moment: Copy, $($t: OnTimestampSet<Moment>),*> OnTimestampSet<Moment> for ($($t,)*) {
fn on_timestamp_set(moment: Moment) {
$($t::on_timestamp_set(moment);)*
}
}
}
}
for_each_tuple!(impl_timestamp_set);
/// The module configuration trait
pub trait Trait: system::Trait {
/// Type used for expressing timestamp.