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

Commit 7eb671f

Browse files
authored
Add an INDEX to the Instance trait (#8555)
* Add an index to the Instance trait * Update frame/support/procedural/src/storage/instance_trait.rs
1 parent a1574d3 commit 7eb671f

File tree

5 files changed

+23
-8
lines changed

5 files changed

+23
-8
lines changed

frame/support/procedural/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,3 +421,7 @@ pub fn require_transactional(attr: TokenStream, input: TokenStream) -> TokenStre
421421
pub fn crate_to_pallet_version(input: TokenStream) -> TokenStream {
422422
pallet_version::crate_to_pallet_version(input).unwrap_or_else(|e| e.to_compile_error()).into()
423423
}
424+
425+
/// The number of module instances supported by the runtime, starting at index 1,
426+
/// and up to `NUMBER_OF_INSTANCE`.
427+
pub(crate) const NUMBER_OF_INSTANCE: u8 = 16;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717

1818
use proc_macro2::Span;
1919
use crate::pallet::Def;
20+
use crate::NUMBER_OF_INSTANCE;
2021

2122
/// * Provide inherent instance to be used by construct_runtime
22-
/// * Provide Instance0 .. Instance16 for instantiable pallet
23+
/// * Provide Instance1 ..= Instance16 for instantiable pallet
2324
pub fn expand_instances(def: &mut Def) -> proc_macro2::TokenStream {
2425
let frame_support = &def.frame_support;
2526
let inherent_ident = syn::Ident::new(crate::INHERENT_INSTANCE_NAME, Span::call_site());
2627
let instances = if def.config.has_instance {
27-
(0..16).map(|i| syn::Ident::new(&format!("Instance{}", i), Span::call_site())).collect()
28+
(1..=NUMBER_OF_INSTANCE).map(|i| syn::Ident::new(&format!("Instance{}", i), Span::call_site())).collect()
2829
} else {
2930
vec![]
3031
};

frame/support/procedural/src/storage/instance_trait.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@
2121
use proc_macro2::{TokenStream, Span};
2222
use quote::quote;
2323
use super::DeclStorageDefExt;
24+
use crate::NUMBER_OF_INSTANCE;
2425

25-
const NUMBER_OF_INSTANCE: usize = 16;
2626
pub(crate) const INHERENT_INSTANCE_NAME: &str = "__InherentHiddenInstance";
2727

2828
// Used to generate an instance implementation.
2929
struct InstanceDef {
3030
prefix: String,
3131
instance_struct: syn::Ident,
3232
doc: TokenStream,
33+
// Index is same as instance number. Default is 0.
34+
index: u8,
3335
}
3436

3537
pub fn decl_and_impl(scrate: &TokenStream, def: &DeclStorageDefExt) -> TokenStream {
@@ -39,20 +41,22 @@ pub fn decl_and_impl(scrate: &TokenStream, def: &DeclStorageDefExt) -> TokenStre
3941

4042
// Implementation of instances.
4143
if let Some(module_instance) = &def.module_instance {
42-
let instance_defs = (0..NUMBER_OF_INSTANCE)
44+
let instance_defs = (1..=NUMBER_OF_INSTANCE)
4345
.map(|i| {
4446
let name = format!("Instance{}", i);
4547
InstanceDef {
4648
instance_struct: syn::Ident::new(&name, proc_macro2::Span::call_site()),
4749
prefix: name,
4850
doc: quote!(#[doc=r"Module instance"]),
51+
index: i,
4952
}
5053
})
5154
.chain(
5255
module_instance.instance_default.as_ref().map(|ident| InstanceDef {
5356
prefix: String::new(),
5457
instance_struct: ident.clone(),
5558
doc: quote!(#[doc=r"Default module instance"]),
59+
index: 0,
5660
})
5761
);
5862

@@ -83,6 +87,8 @@ pub fn decl_and_impl(scrate: &TokenStream, def: &DeclStorageDefExt) -> TokenStre
8387
/// instance.
8488
#[doc(hidden)]
8589
),
90+
// This is just to make the type system happy. Not actually used.
91+
index: 0,
8692
};
8793
impls.extend(create_and_impl_instance_struct(scrate, &instance_def, def));
8894
}
@@ -116,6 +122,7 @@ fn create_and_impl_instance_struct(
116122
let instance_struct = &instance_def.instance_struct;
117123
let prefix = format!("{}{}", instance_def.prefix, def.crate_name.to_string());
118124
let doc = &instance_def.doc;
125+
let index = instance_def.index;
119126

120127
quote! {
121128
// Those trait are derived because of wrong bounds for generics
@@ -129,6 +136,7 @@ fn create_and_impl_instance_struct(
129136
pub struct #instance_struct;
130137
impl #instance_trait for #instance_struct {
131138
const PREFIX: &'static str = #prefix;
139+
const INDEX: u8 = #index;
132140
}
133141
}
134142
}

frame/support/src/instances.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@
3131
//! NOTE: [`frame_support::pallet`] will reexport them inside the module, in order to make them
3232
//! accessible to [`frame_support::construct_runtime`].
3333
34-
/// Instance0 to be used for instantiable pallet define with `pallet` macro.
35-
#[derive(Clone, Copy, PartialEq, Eq, crate::RuntimeDebugNoBound)]
36-
pub struct Instance0;
37-
3834
/// Instance1 to be used for instantiable pallet define with `pallet` macro.
3935
#[derive(Clone, Copy, PartialEq, Eq, crate::RuntimeDebugNoBound)]
4036
pub struct Instance1;
@@ -94,3 +90,7 @@ pub struct Instance14;
9490
/// Instance15 to be used for instantiable pallet define with `pallet` macro.
9591
#[derive(Clone, Copy, PartialEq, Eq, crate::RuntimeDebugNoBound)]
9692
pub struct Instance15;
93+
94+
/// Instance16 to be used for instantiable pallet define with `pallet` macro.
95+
#[derive(Clone, Copy, PartialEq, Eq, crate::RuntimeDebugNoBound)]
96+
pub struct Instance16;

frame/support/src/traits/storage.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
pub trait Instance: 'static {
2727
/// Unique module prefix. E.g. "InstanceNMyModule" or "MyModule"
2828
const PREFIX: &'static str;
29+
/// Unique numerical identifier for an instance.
30+
const INDEX: u8;
2931
}
3032

3133
/// An instance of a storage in a pallet.

0 commit comments

Comments
 (0)