mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 17:31:05 +00:00
Include default values in storage metadata (#1264)
* Add a 'default' field to metadata. It contains code to generate the default value. * wasm update * Make 'default' field an `Option` * Boxed fn is not static, that won't be fine * static fn won't do it to as it cannot get T param, will try fat trait * Fat pointer over phantom data compatible with static instantiation * DecodeDifferent is cool, using it for decoding. * using once cell to do what would require copying lazy_static internals. * Remove cache when no_std (non compatible deps) * wasm bins update * Fuse tooling struct and enum derive.
This commit is contained in:
Generated
+7
@@ -1961,6 +1961,11 @@ dependencies = [
|
||||
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "opaque-debug"
|
||||
version = "0.1.1"
|
||||
@@ -3025,6 +3030,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"hex-literal 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"mashup 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"once_cell 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parity-codec 2.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parity-codec-derive 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"pretty_assertions 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@@ -4814,6 +4820,7 @@ dependencies = [
|
||||
"checksum num_cpus 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "cee7e88156f3f9e19bdd598f8d6c9db7bf4078f99f8381f43a55b09648d1a6e3"
|
||||
"checksum num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c51a3322e4bca9d212ad9a158a02abc6934d005490c054a2778df73a70aa0a30"
|
||||
"checksum ole32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5d2c49021782e5233cd243168edfa8037574afed4eba4bbaf538b3d8d1789d8c"
|
||||
"checksum once_cell 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "d7ce3535d54560c937c1652ba4a0da66bfc63e0f8e07bed127483afb6e5ee925"
|
||||
"checksum opaque-debug 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d620c9c26834b34f039489ac0dfdb12c7ac15ccaf818350a64c9b5334a452ad7"
|
||||
"checksum openssl 0.10.15 (registry+https://github.com/rust-lang/crates.io-index)" = "5e1309181cdcbdb51bc3b6bedb33dfac2a83b3d585033d3f6d9e22e8c1928613"
|
||||
"checksum openssl-probe 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de"
|
||||
|
||||
+7
@@ -508,6 +508,11 @@ dependencies = [
|
||||
"libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "openssl"
|
||||
version = "0.10.15"
|
||||
@@ -1009,6 +1014,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"hex-literal 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"mashup 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"once_cell 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parity-codec 2.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.81 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_derive 1.0.81 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@@ -1666,6 +1672,7 @@ dependencies = [
|
||||
"checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea"
|
||||
"checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1"
|
||||
"checksum num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c51a3322e4bca9d212ad9a158a02abc6934d005490c054a2778df73a70aa0a30"
|
||||
"checksum once_cell 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "d7ce3535d54560c937c1652ba4a0da66bfc63e0f8e07bed127483afb6e5ee925"
|
||||
"checksum openssl 0.10.15 (registry+https://github.com/rust-lang/crates.io-index)" = "5e1309181cdcbdb51bc3b6bedb33dfac2a83b3d585033d3f6d9e22e8c1928613"
|
||||
"checksum openssl-sys 0.9.39 (registry+https://github.com/rust-lang/crates.io-index)" = "278c1ad40a89aa1e741a1eed089a2f60b18fab8089c3139b542140fc7d674106"
|
||||
"checksum owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cdf84f41639e037b484f93433aa3897863b561ed65c6e59c7073d7c561710f37"
|
||||
|
||||
BIN
Binary file not shown.
Generated
+7
@@ -564,6 +564,11 @@ dependencies = [
|
||||
"libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "openssl"
|
||||
version = "0.10.15"
|
||||
@@ -1283,6 +1288,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"hex-literal 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"mashup 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"once_cell 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parity-codec 2.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.81 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_derive 1.0.81 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@@ -1991,6 +1997,7 @@ dependencies = [
|
||||
"checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea"
|
||||
"checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1"
|
||||
"checksum num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c51a3322e4bca9d212ad9a158a02abc6934d005490c054a2778df73a70aa0a30"
|
||||
"checksum once_cell 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "d7ce3535d54560c937c1652ba4a0da66bfc63e0f8e07bed127483afb6e5ee925"
|
||||
"checksum openssl 0.10.15 (registry+https://github.com/rust-lang/crates.io-index)" = "5e1309181cdcbdb51bc3b6bedb33dfac2a83b3d585033d3f6d9e22e8c1928613"
|
||||
"checksum openssl-sys 0.9.39 (registry+https://github.com/rust-lang/crates.io-index)" = "278c1ad40a89aa1e741a1eed089a2f60b18fab8089c3139b542140fc7d674106"
|
||||
"checksum owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cdf84f41639e037b484f93433aa3897863b561ed65c6e59c7073d7c561710f37"
|
||||
|
||||
BIN
Binary file not shown.
@@ -110,8 +110,8 @@ impl<B, O> serde::Serialize for DecodeDifferent<B, O>
|
||||
O: serde::Serialize + 'static,
|
||||
{
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
match self {
|
||||
DecodeDifferent::Encode(b) => b.serialize(serializer),
|
||||
@@ -187,8 +187,8 @@ impl<E: Encode + ::std::fmt::Debug> std::fmt::Debug for FnEncode<E> {
|
||||
#[cfg(feature = "std")]
|
||||
impl<E: Encode + serde::Serialize> serde::Serialize for FnEncode<E> {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
self.0().serialize(serializer)
|
||||
}
|
||||
@@ -229,9 +229,55 @@ pub struct StorageFunctionMetadata {
|
||||
pub name: DecodeDifferentStr,
|
||||
pub modifier: StorageFunctionModifier,
|
||||
pub ty: StorageFunctionType,
|
||||
pub default: ByteGetter,
|
||||
pub documentation: DecodeDifferentArray<&'static str, StringBuf>,
|
||||
}
|
||||
|
||||
/// A technical trait to store lazy initiated vec value as static dyn pointer.
|
||||
pub trait DefaultByte {
|
||||
fn default_byte(&self) -> Vec<u8>;
|
||||
}
|
||||
|
||||
/// Wrapper over dyn pointer for accessing a cached once byet value.
|
||||
#[derive(Clone)]
|
||||
pub struct DefaultByteGetter(pub &'static dyn DefaultByte);
|
||||
|
||||
/// Decode different for static lazy initiated byte value.
|
||||
pub type ByteGetter = DecodeDifferent<DefaultByteGetter, Vec<u8>>;
|
||||
|
||||
impl Encode for DefaultByteGetter {
|
||||
fn encode_to<W: Output>(&self, dest: &mut W) {
|
||||
self.0.default_byte().encode_to(dest)
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<DefaultByteGetter> for DefaultByteGetter {
|
||||
fn eq(&self, other: &DefaultByteGetter) -> bool {
|
||||
let left = self.0.default_byte();
|
||||
let right = other.0.default_byte();
|
||||
left.eq(&right)
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for DefaultByteGetter { }
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl serde::Serialize for DefaultByteGetter {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
self.0.default_byte().serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl std::fmt::Debug for DefaultByteGetter {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
self.0.default_byte().fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
/// A storage function type.
|
||||
#[derive(Clone, PartialEq, Eq, Encode)]
|
||||
#[cfg_attr(feature = "std", derive(Decode, Debug, Serialize))]
|
||||
|
||||
@@ -14,6 +14,7 @@ sr-io = { path = "../../core/sr-io", default-features = false }
|
||||
sr-primitives = { path = "../../core/sr-primitives", default-features = false }
|
||||
srml-support-procedural = { path = "./procedural" }
|
||||
mashup = "0.1.7"
|
||||
once_cell = { version = "0.1.6", default-features = false, optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
pretty_assertions = "0.5.1"
|
||||
@@ -23,6 +24,7 @@ parity-codec-derive = { version = "2.1" }
|
||||
default = ["std"]
|
||||
std = [
|
||||
"hex-literal",
|
||||
"once_cell",
|
||||
"serde/std",
|
||||
"serde_derive",
|
||||
"sr-io/std",
|
||||
|
||||
@@ -26,7 +26,7 @@ use syn::token::CustomKeyword;
|
||||
pub mod transformation;
|
||||
|
||||
/// Parsing usage only
|
||||
#[derive(ParseStruct, ToTokensStruct, Debug)]
|
||||
#[derive(Parse, ToTokens, Debug)]
|
||||
struct StorageDefinition {
|
||||
pub hidden_crate: Option<SpecificHiddenCrate>,
|
||||
pub visibility: syn::Visibility,
|
||||
@@ -44,30 +44,30 @@ struct StorageDefinition {
|
||||
}
|
||||
|
||||
|
||||
#[derive(ParseStruct, ToTokensStruct, Debug)]
|
||||
#[derive(Parse, ToTokens, Debug)]
|
||||
struct SpecificHiddenCrate {
|
||||
pub keyword: ext::CustomToken<SpecificHiddenCrate>,
|
||||
pub ident: ext::Parens<Ident>,
|
||||
}
|
||||
|
||||
#[derive(ParseStruct, ToTokensStruct, Debug)]
|
||||
#[derive(Parse, ToTokens, Debug)]
|
||||
struct AddExtraGenesis {
|
||||
pub extragenesis_keyword: ext::CustomToken<AddExtraGenesis>,
|
||||
pub content: ext::Braces<AddExtraGenesisContent>,
|
||||
}
|
||||
|
||||
#[derive(ParseStruct, ToTokensStruct, Debug)]
|
||||
#[derive(Parse, ToTokens, Debug)]
|
||||
struct AddExtraGenesisContent {
|
||||
pub lines: ext::Punctuated<AddExtraGenesisLineEnum, Token![;]>,
|
||||
}
|
||||
|
||||
#[derive(ParseEnum, ToTokensEnum, Debug)]
|
||||
#[derive(Parse, ToTokens, Debug)]
|
||||
enum AddExtraGenesisLineEnum {
|
||||
AddExtraGenesisLine(AddExtraGenesisLine),
|
||||
AddExtraGenesisBuild(DeclStorageBuild),
|
||||
}
|
||||
|
||||
#[derive(ParseStruct, ToTokensStruct, Debug)]
|
||||
#[derive(Parse, ToTokens, Debug)]
|
||||
struct AddExtraGenesisLine {
|
||||
pub attrs: ext::OuterAttributes,
|
||||
pub config_keyword: ext::CustomToken<ConfigKeyword>,
|
||||
@@ -78,7 +78,7 @@ struct AddExtraGenesisLine {
|
||||
pub default_value: ext::Seq<DeclStorageDefault>,
|
||||
}
|
||||
|
||||
#[derive(ParseStruct, ToTokensStruct, Debug)]
|
||||
#[derive(Parse, ToTokens, Debug)]
|
||||
struct DeclStorageLine {
|
||||
// attrs (main use case is doc)
|
||||
pub attrs: ext::OuterAttributes,
|
||||
@@ -96,31 +96,31 @@ struct DeclStorageLine {
|
||||
}
|
||||
|
||||
|
||||
#[derive(ParseStruct, ToTokensStruct, Debug)]
|
||||
#[derive(Parse, ToTokens, Debug)]
|
||||
struct DeclStorageGetter {
|
||||
pub getter_keyword: ext::CustomToken<DeclStorageGetter>,
|
||||
pub getfn: ext::Parens<Ident>,
|
||||
}
|
||||
|
||||
#[derive(ParseStruct, ToTokensStruct, Debug)]
|
||||
#[derive(Parse, ToTokens, Debug)]
|
||||
struct DeclStorageConfig {
|
||||
pub config_keyword: ext::CustomToken<DeclStorageConfig>,
|
||||
pub expr: ext::Parens<Option<syn::Ident>>,
|
||||
}
|
||||
|
||||
#[derive(ParseStruct, ToTokensStruct, Debug)]
|
||||
#[derive(Parse, ToTokens, Debug)]
|
||||
struct DeclStorageBuild {
|
||||
pub build_keyword: ext::CustomToken<DeclStorageBuild>,
|
||||
pub expr: ext::Parens<syn::Expr>,
|
||||
}
|
||||
|
||||
#[derive(ParseEnum, ToTokensEnum, Debug)]
|
||||
#[derive(Parse, ToTokens, Debug)]
|
||||
enum DeclStorageType {
|
||||
Map(DeclStorageMap),
|
||||
Simple(syn::Type),
|
||||
}
|
||||
|
||||
#[derive(ParseStruct, ToTokensStruct, Debug)]
|
||||
#[derive(Parse, ToTokens, Debug)]
|
||||
struct DeclStorageMap {
|
||||
pub map_keyword: ext::CustomToken<MapKeyword>,
|
||||
pub key: syn::Type,
|
||||
@@ -128,7 +128,7 @@ struct DeclStorageMap {
|
||||
pub value: syn::Type,
|
||||
}
|
||||
|
||||
#[derive(ParseStruct, ToTokensStruct, Debug)]
|
||||
#[derive(Parse, ToTokens, Debug)]
|
||||
struct DeclStorageDefault {
|
||||
pub equal_token: Token![=],
|
||||
pub expr: syn::Expr,
|
||||
|
||||
@@ -110,8 +110,10 @@ pub fn decl_storage_impl(input: TokenStream) -> TokenStream {
|
||||
&traitinstance,
|
||||
&storage_lines,
|
||||
);
|
||||
let store_functions_to_metadata = store_functions_to_metadata(
|
||||
let (store_default_struct, store_functions_to_metadata) = store_functions_to_metadata(
|
||||
&scrate,
|
||||
&traitinstance,
|
||||
&traittype,
|
||||
&storage_lines,
|
||||
);
|
||||
let cratename_string = cratename.to_string();
|
||||
@@ -119,12 +121,13 @@ pub fn decl_storage_impl(input: TokenStream) -> TokenStream {
|
||||
#scrate_decl
|
||||
#decl_storage_items
|
||||
#visibility trait #storetype {
|
||||
#decl_store_items
|
||||
#decl_store_items
|
||||
}
|
||||
#store_default_struct
|
||||
impl<#traitinstance: #traittype> #storetype for #module_ident<#traitinstance> {
|
||||
#impl_store_items
|
||||
}
|
||||
impl<#traitinstance: #traittype> #module_ident<#traitinstance> {
|
||||
impl<#traitinstance: 'static + #traittype> #module_ident<#traitinstance> {
|
||||
#impl_store_fns
|
||||
pub fn store_metadata() -> #scrate::storage::generator::StorageMetadata {
|
||||
#scrate::storage::generator::StorageMetadata {
|
||||
@@ -557,15 +560,19 @@ fn impl_store_fns(
|
||||
|
||||
fn store_functions_to_metadata (
|
||||
scrate: &TokenStream2,
|
||||
traitinstance: &Ident,
|
||||
traittype: &syn::TypeParamBound,
|
||||
storage_lines: &ext::Punctuated<DeclStorageLine, Token![;]>,
|
||||
) -> TokenStream2 {
|
||||
) -> (TokenStream2, TokenStream2) {
|
||||
|
||||
let mut items = TokenStream2::new();
|
||||
let mut default_getter_struct_def = TokenStream2::new();
|
||||
for sline in storage_lines.inner.iter() {
|
||||
let DeclStorageLine {
|
||||
attrs,
|
||||
name,
|
||||
storage_type,
|
||||
default_value,
|
||||
..
|
||||
} = sline;
|
||||
|
||||
@@ -603,6 +610,11 @@ fn store_functions_to_metadata (
|
||||
#scrate::storage::generator::StorageFunctionModifier::Optional
|
||||
}
|
||||
};
|
||||
let default = default_value.inner.get(0).as_ref().map(|d| &d.expr)
|
||||
.map(|d| {
|
||||
quote!( #d )
|
||||
})
|
||||
.unwrap_or_else(|| quote!( Default::default() ));
|
||||
let mut docs = TokenStream2::new();
|
||||
for attr in attrs.inner.iter().filter_map(|v| v.interpret_meta()) {
|
||||
if let syn::Meta::NameValue(syn::MetaNameValue{
|
||||
@@ -616,20 +628,52 @@ fn store_functions_to_metadata (
|
||||
}
|
||||
}
|
||||
let str_name = name.to_string();
|
||||
let struct_name = proc_macro2::Ident::new(&("__GetByteStruct".to_string() + &str_name), name.span());
|
||||
let cache_name = proc_macro2::Ident::new(&("__CacheGetByteStruct".to_string() + &str_name), name.span());
|
||||
let item = quote! {
|
||||
#scrate::storage::generator::StorageFunctionMetadata {
|
||||
name: #scrate::storage::generator::DecodeDifferent::Encode(#str_name),
|
||||
modifier: #modifier,
|
||||
ty: #stype,
|
||||
default: #scrate::storage::generator::DecodeDifferent::Encode(
|
||||
#scrate::storage::generator::DefaultByteGetter(
|
||||
&#struct_name::<#traitinstance>(#scrate::rstd::marker::PhantomData)
|
||||
)
|
||||
),
|
||||
documentation: #scrate::storage::generator::DecodeDifferent::Encode(&[ #docs ]),
|
||||
},
|
||||
};
|
||||
items.extend(item);
|
||||
let def_get = quote! {
|
||||
pub struct #struct_name<#traitinstance>(pub #scrate::rstd::marker::PhantomData<#traitinstance>);
|
||||
#[cfg(feature = "std")]
|
||||
static #cache_name: #scrate::once_cell::sync::OnceCell<Vec<u8>> = #scrate::once_cell::sync::OnceCell::INIT;
|
||||
#[cfg(feature = "std")]
|
||||
impl<#traitinstance: #traittype> #scrate::storage::generator::DefaultByte for #struct_name<#traitinstance> {
|
||||
fn default_byte(&self) -> Vec<u8> {
|
||||
use #scrate::codec::Encode;
|
||||
#cache_name.get_or_init(|| {
|
||||
let def_val: #gettype = #default;
|
||||
<#gettype as Encode>::encode(&def_val)
|
||||
}).clone()
|
||||
}
|
||||
}
|
||||
#[cfg(not(feature = "std"))]
|
||||
impl<#traitinstance: #traittype> #scrate::storage::generator::DefaultByte for #struct_name<#traitinstance> {
|
||||
fn default_byte(&self) -> Vec<u8> {
|
||||
use #scrate::codec::Encode;
|
||||
let def_val: #gettype = #default;
|
||||
<#gettype as Encode>::encode(&def_val)
|
||||
}
|
||||
}
|
||||
};
|
||||
default_getter_struct_def.extend(def_get);
|
||||
}
|
||||
|
||||
quote!{
|
||||
#scrate::storage::generator::DecodeDifferent::Encode(&[
|
||||
#items
|
||||
])
|
||||
}
|
||||
(default_getter_struct_def, quote!{
|
||||
{
|
||||
#scrate::storage::generator::DecodeDifferent::Encode(&[
|
||||
#items
|
||||
])
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -59,16 +59,30 @@ pub(crate) fn fields_access(
|
||||
})
|
||||
}
|
||||
|
||||
/// self defined parsing struct (use where clause on struct for it: not meant for good struct
|
||||
/// design but fast parse impl).
|
||||
#[proc_macro_derive(ParseStruct)]
|
||||
pub fn derive_parse_struct(input: TokenStream) -> TokenStream {
|
||||
/// self defined parsing struct or enum.
|
||||
/// not meant for any struct/enum, just for fast
|
||||
/// parse implementation.
|
||||
/// For enums:
|
||||
/// variant are tested in order of definition.
|
||||
/// Empty variant is always true.
|
||||
/// Please use carefully, this will fully parse successfull variant twice.
|
||||
#[proc_macro_derive(Parse)]
|
||||
pub fn derive_parse(input: TokenStream) -> TokenStream {
|
||||
let item = parse_macro_input!(input as syn::Item);
|
||||
match item {
|
||||
syn::Item::Enum(input) => derive_parse_enum(input),
|
||||
syn::Item::Struct(input) => derive_parse_struct(input),
|
||||
_ => TokenStream::new(), // ignore
|
||||
}
|
||||
}
|
||||
|
||||
fn derive_parse_struct(input: syn::ItemStruct) -> TokenStream {
|
||||
let syn::ItemStruct {
|
||||
ident,
|
||||
generics,
|
||||
fields,
|
||||
..
|
||||
} = parse_macro_input!(input as syn::ItemStruct);
|
||||
} = input;
|
||||
let field_names = {
|
||||
let name = fields_idents(fields.iter().map(Clone::clone));
|
||||
quote!{
|
||||
@@ -93,42 +107,13 @@ pub fn derive_parse_struct(input: TokenStream) -> TokenStream {
|
||||
tokens.into()
|
||||
}
|
||||
|
||||
#[proc_macro_derive(ToTokensStruct)]
|
||||
pub fn derive_totokens_struct(input: TokenStream) -> TokenStream {
|
||||
let syn::ItemStruct {
|
||||
ident,
|
||||
generics,
|
||||
fields,
|
||||
..
|
||||
} = parse_macro_input!(input as syn::ItemStruct);
|
||||
let fields = fields_access(fields.iter().map(Clone::clone));
|
||||
let tokens = quote! {
|
||||
|
||||
impl #generics quote::ToTokens for #ident #generics {
|
||||
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
|
||||
#(
|
||||
self.#fields.to_tokens(tokens);
|
||||
)*
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
tokens.into()
|
||||
}
|
||||
|
||||
|
||||
/// self defined parsing enum, variant are tested in order of definition.
|
||||
/// Empty variant allways true.
|
||||
/// Please use carefully, this will fully parse successfull variant twice.
|
||||
#[proc_macro_derive(ParseEnum)]
|
||||
pub fn derive_parse_enum(input: TokenStream) -> TokenStream {
|
||||
|
||||
fn derive_parse_enum(input: syn::ItemEnum) -> TokenStream {
|
||||
let syn::ItemEnum {
|
||||
ident,
|
||||
generics,
|
||||
variants,
|
||||
..
|
||||
} = parse_macro_input!(input as syn::ItemEnum);
|
||||
} = input;
|
||||
let variants = variants.iter().map(|v| {
|
||||
let variant_ident = v.ident.clone();
|
||||
let fields_build = if v.fields.iter().count() > 0 {
|
||||
@@ -187,15 +172,51 @@ pub fn derive_parse_enum(input: TokenStream) -> TokenStream {
|
||||
tokens.into()
|
||||
}
|
||||
|
||||
/// only output field (empty field act as a None)
|
||||
#[proc_macro_derive(ToTokensEnum)]
|
||||
pub fn derive_totokens_enum(input: TokenStream) -> TokenStream {
|
||||
/// self defined parsing struct or enum.
|
||||
/// not meant for any struct/enum, just for fast
|
||||
/// parse implementation.
|
||||
/// For enum:
|
||||
/// it only output fields (empty field act as a None).
|
||||
#[proc_macro_derive(ToTokens)]
|
||||
pub fn derive_totokens(input: TokenStream) -> TokenStream {
|
||||
let item = parse_macro_input!(input as syn::Item);
|
||||
match item {
|
||||
syn::Item::Enum(input) => derive_totokens_enum(input),
|
||||
syn::Item::Struct(input) => derive_totokens_struct(input),
|
||||
_ => TokenStream::new(), // ignore
|
||||
}
|
||||
}
|
||||
|
||||
fn derive_totokens_struct(input: syn::ItemStruct) -> TokenStream {
|
||||
let syn::ItemStruct {
|
||||
ident,
|
||||
generics,
|
||||
fields,
|
||||
..
|
||||
} = input;
|
||||
|
||||
let fields = fields_access(fields.iter().map(Clone::clone));
|
||||
let tokens = quote! {
|
||||
|
||||
impl #generics quote::ToTokens for #ident #generics {
|
||||
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
|
||||
#(
|
||||
self.#fields.to_tokens(tokens);
|
||||
)*
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
tokens.into()
|
||||
}
|
||||
|
||||
fn derive_totokens_enum(input: syn::ItemEnum) -> TokenStream {
|
||||
let syn::ItemEnum {
|
||||
ident,
|
||||
generics,
|
||||
variants,
|
||||
..
|
||||
} = parse_macro_input!(input as syn::ItemEnum);
|
||||
} = input;
|
||||
let variants = variants.iter().map(|v| {
|
||||
let v_ident = v.ident.clone();
|
||||
let fields_build = if v.fields.iter().count() > 0 {
|
||||
|
||||
@@ -31,7 +31,7 @@ use syn::Ident;
|
||||
|
||||
/// stop parsing here getting remaining token as content
|
||||
/// Warn duplicate stream (part of)
|
||||
#[derive(ParseStruct, ToTokensStruct, Debug)]
|
||||
#[derive(Parse, ToTokens, Debug)]
|
||||
pub struct StopParse {
|
||||
pub inner: T2,
|
||||
}
|
||||
|
||||
@@ -45,6 +45,11 @@ extern crate parity_codec_derive;
|
||||
|
||||
#[doc(hidden)]
|
||||
pub extern crate parity_codec as codec;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[doc(hidden)]
|
||||
pub extern crate once_cell;
|
||||
|
||||
pub use self::storage::generator::Storage as GenericStorage;
|
||||
|
||||
#[macro_use]
|
||||
|
||||
@@ -15,7 +15,8 @@
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
pub use srml_metadata::{
|
||||
DecodeDifferent, FnEncode, RuntimeMetadata, RuntimeModuleMetadata
|
||||
DecodeDifferent, FnEncode, RuntimeMetadata, RuntimeModuleMetadata,
|
||||
DefaultByteGetter,
|
||||
};
|
||||
|
||||
/// Implements the metadata support for the given runtime and all its modules.
|
||||
@@ -188,7 +189,7 @@ mod tests {
|
||||
StorageMethod : Option<u32>;
|
||||
}
|
||||
add_extra_genesis {
|
||||
build(|_, _, _| {});
|
||||
build(|_, _, _| {});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -311,15 +312,20 @@ mod tests {
|
||||
)),
|
||||
storage: Some(DecodeDifferent::Encode(FnEncode(||
|
||||
StorageMetadata {
|
||||
prefix: DecodeDifferent::Encode("TestStorage"),
|
||||
functions: DecodeDifferent::Encode(&[
|
||||
StorageFunctionMetadata {
|
||||
name: DecodeDifferent::Encode("StorageMethod"),
|
||||
modifier: StorageFunctionModifier::Optional,
|
||||
ty: StorageFunctionType::Plain(DecodeDifferent::Encode("u32")),
|
||||
documentation: DecodeDifferent::Encode(&[]),
|
||||
}
|
||||
])
|
||||
prefix: DecodeDifferent::Encode("TestStorage"),
|
||||
functions: DecodeDifferent::Encode(&[
|
||||
StorageFunctionMetadata {
|
||||
name: DecodeDifferent::Encode("StorageMethod"),
|
||||
modifier: StorageFunctionModifier::Optional,
|
||||
ty: StorageFunctionType::Plain(DecodeDifferent::Encode("u32")),
|
||||
default: DecodeDifferent::Encode(
|
||||
DefaultByteGetter(
|
||||
&event_module2::__GetByteStructStorageMethod(::std::marker::PhantomData::<TestRuntime>)
|
||||
)
|
||||
),
|
||||
documentation: DecodeDifferent::Encode(&[]),
|
||||
}
|
||||
])
|
||||
}
|
||||
))),
|
||||
}
|
||||
|
||||
@@ -55,7 +55,8 @@ pub use rstd::marker::PhantomData;
|
||||
|
||||
pub use srml_metadata::{
|
||||
DecodeDifferent, StorageMetadata, StorageFunctionMetadata,
|
||||
StorageFunctionType, StorageFunctionModifier
|
||||
StorageFunctionType, StorageFunctionModifier,
|
||||
DefaultByte, DefaultByteGetter,
|
||||
};
|
||||
|
||||
/// Abstraction around storage.
|
||||
@@ -525,6 +526,7 @@ mod tests {
|
||||
use std::cell::RefCell;
|
||||
use codec::Codec;
|
||||
use super::*;
|
||||
use rstd::marker::PhantomData;
|
||||
|
||||
impl Storage for RefCell<HashMap<Vec<u8>, Vec<u8>>> {
|
||||
fn exists(&self, key: &[u8]) -> bool {
|
||||
@@ -657,80 +659,117 @@ mod tests {
|
||||
name: DecodeDifferent::Encode("U32"),
|
||||
modifier: StorageFunctionModifier::Optional,
|
||||
ty: StorageFunctionType::Plain(DecodeDifferent::Encode("u32")),
|
||||
default: DecodeDifferent::Encode(
|
||||
DefaultByteGetter(&__GetByteStructU32(PhantomData::<TraitImpl>))
|
||||
),
|
||||
documentation: DecodeDifferent::Encode(&[ " Hello, this is doc!" ]),
|
||||
},
|
||||
StorageFunctionMetadata {
|
||||
name: DecodeDifferent::Encode("PUBU32"),
|
||||
modifier: StorageFunctionModifier::Optional,
|
||||
ty: StorageFunctionType::Plain(DecodeDifferent::Encode("u32")),
|
||||
default: DecodeDifferent::Encode(
|
||||
DefaultByteGetter(&__GetByteStructPUBU32(PhantomData::<TraitImpl>))
|
||||
),
|
||||
documentation: DecodeDifferent::Encode(&[]),
|
||||
},
|
||||
StorageFunctionMetadata {
|
||||
name: DecodeDifferent::Encode("U32MYDEF"),
|
||||
modifier: StorageFunctionModifier::Optional,
|
||||
ty: StorageFunctionType::Plain(DecodeDifferent::Encode("u32")),
|
||||
default: DecodeDifferent::Encode(
|
||||
DefaultByteGetter(&__GetByteStructU32MYDEF(PhantomData::<TraitImpl>))
|
||||
),
|
||||
documentation: DecodeDifferent::Encode(&[]),
|
||||
},
|
||||
StorageFunctionMetadata {
|
||||
name: DecodeDifferent::Encode("PUBU32MYDEF"),
|
||||
modifier: StorageFunctionModifier::Optional,
|
||||
ty: StorageFunctionType::Plain(DecodeDifferent::Encode("u32")),
|
||||
default: DecodeDifferent::Encode(
|
||||
DefaultByteGetter(&__GetByteStructPUBU32MYDEF(PhantomData::<TraitImpl>))
|
||||
),
|
||||
documentation: DecodeDifferent::Encode(&[]),
|
||||
},
|
||||
|
||||
StorageFunctionMetadata {
|
||||
name: DecodeDifferent::Encode("GETU32"),
|
||||
modifier: StorageFunctionModifier::Default,
|
||||
ty: StorageFunctionType::Plain(DecodeDifferent::Encode("T::Origin")),
|
||||
default: DecodeDifferent::Encode(
|
||||
DefaultByteGetter(&__GetByteStructGETU32(PhantomData::<TraitImpl>))
|
||||
),
|
||||
documentation: DecodeDifferent::Encode(&[]),
|
||||
},
|
||||
StorageFunctionMetadata {
|
||||
name: DecodeDifferent::Encode("PUBGETU32"),
|
||||
modifier: StorageFunctionModifier::Default,
|
||||
ty: StorageFunctionType::Plain(DecodeDifferent::Encode("u32")),
|
||||
default: DecodeDifferent::Encode(
|
||||
DefaultByteGetter(&__GetByteStructPUBGETU32(PhantomData::<TraitImpl>))
|
||||
),
|
||||
documentation: DecodeDifferent::Encode(&[]),
|
||||
},
|
||||
StorageFunctionMetadata {
|
||||
name: DecodeDifferent::Encode("GETU32WITHCONFIG"),
|
||||
modifier: StorageFunctionModifier::Default,
|
||||
ty: StorageFunctionType::Plain(DecodeDifferent::Encode("u32")),
|
||||
default: DecodeDifferent::Encode(
|
||||
DefaultByteGetter(&__GetByteStructGETU32WITHCONFIG(PhantomData::<TraitImpl>))
|
||||
),
|
||||
documentation: DecodeDifferent::Encode(&[]),
|
||||
},
|
||||
StorageFunctionMetadata {
|
||||
name: DecodeDifferent::Encode("PUBGETU32WITHCONFIG"),
|
||||
modifier: StorageFunctionModifier::Default,
|
||||
ty: StorageFunctionType::Plain(DecodeDifferent::Encode("u32")),
|
||||
default: DecodeDifferent::Encode(
|
||||
DefaultByteGetter(&__GetByteStructPUBGETU32WITHCONFIG(PhantomData::<TraitImpl>))
|
||||
),
|
||||
documentation: DecodeDifferent::Encode(&[]),
|
||||
},
|
||||
|
||||
StorageFunctionMetadata {
|
||||
name: DecodeDifferent::Encode("GETU32MYDEF"),
|
||||
modifier: StorageFunctionModifier::Optional,
|
||||
ty: StorageFunctionType::Plain(DecodeDifferent::Encode("u32")),
|
||||
default: DecodeDifferent::Encode(
|
||||
DefaultByteGetter(&__GetByteStructGETU32MYDEF(PhantomData::<TraitImpl>))
|
||||
),
|
||||
documentation: DecodeDifferent::Encode(&[]),
|
||||
},
|
||||
StorageFunctionMetadata {
|
||||
name: DecodeDifferent::Encode("PUBGETU32MYDEF"),
|
||||
modifier: StorageFunctionModifier::Default,
|
||||
ty: StorageFunctionType::Plain(DecodeDifferent::Encode("u32")),
|
||||
default: DecodeDifferent::Encode(
|
||||
DefaultByteGetter(&__GetByteStructPUBGETU32MYDEF(PhantomData::<TraitImpl>))
|
||||
),
|
||||
documentation: DecodeDifferent::Encode(&[]),
|
||||
},
|
||||
StorageFunctionMetadata {
|
||||
name: DecodeDifferent::Encode("GETU32WITHCONFIGMYDEF"),
|
||||
modifier: StorageFunctionModifier::Default,
|
||||
ty: StorageFunctionType::Plain(DecodeDifferent::Encode("u32")),
|
||||
default: DecodeDifferent::Encode(
|
||||
DefaultByteGetter(&__GetByteStructGETU32WITHCONFIGMYDEF(PhantomData::<TraitImpl>))
|
||||
),
|
||||
documentation: DecodeDifferent::Encode(&[]),
|
||||
},
|
||||
StorageFunctionMetadata {
|
||||
name: DecodeDifferent::Encode("PUBGETU32WITHCONFIGMYDEF"),
|
||||
modifier: StorageFunctionModifier::Default,
|
||||
ty: StorageFunctionType::Plain(DecodeDifferent::Encode("u32")),
|
||||
default: DecodeDifferent::Encode(
|
||||
DefaultByteGetter(&__GetByteStructPUBGETU32WITHCONFIGMYDEF(PhantomData::<TraitImpl>))
|
||||
),
|
||||
documentation: DecodeDifferent::Encode(&[]),
|
||||
},
|
||||
StorageFunctionMetadata {
|
||||
name: DecodeDifferent::Encode("PUBGETU32WITHCONFIGMYDEFOPT"),
|
||||
modifier: StorageFunctionModifier::Optional,
|
||||
ty: StorageFunctionType::Plain(DecodeDifferent::Encode("u32")),
|
||||
default: DecodeDifferent::Encode(
|
||||
DefaultByteGetter(&__GetByteStructPUBGETU32WITHCONFIGMYDEFOPT(PhantomData::<TraitImpl>))
|
||||
),
|
||||
documentation: DecodeDifferent::Encode(&[]),
|
||||
},
|
||||
|
||||
@@ -740,6 +779,9 @@ mod tests {
|
||||
ty: StorageFunctionType::Map{
|
||||
key: DecodeDifferent::Encode("u32"), value: DecodeDifferent::Encode("String")
|
||||
},
|
||||
default: DecodeDifferent::Encode(
|
||||
DefaultByteGetter(&__GetByteStructMAPU32(PhantomData::<TraitImpl>))
|
||||
),
|
||||
documentation: DecodeDifferent::Encode(&[]),
|
||||
},
|
||||
StorageFunctionMetadata {
|
||||
@@ -748,6 +790,9 @@ mod tests {
|
||||
ty: StorageFunctionType::Map{
|
||||
key: DecodeDifferent::Encode("u32"), value: DecodeDifferent::Encode("String")
|
||||
},
|
||||
default: DecodeDifferent::Encode(
|
||||
DefaultByteGetter(&__GetByteStructPUBMAPU32(PhantomData::<TraitImpl>))
|
||||
),
|
||||
documentation: DecodeDifferent::Encode(&[]),
|
||||
},
|
||||
StorageFunctionMetadata {
|
||||
@@ -756,6 +801,9 @@ mod tests {
|
||||
ty: StorageFunctionType::Map{
|
||||
key: DecodeDifferent::Encode("u32"), value: DecodeDifferent::Encode("String")
|
||||
},
|
||||
default: DecodeDifferent::Encode(
|
||||
DefaultByteGetter(&__GetByteStructMAPU32MYDEF(PhantomData::<TraitImpl>))
|
||||
),
|
||||
documentation: DecodeDifferent::Encode(&[]),
|
||||
},
|
||||
StorageFunctionMetadata {
|
||||
@@ -764,15 +812,20 @@ mod tests {
|
||||
ty: StorageFunctionType::Map{
|
||||
key: DecodeDifferent::Encode("u32"), value: DecodeDifferent::Encode("String")
|
||||
},
|
||||
default: DecodeDifferent::Encode(
|
||||
DefaultByteGetter(&__GetByteStructPUBMAPU32MYDEF(PhantomData::<TraitImpl>))
|
||||
),
|
||||
documentation: DecodeDifferent::Encode(&[]),
|
||||
},
|
||||
|
||||
StorageFunctionMetadata {
|
||||
name: DecodeDifferent::Encode("GETMAPU32"),
|
||||
modifier: StorageFunctionModifier::Default,
|
||||
ty: StorageFunctionType::Map{
|
||||
key: DecodeDifferent::Encode("u32"), value: DecodeDifferent::Encode("String")
|
||||
},
|
||||
default: DecodeDifferent::Encode(
|
||||
DefaultByteGetter(&__GetByteStructGETMAPU32(PhantomData::<TraitImpl>))
|
||||
),
|
||||
documentation: DecodeDifferent::Encode(&[]),
|
||||
},
|
||||
StorageFunctionMetadata {
|
||||
@@ -781,15 +834,20 @@ mod tests {
|
||||
ty: StorageFunctionType::Map{
|
||||
key: DecodeDifferent::Encode("u32"), value: DecodeDifferent::Encode("String")
|
||||
},
|
||||
default: DecodeDifferent::Encode(
|
||||
DefaultByteGetter(&__GetByteStructPUBGETMAPU32(PhantomData::<TraitImpl>))
|
||||
),
|
||||
documentation: DecodeDifferent::Encode(&[]),
|
||||
},
|
||||
|
||||
StorageFunctionMetadata {
|
||||
name: DecodeDifferent::Encode("GETMAPU32MYDEF"),
|
||||
modifier: StorageFunctionModifier::Default,
|
||||
ty: StorageFunctionType::Map{
|
||||
key: DecodeDifferent::Encode("u32"), value: DecodeDifferent::Encode("String")
|
||||
},
|
||||
default: DecodeDifferent::Encode(
|
||||
DefaultByteGetter(&__GetByteStructGETMAPU32MYDEF(PhantomData::<TraitImpl>))
|
||||
),
|
||||
documentation: DecodeDifferent::Encode(&[]),
|
||||
},
|
||||
StorageFunctionMetadata {
|
||||
@@ -798,24 +856,36 @@ mod tests {
|
||||
ty: StorageFunctionType::Map{
|
||||
key: DecodeDifferent::Encode("u32"), value: DecodeDifferent::Encode("String")
|
||||
},
|
||||
default: DecodeDifferent::Encode(
|
||||
DefaultByteGetter(&__GetByteStructPUBGETMAPU32MYDEF(PhantomData::<TraitImpl>))
|
||||
),
|
||||
documentation: DecodeDifferent::Encode(&[]),
|
||||
},
|
||||
StorageFunctionMetadata {
|
||||
name: DecodeDifferent::Encode("COMPLEX_TYPE1"),
|
||||
modifier: StorageFunctionModifier::Default,
|
||||
ty: StorageFunctionType::Plain(DecodeDifferent::Encode("::std::vec::Vec<<T as Trait>::Origin>")),
|
||||
default: DecodeDifferent::Encode(
|
||||
DefaultByteGetter(&__GetByteStructCOMPLEX_TYPE1(PhantomData::<TraitImpl>))
|
||||
),
|
||||
documentation: DecodeDifferent::Encode(&[]),
|
||||
},
|
||||
StorageFunctionMetadata {
|
||||
name: DecodeDifferent::Encode("COMPLEX_TYPE2"),
|
||||
modifier: StorageFunctionModifier::Default,
|
||||
ty: StorageFunctionType::Plain(DecodeDifferent::Encode("(Vec<Vec<(u16, Box<()>)>>, u32)")),
|
||||
default: DecodeDifferent::Encode(
|
||||
DefaultByteGetter(&__GetByteStructCOMPLEX_TYPE2(PhantomData::<TraitImpl>))
|
||||
),
|
||||
documentation: DecodeDifferent::Encode(&[]),
|
||||
},
|
||||
StorageFunctionMetadata {
|
||||
name: DecodeDifferent::Encode("COMPLEX_TYPE3"),
|
||||
modifier: StorageFunctionModifier::Default,
|
||||
ty: StorageFunctionType::Plain(DecodeDifferent::Encode("([u32; 25])")),
|
||||
default: DecodeDifferent::Encode(
|
||||
DefaultByteGetter(&__GetByteStructCOMPLEX_TYPE3(PhantomData::<TraitImpl>))
|
||||
),
|
||||
documentation: DecodeDifferent::Encode(&[]),
|
||||
},
|
||||
])
|
||||
|
||||
@@ -63,7 +63,7 @@ pub fn extrinsics_data_root<H: Hash>(xts: Vec<Vec<u8>>) -> H::Output {
|
||||
H::enumerated_trie_root(&xts)
|
||||
}
|
||||
|
||||
pub trait Trait: Eq + Clone {
|
||||
pub trait Trait: 'static + Eq + Clone {
|
||||
type Origin: Into<Option<RawOrigin<Self::AccountId>>> + From<RawOrigin<Self::AccountId>>;
|
||||
type Index: Parameter + Member + MaybeSerializeDebugButNotDeserialize + Default + MaybeDisplay + SimpleArithmetic + Copy;
|
||||
type BlockNumber: Parameter + Member + MaybeSerializeDebug + MaybeDisplay + SimpleArithmetic + Default + Bounded + Copy + rstd::hash::Hash;
|
||||
|
||||
Reference in New Issue
Block a user