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

Commit

Permalink
add allow type alias bounds automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
gui1117 committed Sep 2, 2020
1 parent b3857a3 commit fae5be6
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 41 deletions.
3 changes: 3 additions & 0 deletions frame/support/procedural/src/pallet/expand/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub fn expand_storages(def: &mut Def) -> proc_macro2::TokenStream {
.collect::<Vec<_>>();

// Replace first arg `_` by the generated prefix structure.
// Add `#[allow(type_alias_bounds)]`
for (i, def_storage) in def.storages.iter_mut().enumerate() {
let item = &mut def.item.content.as_mut().expect("Checked by def").1[def_storage.index];

Expand All @@ -48,6 +49,8 @@ pub fn expand_storages(def: &mut Def) -> proc_macro2::TokenStream {
unreachable!("Checked by def");
};

typ_item.attrs.push(syn::parse_quote!(#[allow(type_alias_bounds)]));

let typ_path = if let syn::Type::Path(p) = &mut *typ_item.ty {
p
} else {
Expand Down
4 changes: 2 additions & 2 deletions frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1225,7 +1225,7 @@ pub mod pallet_prelude {
/// //
/// // NOTE: for storage hasher, the type is not copied because storage hasher trait already
/// // implements metadata. Thus generic storage hasher is supported.
/// #[pallet::storage] #[allow(type_alias_bounds)]
/// #[pallet::storage]
/// type MyStorageValue<T: Trait> = StorageValueType<_, T::Balance, ValueQuery>;
///
/// // Another declaration
Expand Down Expand Up @@ -1351,7 +1351,7 @@ pub mod pallet_prelude {
/// Something(u32),
/// }
///
/// #[pallet::storage] #[allow(type_alias_bounds)]
/// #[pallet::storage]
/// type MyStorageValue<T: Trait<I>, I: Instance = DefaultInstance> =
/// StorageValueType<_, T::Balance, ValueQuery>;
///
Expand Down
34 changes: 3 additions & 31 deletions frame/support/test/tests/pallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,52 +95,26 @@ pub mod pallet {
Something(u32),
}

// Declare a storage, any amount of storage can be declared.
//
// Is expected either `StorageValueType`, `StorageMapType` or `StorageDoubleMapType`.
// The macro generates for struct `$identP` (for storage of name `$ident`) and implement
// storage instance on it.
// The macro macro expand the metadata for the storage with the type used:
// * For storage value the type for value will be copied into metadata
// * For storage map the type for value and the type for key will be copied into metadata
// * For storage double map the type for value, key1, and key2 will be copied into
// metadata.
//
// NOTE: for storage hasher, the type is not copied because storage hasher trait already
// implements metadata. Thus generic storage hasher is supported.
#[pallet::storage] #[allow(type_alias_bounds)]
type MyStorageValue<T: Trait> = StorageValueType<MyStorageValueP, T::Balance, ValueQuery>;
#[pallet::storage]
type MyStorageValue<T: Trait> = StorageValueType<_, T::Balance, ValueQuery>;

// Another declaration
#[pallet::storage]
type MyStorage = StorageMapType<MyStorageP, Blake2_128Concat, u32, u32>;
type MyStorage = StorageMapType<_, Blake2_128Concat, u32, u32>;

// Declare genesis config. (This is optional)
//
// The macro accept either type alias or struct or enum, it checks generics are consistent.
//
// Type must implement `Default` traits
#[pallet::genesis_config]
#[derive(Default)]
pub struct GenesisConfig {
_myfield: u32,
}

// Declare genesis builder. (This is need only if GenesisConfig is declared)
#[pallet::genesis_build]
impl<T: Trait> GenesisBuilder<T> for GenesisConfig {
fn build(&self) {}
}

// Declare a pallet origin. (this is optional)
//
// The macro accept type alias or struct or enum, it checks generics are consistent.
#[pallet::origin]
pub struct Origin<T>(PhantomData<T>);

// Declare inherent provider for module. (this is optional)
//
// The macro checks module is `Module<T>` or `Module<T, I>` and trait is `ProvideInherent`
#[pallet::inherent]
impl<T: Trait> ProvideInherent for Module<T> {
type Call = Call<T>;
Expand All @@ -153,8 +127,6 @@ pub mod pallet {
}
}

// Regular rust code needed for implementing ProvideInherent trait

#[derive(codec::Encode, sp_runtime::RuntimeDebug)]
#[cfg_attr(feature = "std", derive(codec::Decode))]
pub enum InherentError {
Expand Down
8 changes: 4 additions & 4 deletions frame/support/test/tests/pallet_compatibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,22 +120,22 @@ pub mod pallet {
Dummy(T::Balance),
}

#[pallet::storage] #[allow(type_alias_bounds)]
#[pallet::storage]
/// Some documentation
type Dummy<T: Trait> = StorageValueType<_, T::Balance, OptionQuery>;

#[pallet::storage] #[allow(type_alias_bounds)]
#[pallet::storage]
type Bar<T: Trait> = StorageMapType<_, Blake2_128Concat, T::AccountId, T::Balance, ValueQuery>;

#[pallet::storage] #[allow(type_alias_bounds)]
#[pallet::storage]
type Foo<T: Trait> = StorageValueType<_, T::Balance, ValueQuery, OnFooEmpty<T>>;
pub struct OnFooEmpty<T: Trait>(PhantomData<T>); // TODO TODO: allow faster declaration with parameter_types
impl<T: Trait> Get<T::Balance> for OnFooEmpty<T> { fn get() -> T::Balance { 3.into() } }
// #[pallet::type_value] pub struct BalanceDefault: Balance = 0;
// #[pallet::type_value] pub fn BalanceDefault<T: Trait>() -> T::Balance { 0.into() }
// #[pallet::type_value] pub struct BalanceDefault<T: Trait>(fn() -> T::Balance { 0.into() })

#[pallet::storage] #[allow(type_alias_bounds)]
#[pallet::storage]
type Double = StorageDoubleMapType<
_, Blake2_128Concat, u32, Twox64Concat, u64, u16, ValueQuery
>;
Expand Down
8 changes: 4 additions & 4 deletions frame/support/test/tests/pallet_compatibility_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,21 @@ pub mod pallet {
Dummy(T::Balance),
}

#[pallet::storage] #[allow(type_alias_bounds)]
#[pallet::storage]
/// Some documentation
type Dummy<T: Trait<I>, I: Instance = DefaultInstance> = StorageValueType<_, T::Balance, OptionQuery>;

#[pallet::storage] #[allow(type_alias_bounds)]
#[pallet::storage]
type Bar<T: Trait<I>, I: Instance = DefaultInstance> =
StorageMapType<_, Blake2_128Concat, T::AccountId, T::Balance, ValueQuery>;

#[pallet::storage] #[allow(type_alias_bounds)]
#[pallet::storage]
type Foo<T: Trait<I>, I: Instance = DefaultInstance> =
StorageValueType<_, T::Balance, ValueQuery, OnFooEmpty<T, I>>;
pub struct OnFooEmpty<T: Trait<I>, I: Instance>(PhantomData<(T, I)>);
impl<T: Trait<I>, I: Instance> Get<T::Balance> for OnFooEmpty<T, I> { fn get() -> T::Balance { 3.into() } }

#[pallet::storage] #[allow(type_alias_bounds)]
#[pallet::storage]
type Double<I: Instance = DefaultInstance> = StorageDoubleMapType<
_, Blake2_128Concat, u32, Twox64Concat, u64, u16, ValueQuery
>;
Expand Down

0 comments on commit fae5be6

Please sign in to comment.