Support NMap in generate_storage_alias (#9147)

* Support NMap in generate_storage_alias

* Verify that 2-key NMap is identical to DoubleMap

* Also compare key hashes and make sure they're identical

* Fix and add tests for 1-tuple NMap generated by generate_storage_alias
This commit is contained in:
Keith Yeung
2021-06-28 02:17:28 -07:00
committed by GitHub
parent 0cccd282a1
commit fb1bd5735c
4 changed files with 69 additions and 10 deletions
+30 -1
View File
@@ -100,7 +100,8 @@ impl TypeId for PalletId {
}
/// Generate a new type alias for [`storage::types::StorageValue`],
/// [`storage::types::StorageMap`] and [`storage::types::StorageDoubleMap`].
/// [`storage::types::StorageMap`], [`storage::types::StorageDoubleMap`]
/// and [`storage::types::StorageNMap`].
///
/// Useful for creating a *storage-like* struct for test and migrations.
///
@@ -154,6 +155,18 @@ macro_rules! generate_storage_alias {
>;
}
};
($pallet:ident, $name:ident => NMap<$(($key:ty, $hasher:ty),)+ $value:ty>) => {
$crate::paste::paste! {
$crate::generate_storage_alias!(@GENERATE_INSTANCE_STRUCT $pallet, $name);
type $name = $crate::storage::types::StorageNMap<
[<$name Instance>],
(
$( $crate::storage::types::Key<$hasher, $key>, )+
),
$value,
>;
}
};
($pallet:ident, $name:ident => Value<$value:ty>) => {
$crate::paste::paste! {
$crate::generate_storage_alias!(@GENERATE_INSTANCE_STRUCT $pallet, $name);
@@ -193,6 +206,22 @@ macro_rules! generate_storage_alias {
>;
}
};
(
$pallet:ident,
$name:ident<$t:ident : $bounds:tt> => NMap<$(($key:ty, $hasher:ty),)+ $value:ty>
) => {
$crate::paste::paste! {
$crate::generate_storage_alias!(@GENERATE_INSTANCE_STRUCT $pallet, $name);
#[allow(type_alias_bounds)]
type $name<$t : $bounds> = $crate::storage::types::StorageNMap<
[<$name Instance>],
(
$( $crate::storage::types::Key<$hasher, $key>, )+
),
$value,
>;
}
};
($pallet:ident, $name:ident<$t:ident : $bounds:tt> => Value<$value:ty>) => {
$crate::paste::paste! {
$crate::generate_storage_alias!(@GENERATE_INSTANCE_STRUCT $pallet, $name);