Switch srml-session keys to decl_storage! (#3184)

* Switch `srml-session` keys to `decl_storage!`

* Expose `DEDUP_KEY_PREFIX` in constants

* Fix test

* Bump spec version
This commit is contained in:
Bastian Köcher
2019-07-24 10:40:04 +02:00
committed by Gavin Wood
parent c8dab27f35
commit 5d58d583e3
14 changed files with 365 additions and 201 deletions
+1 -1
View File
@@ -18,7 +18,7 @@
//! Proc macro of Support code for the runtime.
// end::description[]
#![recursion_limit="256"]
#![recursion_limit="512"]
extern crate proc_macro;
@@ -694,13 +694,18 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
let mutate_impl = if !is_option {
quote!{
#as_double_map::insert(key1, key2, &val, storage)
#as_double_map::insert(k1, k2, &val, storage)
}
} else {
quote!{
match val {
Some(ref val) => #as_double_map::insert(key1, key2, &val, storage),
None => #as_double_map::remove(key1, key2, storage),
Some(ref val) => #as_double_map::insert::<KArg1, KArg2, #typ, S>(
k1,
k2,
val,
storage,
),
None => #as_double_map::remove(k1, k2, storage),
}
}
};
@@ -751,7 +756,10 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
{
type Query = #value_type;
fn prefix_for(k1: &#k1ty) -> Vec<u8> {
fn prefix_for<KArg1>(k1: &KArg1) -> #scrate::rstd::vec::Vec<u8> where
KArg1: ?Sized + #scrate::codec::Encode,
#k1ty: #scrate::rstd::borrow::Borrow<KArg1>,
{
use #scrate::storage::hashed::generator::StorageHasher;
let mut key = #as_double_map::prefix().to_vec();
@@ -763,7 +771,15 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
#final_prefix
}
fn key_for(k1: &#k1ty, k2: &#k2ty) -> Vec<u8> {
fn key_for<KArg1, KArg2>(
k1: &KArg1,
k2: &KArg2,
) -> #scrate::rstd::vec::Vec<u8> where
#k1ty: #scrate::rstd::borrow::Borrow<KArg1>,
#k2ty: #scrate::rstd::borrow::Borrow<KArg2>,
KArg1: ?Sized + #scrate::codec::Encode,
KArg2: ?Sized + #scrate::codec::Encode,
{
use #scrate::storage::hashed::generator::StorageHasher;
let mut key = #as_double_map::prefix_for(k1);
@@ -771,25 +787,50 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
key
}
fn get<S: #scrate::UnhashedStorage>(key1: &#k1ty, key2: &#k2ty, storage: &S) -> Self::Query {
let key = #as_double_map::key_for(key1, key2);
fn get<KArg1, KArg2, S: #scrate::UnhashedStorage>(
k1: &KArg1,
k2: &KArg2,
storage: &S,
) -> Self::Query where
#k1ty: #scrate::rstd::borrow::Borrow<KArg1>,
#k2ty: #scrate::rstd::borrow::Borrow<KArg2>,
KArg1: ?Sized + #scrate::codec::Encode,
KArg2: ?Sized + #scrate::codec::Encode,
{
let key = #as_double_map::key_for(k1, k2);
storage.get(&key).#option_simple_1(|| #fielddefault)
}
fn take<S: #scrate::UnhashedStorage>(key1: &#k1ty, key2: &#k2ty, storage: &mut S) -> Self::Query {
let key = #as_double_map::key_for(key1, key2);
fn take<KArg1, KArg2, S: #scrate::UnhashedStorage>(
k1: &KArg1,
k2: &KArg2,
storage: &mut S,
) -> Self::Query where
#k1ty: #scrate::rstd::borrow::Borrow<KArg1>,
#k2ty: #scrate::rstd::borrow::Borrow<KArg2>,
KArg1: ?Sized + #scrate::codec::Encode,
KArg2: ?Sized + #scrate::codec::Encode,
{
let key = #as_double_map::key_for(k1, k2);
storage.take(&key).#option_simple_1(|| #fielddefault)
}
fn mutate<R, F, S>(key1: &#k1ty, key2: &#k2ty, f: F, storage: &mut S) -> R
where
fn mutate<KArg1, KArg2, R, F, S: #scrate::UnhashedStorage>(
k1: &KArg1,
k2: &KArg2,
f: F,
storage: &mut S,
) -> R where
#k1ty: #scrate::rstd::borrow::Borrow<KArg1>,
#k2ty: #scrate::rstd::borrow::Borrow<KArg2>,
KArg1: ?Sized + #scrate::codec::Encode,
KArg2: ?Sized + #scrate::codec::Encode,
F: FnOnce(&mut Self::Query) -> R,
S: #scrate::UnhashedStorage,
{
let mut val = #as_double_map::get(key1, key2, storage);
let mut val = #as_double_map::get(k1, k2, storage);
let ret = f(&mut val);
#mutate_impl ;
#mutate_impl;
ret
}
}
@@ -905,15 +905,17 @@ fn impl_store_fns(
};
quote!{
pub fn #get_fn<KArg1, KArg2>(k1: KArg1, k2: KArg2) -> #value_type
pub fn #get_fn<KArg1, KArg2>(k1: &KArg1, k2: &KArg2) -> #value_type
where
KArg1: #scrate::rstd::borrow::Borrow<#key1_type>,
KArg2: #scrate::rstd::borrow::Borrow<#key2_type>,
#key1_type: #scrate::rstd::borrow::Borrow<KArg1>,
#key2_type: #scrate::rstd::borrow::Borrow<KArg2>,
KArg1: ?Sized + #scrate::codec::Encode,
KArg2: ?Sized + #scrate::codec::Encode,
{
<
#name<#struct_trait #instance> as
#scrate::storage::unhashed::generator::StorageDoubleMap<#key1_type, #key2_type, #typ>
>::get(k1.borrow(), k2.borrow(), &#scrate::storage::RuntimeStorage)
>::get(k1, k2, &#scrate::storage::RuntimeStorage)
}
}
}