pallet macro: easier syntax for #[pallet::pallet] with struct Pallet<T>(_) (#8091)

This commit is contained in:
Guillaume Thiolliere
2021-02-10 10:27:05 +01:00
committed by GitHub
parent 2d31af3eb6
commit e78d139676
13 changed files with 30 additions and 19 deletions
@@ -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;
@@ -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,