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

Commit

Permalink
pallet macro: easier syntax for #[pallet::pallet] with `struct Pall…
Browse files Browse the repository at this point in the history
…et<T>(_)` (#8091)
  • Loading branch information
gui1117 committed Feb 10, 2021
1 parent 28b950e commit 91a7418
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion bin/node-template/pallets/template/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub mod pallet {

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(PhantomData<T>);
pub struct Pallet<T>(_);

// The pallet's runtime storage items.
// https://substrate.dev/docs/en/knowledgebase/runtime/storage
Expand Down
2 changes: 1 addition & 1 deletion frame/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ pub mod pallet {

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::config]
/// The module configuration trait.
Expand Down
10 changes: 10 additions & 0 deletions frame/support/procedural/src/pallet/expand/pallet_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::pallet::Def;
/// * Implement OnGenesis on Pallet
/// * Implement ModuleErrorMetadata on Pallet
/// * declare Module type alias for construct_runtime
/// * replace the first field type of `struct Pallet` with `PhantomData` if it is `_`
pub fn expand_pallet_struct(def: &mut Def) -> proc_macro2::TokenStream {
let frame_support = &def.frame_support;
let frame_system = &def.frame_system;
Expand All @@ -41,6 +42,15 @@ pub fn expand_pallet_struct(def: &mut Def) -> proc_macro2::TokenStream {
}
};

// If the first field type is `_` then we replace with `PhantomData`
if let Some(field) = pallet_item.fields.iter_mut().next() {
if field.ty == syn::parse_quote!(_) {
field.ty = syn::parse_quote!(
#frame_support::sp_std::marker::PhantomData<(#type_use_gen)>
);
}
}

pallet_item.attrs.push(syn::parse_quote!(
#[derive(
#frame_support::CloneNoBound,
Expand Down
9 changes: 5 additions & 4 deletions frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,7 @@ pub mod pallet_prelude {
/// Item must be defined as followed:
/// ```ignore
/// #[pallet::pallet]
/// pub struct Pallet<T>(PhantomData<T>);
/// pub struct Pallet<T>(_);
/// ```
/// I.e. a regular struct definition named `Pallet`, with generic T and no where clause.
///
Expand All @@ -1150,7 +1150,7 @@ pub mod pallet_prelude {
/// ```ignore
/// #[pallet::pallet]
/// #[pallet::generate_store(pub(super) trait Store)]
/// pub struct Pallet<T>(PhantomData<T>);
/// pub struct Pallet<T>(_);
/// ```
/// More precisely the store trait contains an associated type for each storage. It is implemented
/// for `Pallet` allowing to access the storage from pallet struct.
Expand All @@ -1169,6 +1169,7 @@ pub mod pallet_prelude {
/// frame_support::RuntimeDebugNoBound,
/// )]
/// ```
/// and replace the type `_` by `PhantomData<T>`.
///
/// It implements on pallet:
/// * [`traits::GetPalletVersion`]
Expand Down Expand Up @@ -1602,7 +1603,7 @@ pub mod pallet_prelude {
/// // Define the pallet struct placeholder, various pallet function are implemented on it.
/// #[pallet::pallet]
/// #[pallet::generate_store(pub(super) trait Store)]
/// pub struct Pallet<T>(PhantomData<T>);
/// pub struct Pallet<T>(_);
///
/// // Implement the pallet hooks.
/// #[pallet::hooks]
Expand Down Expand Up @@ -1920,7 +1921,7 @@ pub mod pallet_prelude {
/// #[pallet::generate_store($visibility_of_trait_store trait Store)]
/// // NOTE: if the visibility of trait store is private but you want to make it available
/// // in super, then use `pub(super)` or `pub(crate)` to make it available in crate.
/// pub struct Pallet<T>(PhantomData<T>);
/// pub struct Pallet<T>(_);
/// // pub struct Pallet<T, I = ()>(PhantomData<T>); // for instantiable pallet
/// }
/// ```
Expand Down
4 changes: 2 additions & 2 deletions frame/support/test/tests/pallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub mod pallet {

#[pallet::pallet]
#[pallet::generate_store(pub(crate) trait Store)]
pub struct Pallet<T>(PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T>
Expand Down Expand Up @@ -290,7 +290,7 @@ pub mod pallet2 {

#[pallet::pallet]
#[pallet::generate_store(pub(crate) trait Store)]
pub struct Pallet<T>(PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T>
Expand Down
2 changes: 1 addition & 1 deletion frame/support/test/tests/pallet_compatibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub mod pallet {
}

#[pallet::pallet]
pub struct Pallet<T>(PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<T::BlockNumber> for Pallet<T> {
Expand Down
4 changes: 2 additions & 2 deletions frame/support/test/tests/pallet_ui/hooks_invalid_item.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#[frame_support::pallet]
mod pallet {
use frame_support::pallet_prelude::{Hooks, PhantomData};
use frame_support::pallet_prelude::Hooks;

#[pallet::config]
pub trait Config: frame_system::Config {}

#[pallet::pallet]
pub struct Pallet<T>(PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks for Pallet<T> {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#[frame_support::pallet]
mod pallet {
use frame_support::pallet_prelude::{Hooks, PhantomData};
use frame_support::pallet_prelude::Hooks;
use frame_system::pallet_prelude::BlockNumberFor;

#[pallet::config]
pub trait Config: frame_system::Config {}

#[pallet::pallet]
pub struct Pallet<T>(PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[frame_support::pallet]
mod pallet {
use frame_support::pallet_prelude::{Hooks, PhantomData};
use frame_support::pallet_prelude::Hooks;
use frame_system::pallet_prelude::BlockNumberFor;

#[pallet::config]
Expand All @@ -9,7 +9,7 @@ mod pallet {
{}

#[pallet::pallet]
pub struct Pallet<T>(PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod pallet {
pub trait Config: frame_system::Config {}

#[pallet::pallet]
pub struct Pallet<T>(PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
2 changes: 1 addition & 1 deletion frame/support/test/tests/pallet_ui/type_value_no_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod pallet {
pub trait Config: frame_system::Config {}

#[pallet::pallet]
pub struct Pallet<T>(PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
2 changes: 1 addition & 1 deletion frame/support/test/tests/pallet_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ mod pallet3 {
}

#[pallet::pallet]
pub struct Pallet<T>(PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
Expand Down
2 changes: 1 addition & 1 deletion frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ pub mod pallet {

#[pallet::pallet]
#[pallet::generate_store(pub (super) trait Store)]
pub struct Pallet<T>(PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
Expand Down

0 comments on commit 91a7418

Please sign in to comment.