Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions srml/support/procedural/src/storage/instance_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub(crate) const DEFAULT_INSTANTIABLE_TRAIT_NAME: &str = "__GeneratedInstantiabl
// prefix for consts in trait Instance
pub(crate) const PREFIX_FOR: &str = "PREFIX_FOR_";
pub(crate) const HEAD_KEY_FOR: &str = "HEAD_KEY_FOR_";
pub(crate) const LINKED_MAP_KEY_FORMAT_FOR: &str = "LINKED_MAP_KEY_FORMAT_FOR_";

// Used to generate the const:
// `const $name: &'static str = $value_prefix ++ instance_prefix ++ $value_suffix`
Expand Down
40 changes: 35 additions & 5 deletions srml/support/procedural/src/storage/storage_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use proc_macro2::TokenStream;
use quote::quote;
use super::{
DeclStorageDefExt, StorageLineTypeDef,
instance_trait::{PREFIX_FOR, HEAD_KEY_FOR},
instance_trait::{PREFIX_FOR, HEAD_KEY_FOR, LINKED_MAP_KEY_FORMAT_FOR},
};

fn from_optional_value_to_query(is_option: bool, default: &Option<syn::Expr>) -> TokenStream {
Expand Down Expand Up @@ -156,21 +156,51 @@ pub fn decl_and_impl(scrate: &TokenStream, def: &DeclStorageDefExt) -> TokenStre
quote!( #prefix.as_bytes() )
};

let (key_format_type, key_format_decl) = {
let name = syn::Ident::new(
&format!("{}{}", LINKED_MAP_KEY_FORMAT_FOR, line.name),
proc_macro2::Span::call_site(),
);

let format_type = quote!( #name<#optional_instance>);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let format_type = quote!( #name<#optional_instance>);
let format_type = quote!( #name<#optional_instance> );


let optional_phantom = optional_instance
.as_ref()
.map(|instance| quote!( #scrate::rstd::marker::PhantomData<#instance> ));

let struct_decl = quote!(
#visibility struct #format_type(#optional_phantom);
);

let format_impl = quote!(
impl<#optional_instance_bound> #scrate::storage::generator::LinkedMapKeyFormat
for #format_type
{
type Hasher = #scrate::#hasher;

fn head_key() -> &'static [u8] {
#head_key
}
}
);

(format_type, quote!( #struct_decl #format_impl ))
};

quote!(
#key_format_decl

impl<#impl_trait> #scrate::#storage_generator_trait for #storage_struct
#optional_storage_where_clause
{
type Query = #query_type;
type Hasher = #scrate::#hasher;
type KeyFormat = #key_format_type;

fn prefix() -> &'static [u8] {
#final_prefix
}

fn head_key() -> &'static [u8] {
#head_key
}

fn from_optional_value_to_query(v: Option<#value_type>) -> Self::Query {
#from_optional_value_to_query
}
Expand Down
Loading