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

Commit 669ed36

Browse files
kianenigmabkchr
andauthored
Proposal: Flatten AllPallets and similar types (#11813)
* flratten AllPallets types * feature flag it * fix * fix * fmt * remove todo * Update frame/support/src/traits/metadata.rs Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * Update frame/support/src/migrations.rs Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * fix * mark as deprecated * add docs * fix ui test? * fmt Co-authored-by: parity-processbot <> Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
1 parent bfc2d08 commit 669ed36

15 files changed

Lines changed: 121 additions & 108 deletions

File tree

frame/support/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,7 @@ no-metadata-docs = ["frame-support-procedural/no-metadata-docs"]
7373
# By default some types have documentation, `full-metadata-docs` allows to add documentation to
7474
# more types in the metadata.
7575
full-metadata-docs = ["scale-info/docs"]
76+
# Generate impl-trait for tuples with the given number of tuples. Will be needed as the number of
77+
# pallets in a runtime grows. Does increase the compile time!
78+
tuples-96 = []
79+
tuples-128 = []

frame/support/procedural/src/construct_runtime/mod.rs

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -308,47 +308,26 @@ fn decl_all_pallets<'a>(
308308
names.push(&pallet_declaration.name);
309309
}
310310

311-
// Make nested tuple structure like:
312-
// `((FirstPallet, (SecondPallet, ( ... , LastPallet) ... ))))`
313-
// But ignore the system pallet.
314-
let all_pallets_without_system = names
315-
.iter()
316-
.filter(|n| **n != SYSTEM_PALLET_NAME)
317-
.rev()
318-
.fold(TokenStream2::default(), |combined, name| quote!((#name, #combined)));
319-
320-
// Make nested tuple structure like:
321-
// `((FirstPallet, (SecondPallet, ( ... , LastPallet) ... ))))`
322-
let all_pallets_with_system = names
323-
.iter()
324-
.rev()
325-
.fold(TokenStream2::default(), |combined, name| quote!((#name, #combined)));
326-
327-
// Make nested tuple structure like:
328-
// `((LastPallet, (SecondLastPallet, ( ... , FirstPallet) ... ))))`
329-
// But ignore the system pallet.
330-
let all_pallets_without_system_reversed = names
331-
.iter()
332-
.filter(|n| **n != SYSTEM_PALLET_NAME)
333-
.fold(TokenStream2::default(), |combined, name| quote!((#name, #combined)));
334-
335-
// Make nested tuple structure like:
336-
// `((LastPallet, (SecondLastPallet, ( ... , FirstPallet) ... ))))`
337-
let all_pallets_with_system_reversed = names
338-
.iter()
339-
.fold(TokenStream2::default(), |combined, name| quote!((#name, #combined)));
340-
341311
let system_pallet = match names.iter().find(|n| **n == SYSTEM_PALLET_NAME) {
342312
Some(name) => name,
343313
None =>
344314
return syn::Error::new(
345315
proc_macro2::Span::call_site(),
346316
"`System` pallet declaration is missing. \
347-
Please add this line: `System: frame_system::{Pallet, Call, Storage, Config, Event<T>},`",
317+
Please add this line: `System: frame_system::{Pallet, Call, Storage, Config, Event<T>},`",
348318
)
349319
.into_compile_error(),
350320
};
351321

322+
let names_without_system =
323+
names.iter().filter(|n| **n != SYSTEM_PALLET_NAME).collect::<Vec<_>>();
324+
let names_reversed = names.clone().into_iter().rev().collect::<Vec<_>>();
325+
let names_without_system_reverse =
326+
names_without_system.clone().into_iter().rev().collect::<Vec<_>>();
327+
let names_reversed_with_system_first = std::iter::once(system_pallet)
328+
.chain(names_without_system_reverse.clone().into_iter())
329+
.collect::<Vec<_>>();
330+
352331
quote!(
353332
#types
354333

@@ -364,25 +343,28 @@ fn decl_all_pallets<'a>(
364343
pub type AllPallets = AllPalletsWithSystem;
365344

366345
/// All pallets included in the runtime as a nested tuple of types.
367-
pub type AllPalletsWithSystem = ( #all_pallets_with_system );
346+
pub type AllPalletsWithSystem = ( #(#names),* );
368347

369348
/// All pallets included in the runtime as a nested tuple of types.
370349
/// Excludes the System pallet.
371-
pub type AllPalletsWithoutSystem = ( #all_pallets_without_system );
350+
pub type AllPalletsWithoutSystem = ( #(#names_without_system),* );
372351

373352
/// All pallets included in the runtime as a nested tuple of types in reversed order.
374353
/// Excludes the System pallet.
375-
pub type AllPalletsWithoutSystemReversed = ( #all_pallets_without_system_reversed );
354+
#[deprecated(note = "Using reverse pallet orders is deprecated. use only \
355+
`AllPalletWithSystem or AllPalletsWithoutSystem`")]
356+
pub type AllPalletsWithoutSystemReversed =( #(#names_without_system_reverse),* );
376357

377358
/// All pallets included in the runtime as a nested tuple of types in reversed order.
378-
pub type AllPalletsWithSystemReversed = ( #all_pallets_with_system_reversed );
359+
#[deprecated(note = "Using reverse pallet orders is deprecated. use only \
360+
`AllPalletWithSystem or AllPalletsWithoutSystem`")]
361+
pub type AllPalletsWithSystemReversed = ( #(#names_reversed),* );
379362

380363
/// All pallets included in the runtime as a nested tuple of types in reversed order.
381364
/// With the system pallet first.
382-
pub type AllPalletsReversedWithSystemFirst = (
383-
#system_pallet,
384-
AllPalletsWithoutSystemReversed
385-
);
365+
#[deprecated(note = "Using reverse pallet orders is deprecated. use only \
366+
`AllPalletWithSystem or AllPalletsWithoutSystem`")]
367+
pub type AllPalletsReversedWithSystemFirst = ( #(#names_reversed_with_system_first),* );
386368
)
387369
}
388370

frame/support/procedural/src/pallet/expand/pallet_struct.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,17 +240,15 @@ pub fn expand_pallet_struct(def: &mut Def) -> proc_macro2::TokenStream {
240240
#config_where_clause
241241
{
242242
fn count() -> usize { 1 }
243-
fn accumulate(
244-
acc: &mut #frame_support::sp_std::vec::Vec<#frame_support::traits::PalletInfoData>
245-
) {
243+
fn infos() -> #frame_support::sp_std::vec::Vec<#frame_support::traits::PalletInfoData> {
246244
use #frame_support::traits::PalletInfoAccess;
247245
let item = #frame_support::traits::PalletInfoData {
248246
index: Self::index(),
249247
name: Self::name(),
250248
module_name: Self::module_name(),
251249
crate_version: Self::crate_version(),
252250
};
253-
acc.push(item);
251+
#frame_support::sp_std::vec![item]
254252
}
255253
}
256254

frame/support/src/dispatch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2207,15 +2207,15 @@ macro_rules! decl_module {
22072207
for $mod_type<$trait_instance $(, $instance)?> where $( $other_where_bounds )*
22082208
{
22092209
fn count() -> usize { 1 }
2210-
fn accumulate(acc: &mut $crate::sp_std::vec::Vec<$crate::traits::PalletInfoData>) {
2210+
fn infos() -> $crate::sp_std::vec::Vec<$crate::traits::PalletInfoData> {
22112211
use $crate::traits::PalletInfoAccess;
22122212
let item = $crate::traits::PalletInfoData {
22132213
index: Self::index(),
22142214
name: Self::name(),
22152215
module_name: Self::module_name(),
22162216
crate_version: Self::crate_version(),
22172217
};
2218-
acc.push(item);
2218+
vec![item]
22192219
}
22202220
}
22212221

frame/support/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616
// limitations under the License.
1717

1818
//! Support code for the runtime.
19+
//!
20+
//! ## Note on Tuple Traits
21+
//!
22+
//! Many of the traits defined in [`traits`] have auto-implementations on tuples as well. Usually,
23+
//! the tuple is a function of number of pallets in the runtime. By default, the traits are
24+
//! implemented for tuples of up to 64 items.
25+
//
26+
// If you have more pallets in your runtime, or for any other reason need more, enabled `tuples-96`
27+
// or the `tuples-128` complication flag. Note that these features *will increase* the compilation
28+
// of this crate.
1929

2030
#![cfg_attr(not(feature = "std"), no_std)]
2131

frame/support/src/migrations.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::{
1919
traits::{GetStorageVersion, PalletInfoAccess},
2020
weights::{RuntimeDbWeight, Weight},
2121
};
22+
use impl_trait_for_tuples::impl_for_tuples;
2223

2324
/// Trait used by [`migrate_from_pallet_version_to_storage_version`] to do the actual migration.
2425
pub trait PalletVersionToStorageVersionHelper {
@@ -42,7 +43,9 @@ impl<T: GetStorageVersion + PalletInfoAccess> PalletVersionToStorageVersionHelpe
4243
}
4344
}
4445

45-
#[impl_trait_for_tuples::impl_for_tuples(30)]
46+
#[cfg_attr(all(not(feature = "tuples-96"), not(feature = "tuples-128")), impl_for_tuples(64))]
47+
#[cfg_attr(all(feature = "tuples-96", not(feature = "tuples-128")), impl_for_tuples(96))]
48+
#[cfg_attr(feature = "tuples-128", impl_for_tuples(128))]
4649
impl PalletVersionToStorageVersionHelper for T {
4750
fn migrate(db_weight: &RuntimeDbWeight) -> Weight {
4851
let mut weight: Weight = 0;

frame/support/src/traits.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ mod error;
5050
pub use error::PalletError;
5151

5252
mod filter;
53-
pub use filter::{ClearFilterGuard, FilterStack, FilterStackGuard, InstanceFilter, IntegrityTest};
53+
pub use filter::{ClearFilterGuard, FilterStack, FilterStackGuard, InstanceFilter};
5454

5555
mod misc;
5656
pub use misc::{
@@ -81,7 +81,8 @@ mod hooks;
8181
#[cfg(feature = "std")]
8282
pub use hooks::GenesisBuild;
8383
pub use hooks::{
84-
Hooks, OnFinalize, OnGenesis, OnIdle, OnInitialize, OnRuntimeUpgrade, OnTimestampSet,
84+
Hooks, IntegrityTest, OnFinalize, OnGenesis, OnIdle, OnInitialize, OnRuntimeUpgrade,
85+
OnTimestampSet,
8586
};
8687
#[cfg(feature = "try-runtime")]
8788
pub use hooks::{OnRuntimeUpgradeHelpersExt, ON_RUNTIME_UPGRADE_PREFIX};

frame/support/src/traits/filter.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -180,17 +180,6 @@ macro_rules! impl_filter_stack {
180180
}
181181
}
182182

183-
/// Type that provide some integrity tests.
184-
///
185-
/// This implemented for modules by `decl_module`.
186-
#[impl_trait_for_tuples::impl_for_tuples(30)]
187-
pub trait IntegrityTest {
188-
/// Run integrity test.
189-
///
190-
/// The test is not executed in a externalities provided environment.
191-
fn integrity_test() {}
192-
}
193-
194183
#[cfg(test)]
195184
pub mod test_impl_filter_stack {
196185
use super::*;

frame/support/src/traits/hooks.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ pub trait OnInitialize<BlockNumber> {
3838
}
3939
}
4040

41-
#[impl_for_tuples(30)]
41+
#[cfg_attr(all(not(feature = "tuples-96"), not(feature = "tuples-128")), impl_for_tuples(64))]
42+
#[cfg_attr(all(feature = "tuples-96", not(feature = "tuples-128")), impl_for_tuples(96))]
43+
#[cfg_attr(feature = "tuples-128", impl_for_tuples(128))]
4244
impl<BlockNumber: Clone> OnInitialize<BlockNumber> for Tuple {
4345
fn on_initialize(n: BlockNumber) -> crate::weights::Weight {
4446
let mut weight = 0;
@@ -50,7 +52,9 @@ impl<BlockNumber: Clone> OnInitialize<BlockNumber> for Tuple {
5052
/// The block finalization trait.
5153
///
5254
/// Implementing this lets you express what should happen for your pallet when the block is ending.
53-
#[impl_for_tuples(30)]
55+
#[cfg_attr(all(not(feature = "tuples-96"), not(feature = "tuples-128")), impl_for_tuples(64))]
56+
#[cfg_attr(all(feature = "tuples-96", not(feature = "tuples-128")), impl_for_tuples(96))]
57+
#[cfg_attr(feature = "tuples-128", impl_for_tuples(128))]
5458
pub trait OnFinalize<BlockNumber> {
5559
/// The block is being finalized. Implement to have something happen.
5660
///
@@ -79,7 +83,9 @@ pub trait OnIdle<BlockNumber> {
7983
}
8084
}
8185

82-
#[impl_for_tuples(30)]
86+
#[cfg_attr(all(not(feature = "tuples-96"), not(feature = "tuples-128")), impl_for_tuples(64))]
87+
#[cfg_attr(all(feature = "tuples-96", not(feature = "tuples-128")), impl_for_tuples(96))]
88+
#[cfg_attr(feature = "tuples-128", impl_for_tuples(128))]
8389
impl<BlockNumber: Copy + AtLeast32BitUnsigned> OnIdle<BlockNumber> for Tuple {
8490
fn on_idle(n: BlockNumber, remaining_weight: crate::weights::Weight) -> crate::weights::Weight {
8591
let on_idle_functions: &[fn(
@@ -105,7 +111,9 @@ impl<BlockNumber: Copy + AtLeast32BitUnsigned> OnIdle<BlockNumber> for Tuple {
105111
/// Implementing this trait for a pallet let's you express operations that should
106112
/// happen at genesis. It will be called in an externalities provided environment and
107113
/// will see the genesis state after all pallets have written their genesis state.
108-
#[impl_for_tuples(30)]
114+
#[cfg_attr(all(not(feature = "tuples-96"), not(feature = "tuples-128")), impl_for_tuples(64))]
115+
#[cfg_attr(all(feature = "tuples-96", not(feature = "tuples-128")), impl_for_tuples(96))]
116+
#[cfg_attr(feature = "tuples-128", impl_for_tuples(128))]
109117
pub trait OnGenesis {
110118
/// Something that should happen at genesis.
111119
fn on_genesis() {}
@@ -187,7 +195,9 @@ pub trait OnRuntimeUpgrade {
187195
}
188196
}
189197

190-
#[impl_for_tuples(30)]
198+
#[cfg_attr(all(not(feature = "tuples-96"), not(feature = "tuples-128")), impl_for_tuples(64))]
199+
#[cfg_attr(all(feature = "tuples-96", not(feature = "tuples-128")), impl_for_tuples(96))]
200+
#[cfg_attr(feature = "tuples-128", impl_for_tuples(128))]
191201
impl OnRuntimeUpgrade for Tuple {
192202
fn on_runtime_upgrade() -> crate::weights::Weight {
193203
let mut weight = 0;
@@ -210,6 +220,19 @@ impl OnRuntimeUpgrade for Tuple {
210220
}
211221
}
212222

223+
/// Type that provide some integrity tests.
224+
///
225+
/// This implemented for modules by `decl_module`.
226+
#[cfg_attr(all(not(feature = "tuples-96"), not(feature = "tuples-128")), impl_for_tuples(64))]
227+
#[cfg_attr(all(feature = "tuples-96", not(feature = "tuples-128")), impl_for_tuples(96))]
228+
#[cfg_attr(feature = "tuples-128", impl_for_tuples(128))]
229+
pub trait IntegrityTest {
230+
/// Run integrity test.
231+
///
232+
/// The test is not executed in a externalities provided environment.
233+
fn integrity_test() {}
234+
}
235+
213236
/// The pallet hooks trait. Implementing this lets you express some logic to execute.
214237
pub trait Hooks<BlockNumber> {
215238
/// The block is being finalized. Implement to have something happen.
@@ -321,7 +344,9 @@ pub trait GenesisBuild<T, I = ()>: Default + sp_runtime::traits::MaybeSerializeD
321344
}
322345

323346
/// A trait which is called when the timestamp is set in the runtime.
324-
#[impl_for_tuples(30)]
347+
#[cfg_attr(all(not(feature = "tuples-96"), not(feature = "tuples-128")), impl_for_tuples(64))]
348+
#[cfg_attr(all(feature = "tuples-96", not(feature = "tuples-128")), impl_for_tuples(96))]
349+
#[cfg_attr(feature = "tuples-128", impl_for_tuples(128))]
325350
pub trait OnTimestampSet<Moment> {
326351
/// Called when the timestamp is set.
327352
fn on_timestamp_set(moment: Moment);

frame/support/src/traits/members.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
//! Traits for dealing with the idea of membership.
1919
20+
use impl_trait_for_tuples::impl_for_tuples;
2021
use sp_std::{marker::PhantomData, prelude::*};
2122

2223
/// A trait for querying whether a type can be said to "contain" a value.
@@ -25,7 +26,9 @@ pub trait Contains<T> {
2526
fn contains(t: &T) -> bool;
2627
}
2728

28-
#[impl_trait_for_tuples::impl_for_tuples(1, 30)]
29+
#[cfg_attr(all(not(feature = "tuples-96"), not(feature = "tuples-128")), impl_for_tuples(64))]
30+
#[cfg_attr(all(feature = "tuples-96", not(feature = "tuples-128")), impl_for_tuples(96))]
31+
#[cfg_attr(feature = "tuples-128", impl_for_tuples(128))]
2932
impl<T> Contains<T> for Tuple {
3033
fn contains(t: &T) -> bool {
3134
for_tuples!( #(
@@ -41,7 +44,9 @@ pub trait ContainsPair<A, B> {
4144
fn contains(a: &A, b: &B) -> bool;
4245
}
4346

44-
#[impl_trait_for_tuples::impl_for_tuples(0, 30)]
47+
#[cfg_attr(all(not(feature = "tuples-96"), not(feature = "tuples-128")), impl_for_tuples(64))]
48+
#[cfg_attr(all(feature = "tuples-96", not(feature = "tuples-128")), impl_for_tuples(96))]
49+
#[cfg_attr(feature = "tuples-128", impl_for_tuples(128))]
4550
impl<A, B> ContainsPair<A, B> for Tuple {
4651
fn contains(a: &A, b: &B) -> bool {
4752
for_tuples!( #(

0 commit comments

Comments
 (0)