Allow to name the generic for storages in #[pallet::storage] (#8751)

* implement named generic for storages

* fix error message on unexpected name for generic
This commit is contained in:
Guillaume Thiolliere
2021-05-19 09:11:11 +02:00
committed by GitHub
parent ed39290f91
commit 0b30049417
18 changed files with 746 additions and 136 deletions
+11 -12
View File
@@ -196,7 +196,7 @@ pub mod pallet {
StorageValue<_, <T::AccountId as SomeAssociation2>::_2>;
#[pallet::storage]
pub type Value<T> = StorageValue<_, u32>;
pub type Value<T> = StorageValue<Value = u32>;
#[pallet::type_value]
pub fn MyDefault<T: Config>() -> u16
@@ -211,14 +211,19 @@ pub mod pallet {
StorageMap<_, Blake2_128Concat, u8, u16, ValueQuery, MyDefault<T>>;
#[pallet::storage]
pub type Map2<T> = StorageMap<_, Twox64Concat, u16, u32, OptionQuery, GetDefault, ConstU32<3>>;
pub type Map2<T> = StorageMap<
Hasher = Twox64Concat, Key = u16, Value = u32, MaxValues = ConstU32<3>
>;
#[pallet::storage]
pub type DoubleMap<T> = StorageDoubleMap<_, Blake2_128Concat, u8, Twox64Concat, u16, u32>;
#[pallet::storage]
pub type DoubleMap2<T> = StorageDoubleMap<
_, Twox64Concat, u16, Blake2_128Concat, u32, u64, OptionQuery, GetDefault, ConstU32<5>,
Hasher1 = Twox64Concat, Key1 = u16,
Hasher2 = Blake2_128Concat, Key2 = u32,
Value = u64,
MaxValues = ConstU32<5>,
>;
#[pallet::storage]
@@ -228,15 +233,9 @@ pub mod pallet {
#[pallet::storage]
#[pallet::getter(fn nmap2)]
pub type NMap2<T> = StorageNMap<
_,
(
NMapKey<Twox64Concat, u16>,
NMapKey<Blake2_128Concat, u32>,
),
u64,
OptionQuery,
GetDefault,
ConstU32<11>,
Key = (NMapKey<Twox64Concat, u16>, NMapKey<Blake2_128Concat, u32>),
Value = u64,
MaxValues = ConstU32<11>,
>;
#[pallet::storage]
@@ -0,0 +1,25 @@
#[frame_support::pallet]
mod pallet {
use frame_support::pallet_prelude::{Hooks, StorageValue};
use frame_system::pallet_prelude::BlockNumberFor;
#[pallet::config]
pub trait Config: frame_system::Config {}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
#[pallet::call]
impl<T: Config> Pallet<T> {}
struct Bar;
#[pallet::storage]
type Foo<T> = StorageValue<Value = Bar>;
}
fn main() {
}
@@ -0,0 +1,33 @@
error[E0277]: the trait bound `Bar: WrapperTypeDecode` is not satisfied
--> $DIR/storage_ensure_span_are_ok_on_wrong_gen.rs:20:12
|
20 | #[pallet::storage]
| ^^^^^^^ the trait `WrapperTypeDecode` is not implemented for `Bar`
|
= note: required because of the requirements on the impl of `Decode` for `Bar`
= note: required because of the requirements on the impl of `FullCodec` for `Bar`
= note: required because of the requirements on the impl of `StorageValueMetadata` for `frame_support::pallet_prelude::StorageValue<_GeneratedPrefixForStorageFoo<T>, Bar>`
= note: required by `frame_support::storage::types::StorageValueMetadata::NAME`
error[E0277]: the trait bound `Bar: EncodeLike` is not satisfied
--> $DIR/storage_ensure_span_are_ok_on_wrong_gen.rs:20:12
|
20 | #[pallet::storage]
| ^^^^^^^ the trait `EncodeLike` is not implemented for `Bar`
|
= note: required because of the requirements on the impl of `FullEncode` for `Bar`
= note: required because of the requirements on the impl of `FullCodec` for `Bar`
= note: required because of the requirements on the impl of `StorageValueMetadata` for `frame_support::pallet_prelude::StorageValue<_GeneratedPrefixForStorageFoo<T>, Bar>`
= note: required by `frame_support::storage::types::StorageValueMetadata::NAME`
error[E0277]: the trait bound `Bar: WrapperTypeEncode` is not satisfied
--> $DIR/storage_ensure_span_are_ok_on_wrong_gen.rs:20:12
|
20 | #[pallet::storage]
| ^^^^^^^ the trait `WrapperTypeEncode` is not implemented for `Bar`
|
= note: required because of the requirements on the impl of `pallet::_::_parity_scale_codec::Encode` for `Bar`
= note: required because of the requirements on the impl of `FullEncode` for `Bar`
= note: required because of the requirements on the impl of `FullCodec` for `Bar`
= note: required because of the requirements on the impl of `StorageValueMetadata` for `frame_support::pallet_prelude::StorageValue<_GeneratedPrefixForStorageFoo<T>, Bar>`
= note: required by `frame_support::storage::types::StorageValueMetadata::NAME`
@@ -0,0 +1,25 @@
#[frame_support::pallet]
mod pallet {
use frame_support::pallet_prelude::{Hooks, StorageValue};
use frame_system::pallet_prelude::BlockNumberFor;
#[pallet::config]
pub trait Config: frame_system::Config {}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
#[pallet::call]
impl<T: Config> Pallet<T> {}
struct Bar;
#[pallet::storage]
type Foo<T> = StorageValue<_, Bar>;
}
fn main() {
}
@@ -0,0 +1,33 @@
error[E0277]: the trait bound `Bar: WrapperTypeDecode` is not satisfied
--> $DIR/storage_ensure_span_are_ok_on_wrong_gen_unnamed.rs:20:12
|
20 | #[pallet::storage]
| ^^^^^^^ the trait `WrapperTypeDecode` is not implemented for `Bar`
|
= note: required because of the requirements on the impl of `Decode` for `Bar`
= note: required because of the requirements on the impl of `FullCodec` for `Bar`
= note: required because of the requirements on the impl of `StorageValueMetadata` for `frame_support::pallet_prelude::StorageValue<_GeneratedPrefixForStorageFoo<T>, Bar>`
= note: required by `frame_support::storage::types::StorageValueMetadata::NAME`
error[E0277]: the trait bound `Bar: EncodeLike` is not satisfied
--> $DIR/storage_ensure_span_are_ok_on_wrong_gen_unnamed.rs:20:12
|
20 | #[pallet::storage]
| ^^^^^^^ the trait `EncodeLike` is not implemented for `Bar`
|
= note: required because of the requirements on the impl of `FullEncode` for `Bar`
= note: required because of the requirements on the impl of `FullCodec` for `Bar`
= note: required because of the requirements on the impl of `StorageValueMetadata` for `frame_support::pallet_prelude::StorageValue<_GeneratedPrefixForStorageFoo<T>, Bar>`
= note: required by `frame_support::storage::types::StorageValueMetadata::NAME`
error[E0277]: the trait bound `Bar: WrapperTypeEncode` is not satisfied
--> $DIR/storage_ensure_span_are_ok_on_wrong_gen_unnamed.rs:20:12
|
20 | #[pallet::storage]
| ^^^^^^^ the trait `WrapperTypeEncode` is not implemented for `Bar`
|
= note: required because of the requirements on the impl of `pallet::_::_parity_scale_codec::Encode` for `Bar`
= note: required because of the requirements on the impl of `FullEncode` for `Bar`
= note: required because of the requirements on the impl of `FullCodec` for `Bar`
= note: required because of the requirements on the impl of `StorageValueMetadata` for `frame_support::pallet_prelude::StorageValue<_GeneratedPrefixForStorageFoo<T>, Bar>`
= note: required by `frame_support::storage::types::StorageValueMetadata::NAME`
@@ -1,4 +1,4 @@
error: Invalid use of `#[pallet::storage]`, the type first generic argument must be `_`, the final argument is automatically set by macro.
error: Invalid pallet::storage, for unnamed generic arguments the type first generic argument must be `_`, the argument is then replaced by macro.
--> $DIR/storage_invalid_first_generic.rs:19:29
|
19 | type Foo<T> = StorageValue<u8, u8>;
@@ -1,4 +1,4 @@
error: Invalid pallet::storage, expected ident: `StorageValue` or `StorageMap` or `StorageDoubleMap` or `StorageNMap` in order to expand metadata, found `u8`
error: Invalid pallet::storage, expected ident: `StorageValue` or `StorageMap` or `StorageDoubleMap` or `StorageNMap` in order to expand metadata, found `u8`.
--> $DIR/storage_not_storage_type.rs:19:16
|
19 | type Foo<T> = u8;
@@ -0,0 +1,23 @@
#[frame_support::pallet]
mod pallet {
use frame_support::pallet_prelude::{Hooks, StorageValue};
use frame_system::pallet_prelude::BlockNumberFor;
#[pallet::config]
pub trait Config: frame_system::Config {}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
#[pallet::call]
impl<T: Config> Pallet<T> {}
#[pallet::storage]
type Foo<T> = StorageValue<Value = u32, Value = u32>;
}
fn main() {
}
@@ -0,0 +1,11 @@
error: Invalid pallet::storage, Duplicated named generic
--> $DIR/storage_value_duplicate_named_generic.rs:19:42
|
19 | type Foo<T> = StorageValue<Value = u32, Value = u32>;
| ^^^^^
error: Invalid pallet::storage, Duplicated named generic
--> $DIR/storage_value_duplicate_named_generic.rs:19:29
|
19 | type Foo<T> = StorageValue<Value = u32, Value = u32>;
| ^^^^^
@@ -0,0 +1,23 @@
#[frame_support::pallet]
mod pallet {
use frame_support::pallet_prelude::{Hooks, StorageValue, OptionQuery};
use frame_system::pallet_prelude::BlockNumberFor;
#[pallet::config]
pub trait Config: frame_system::Config {}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
#[pallet::call]
impl<T: Config> Pallet<T> {}
#[pallet::storage]
type Foo<T> = StorageValue<u32, QueryKind = OptionQuery>;
}
fn main() {
}
@@ -0,0 +1,5 @@
error: Invalid pallet::storage, invalid generic declaration for storage. Expect only type generics or binding generics, e.g. `<Name1 = Gen1, Name2 = Gen2, ..>` or `<Gen1, Gen2, ..>`.
--> $DIR/storage_value_generic_named_and_unnamed.rs:19:16
|
19 | type Foo<T> = StorageValue<u32, QueryKind = OptionQuery>;
| ^^^^^^^^^^^^
@@ -1,4 +1,4 @@
error: pallet::storage unexpected number of generic argument, expected at least 2 args, found none
error: Invalid pallet::storage, invalid number of generic generic arguments, expect more that 0 generic arguments.
--> $DIR/storage_value_no_generic.rs:19:16
|
19 | type Foo<T> = StorageValue;
@@ -0,0 +1,23 @@
#[frame_support::pallet]
mod pallet {
use frame_support::pallet_prelude::{Hooks, StorageValue};
use frame_system::pallet_prelude::BlockNumberFor;
#[pallet::config]
pub trait Config: frame_system::Config {}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
#[pallet::call]
impl<T: Config> Pallet<T> {}
#[pallet::storage]
type Foo<T> = StorageValue<P = u32>;
}
fn main() {
}
@@ -0,0 +1,11 @@
error: Invalid pallet::storage, Unexpected generic `P` for `StorageValue`. `StorageValue` expect generics `Value`, and optional generics `QueryKind`, `OnEmpty`.
--> $DIR/storage_value_unexpected_named_generic.rs:19:29
|
19 | type Foo<T> = StorageValue<P = u32>;
| ^
error: Invalid pallet::storage, cannot find `Value` generic, required for `StorageValue`.
--> $DIR/storage_value_unexpected_named_generic.rs:19:28
|
19 | type Foo<T> = StorageValue<P = u32>;
| ^
@@ -1,4 +1,4 @@
error: Invalid pallet::storage, expected item type
error: Invalid pallet::storage, expect item type.
--> $DIR/storage_wrong_item.rs:19:2
|
19 | impl Foo {}