Use optimized append and len storage methods in SRML. (#3071)

* expose len from codec to storage.

* refactor runtime with len and append.

* Undo example.

* Remove imports.

* Bump codec.

* Optionify.

* Make decode_len counscious.

* Refactor.

* Update srml/support/src/storage/hashed/generator.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Update srml/support/src/storage/hashed/generator.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Update srml/support/src/storage/hashed/generator.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Update srml/support/src/storage/hashed/generator.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Fix merge.

* fix some docs.

* Add NoDefault trait.

* Bump.

* Final nits.

* Update srml/support/src/traits.rs

* new approach toward len.

* re-create lock file.

* Fix build errors and Option handling.

* More test fix

* Use default for append as well.

* Fix runtime.

* Add support for linked_map

* More tweaks from review.

* Fix style

* Change api for none-values

* Bump.
This commit is contained in:
Kian Paimani
2019-08-27 19:39:05 +02:00
committed by Bastian Köcher
parent 99a7492dbf
commit 095c7de7ff
13 changed files with 573 additions and 179 deletions
@@ -43,6 +43,8 @@ pub(crate) struct Impls<'a, I: Iterator<Item=syn::Meta>> {
pub instance_opts: &'a InstanceOpts,
pub type_infos: DeclStorageTypeInfos<'a>,
pub fielddefault: TokenStream2,
pub default_delegator_ident: syn::Ident,
pub default_delegator_return: TokenStream2,
pub prefix: String,
pub cratename: &'a syn::Ident,
pub name: &'a syn::Ident,
@@ -60,6 +62,8 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
instance_opts,
type_infos,
fielddefault,
default_delegator_ident,
default_delegator_return,
prefix,
name,
attrs,
@@ -116,6 +120,17 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
// generator for value
quote! {
#visibility struct #default_delegator_ident<#struct_trait>(
#scrate::rstd::marker::PhantomData<(#trait_and_instance)>
) #where_clause;
impl<#impl_trait> #scrate::traits::StorageDefault<#typ>
for #default_delegator_ident<#trait_and_instance> #where_clause
{
fn default() -> Option<#typ> {
#default_delegator_return
}
}
#( #[ #attrs ] )*
#visibility struct #name<#struct_trait>(
#scrate::rstd::marker::PhantomData<(#trait_and_instance)>
@@ -125,6 +140,7 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
for #name<#trait_and_instance> #where_clause
{
type Query = #value_type;
type Default = #default_delegator_ident<#trait_and_instance>;
/// Get the storage key.
fn key() -> &'static [u8] {
@@ -168,6 +184,8 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
instance_opts,
type_infos,
fielddefault,
default_delegator_ident,
default_delegator_return,
prefix,
name,
attrs,
@@ -230,6 +248,17 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
// generator for map
quote!{
#visibility struct #default_delegator_ident<#struct_trait>(
#scrate::rstd::marker::PhantomData<(#trait_and_instance)>
) #where_clause;
impl<#impl_trait> #scrate::traits::StorageDefault<#typ>
for #default_delegator_ident<#trait_and_instance> #where_clause
{
fn default() -> Option<#typ> {
#default_delegator_return
}
}
#( #[ #attrs ] )*
#visibility struct #name<#struct_trait>(
#scrate::rstd::marker::PhantomData<(#trait_and_instance)>
@@ -239,8 +268,8 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
for #name<#trait_and_instance> #where_clause
{
type Query = #value_type;
type Hasher = #scrate::#hasher;
type Default = #default_delegator_ident<#trait_and_instance>;
/// Get the prefix key in storage.
fn prefix() -> &'static [u8] {
@@ -283,6 +312,10 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
impl<#impl_trait> #scrate::storage::hashed::generator::AppendableStorageMap<#kty, #typ>
for #name<#trait_and_instance> #where_clause
{}
impl<#impl_trait> #scrate::storage::hashed::generator::DecodeLengthStorageMap<#kty, #typ>
for #name<#trait_and_instance> #where_clause
{}
}
}
@@ -295,6 +328,8 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
instance_opts,
type_infos,
fielddefault,
default_delegator_ident,
default_delegator_return,
prefix,
name,
attrs,
@@ -567,12 +602,23 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
#structure
#visibility struct #default_delegator_ident<#struct_trait>(
#scrate::rstd::marker::PhantomData<(#trait_and_instance)>
) #where_clause;
impl<#impl_trait> #scrate::traits::StorageDefault<#typ>
for #default_delegator_ident<#trait_and_instance> #where_clause
{
fn default() -> Option<#typ> {
#default_delegator_return
}
}
impl<#impl_trait> #scrate::storage::hashed::generator::StorageMap<#kty, #typ>
for #name<#trait_and_instance> #where_clause
{
type Query = #value_type;
type Hasher = #scrate::#hasher;
type Default = #default_delegator_ident<#trait_and_instance>;
/// Get the prefix key in storage.
fn prefix() -> &'static [u8] {
@@ -730,6 +776,10 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
})
}
}
impl<#impl_trait> #scrate::storage::hashed::generator::DecodeLengthStorageMap<#kty, #typ>
for #name<#trait_and_instance> #where_clause
{}
}
}
@@ -762,10 +762,26 @@ fn decl_storage_items(
} = sline;
let type_infos = get_type_infos(storage_type);
let fielddefault = default_value.inner
.as_ref()
.map(|d| &d.expr)
.map(|d| quote!( #d ))
.unwrap_or_else(|| quote!{ Default::default() });
let kind = type_infos.kind.clone();
// Propagate doc attributes.
let attrs = attrs.inner.iter().filter_map(|a| a.parse_meta().ok()).filter(|m| m.name() == "doc");
// create default value delegator
let default_delegator_ident = Ident::new(
&format!("{}{}", name.to_string(), "DefaultDelegator"),
proc_macro2::Span::call_site(),
);
let default_delegator_return = if !type_infos.is_option {
quote! { Some(#fielddefault) }
} else {
quote! { #fielddefault }
};
let i = impls::Impls {
scrate,
visibility,
@@ -774,8 +790,9 @@ fn decl_storage_items(
traittype,
instance_opts,
type_infos,
fielddefault: default_value.inner.as_ref().map(|d| &d.expr).map(|d| quote!( #d ))
.unwrap_or_else(|| quote!{ Default::default() }),
fielddefault,
default_delegator_ident,
default_delegator_return,
prefix: build_prefix(cratename, name),
name,
attrs,