-
Notifications
You must be signed in to change notification settings - Fork 28
Derive TypeInfo for own types #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 16 commits
84b3bb4
c2f0f06
b4ba154
b1d26c2
22fb440
00aae34
5a165b1
9ac6306
39f8459
7db6778
687c757
7f4fae7
1d8ff8c
5c22f19
58dc3d2
e73c061
03bf034
c7bfb52
89d4499
14055e1
c7c19fc
d3259c5
54deaf4
5fd7525
2d7530d
ba2e94a
3420903
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,7 +70,7 @@ fn generate_type(input: TokenStream2) -> Result<TokenStream2> { | |
|
|
||
| utils::check_attributes(&ast)?; | ||
|
|
||
| let scale_info = crate_name_ident("scale-info")?; | ||
| let scale_info = scale_info_crate_tokens()?; | ||
| let parity_scale_codec = crate_name_ident("parity-scale-codec")?; | ||
|
|
||
| let ident = &ast.ident; | ||
|
|
@@ -91,11 +91,10 @@ fn generate_type(input: TokenStream2) -> Result<TokenStream2> { | |
| }; | ||
|
|
||
| let (impl_generics, ty_generics, _) = ast.generics.split_for_impl(); | ||
|
|
||
| let generic_type_ids = ast.generics.type_params().map(|ty| { | ||
| let ty_ident = &ty.ident; | ||
| quote! { | ||
| :: #scale_info ::meta_type::<#ty_ident>() | ||
| #scale_info::meta_type::<#ty_ident>() | ||
|
||
| } | ||
| }); | ||
|
|
||
|
|
@@ -107,22 +106,21 @@ fn generate_type(input: TokenStream2) -> Result<TokenStream2> { | |
| let docs = generate_docs(&ast.attrs); | ||
|
|
||
| let type_info_impl = quote! { | ||
| impl #impl_generics :: #scale_info ::TypeInfo for #ident #ty_generics #where_clause { | ||
| impl #impl_generics #scale_info ::TypeInfo for #ident #ty_generics #where_clause { | ||
| type Identity = Self; | ||
| fn type_info() -> :: #scale_info ::Type { | ||
| :: #scale_info ::Type::builder() | ||
| .path(:: #scale_info ::Path::new(::core::stringify!(#ident), ::core::module_path!())) | ||
| .type_params(:: #scale_info ::prelude::vec![ #( #generic_type_ids ),* ]) | ||
| fn type_info() -> #scale_info::Type { | ||
| #scale_info::Type::builder() | ||
| .path(#scale_info::Path::new(::core::stringify!(#ident), ::core::module_path!())) | ||
| .type_params(#scale_info::prelude::vec![ #( #generic_type_ids ),* ]) | ||
| #docs | ||
| .#build_type | ||
| } | ||
| } | ||
| }; | ||
| }; | ||
|
|
||
| Ok(quote! { | ||
| #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] | ||
| const _: () = { | ||
| #type_info_impl; | ||
| #type_info_impl | ||
| }; | ||
| }) | ||
| } | ||
|
|
@@ -140,6 +138,25 @@ fn crate_name_ident(name: &str) -> Result<Ident> { | |
| .map_err(|e| syn::Error::new(Span::call_site(), &e)) | ||
| } | ||
|
|
||
| /// Find the name given to the `scale-info` crate in the context we run in. | ||
| /// If scale-info is not among the dependencies then we must be deriving | ||
| /// types for the scale-info crate itself, in which case we need to rename | ||
| /// "self" to something, so the object paths keep working. | ||
dvdplm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| fn scale_info_crate_tokens() -> Result<TokenStream2> { | ||
dvdplm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| use proc_macro_crate::FoundCrate; | ||
| const SCALE_INFO_CRATE_NAME: &str = "scale-info"; | ||
|
|
||
| let crate_ident = match proc_macro_crate::crate_name(SCALE_INFO_CRATE_NAME) { | ||
| Ok(FoundCrate::Itself) => quote! { crate }, | ||
| Ok(FoundCrate::Name(name)) => { | ||
| let ident = Ident::new(&name, Span::call_site()); | ||
| quote! { :: #ident } | ||
| } | ||
| Err(e) => return Err(syn::Error::new(Span::call_site(), e)), | ||
| }; | ||
| Ok(crate_ident) | ||
| } | ||
|
|
||
| type FieldsList = Punctuated<Field, Comma>; | ||
|
|
||
| fn generate_fields(fields: &FieldsList) -> Vec<TokenStream2> { | ||
|
|
@@ -204,7 +221,10 @@ fn clean_type_string(input: &str) -> String { | |
| .replace("&\'", "&'") | ||
| } | ||
|
|
||
| fn generate_composite_type(data_struct: &DataStruct, scale_info: &Ident) -> TokenStream2 { | ||
| fn generate_composite_type( | ||
| data_struct: &DataStruct, | ||
| scale_info: &TokenStream2, | ||
| ) -> TokenStream2 { | ||
| let fields = match data_struct.fields { | ||
| Fields::Named(ref fs) => { | ||
| let fields = generate_fields(&fs.named); | ||
|
|
@@ -221,13 +241,16 @@ fn generate_composite_type(data_struct: &DataStruct, scale_info: &Ident) -> Toke | |
| } | ||
| }; | ||
| quote! { | ||
| composite(:: #scale_info ::build::Fields::#fields) | ||
| composite(#scale_info::build::Fields::#fields) | ||
| } | ||
| } | ||
|
|
||
| type VariantList = Punctuated<Variant, Comma>; | ||
|
|
||
| fn generate_c_like_enum_def(variants: &VariantList, scale_info: &Ident) -> TokenStream2 { | ||
| fn generate_c_like_enum_def( | ||
| variants: &VariantList, | ||
| scale_info: &TokenStream2, | ||
| ) -> TokenStream2 { | ||
| let variants = variants | ||
| .into_iter() | ||
| .enumerate() | ||
|
|
@@ -246,7 +269,7 @@ fn generate_c_like_enum_def(variants: &VariantList, scale_info: &Ident) -> Token | |
| }); | ||
| quote! { | ||
| variant( | ||
| :: #scale_info ::build::Variants::new() | ||
| #scale_info::build::Variants::new() | ||
| #( #variants )* | ||
| ) | ||
| } | ||
|
|
@@ -259,7 +282,10 @@ fn is_c_like_enum(variants: &VariantList) -> bool { | |
| variants.iter().all(|v| matches!(v.fields, Fields::Unit)) | ||
| } | ||
|
|
||
| fn generate_variant_type(data_enum: &DataEnum, scale_info: &Ident) -> TokenStream2 { | ||
| fn generate_variant_type( | ||
| data_enum: &DataEnum, | ||
| scale_info: &TokenStream2, | ||
| ) -> TokenStream2 { | ||
| let variants = &data_enum.variants; | ||
|
|
||
| if is_c_like_enum(variants) { | ||
|
|
@@ -279,20 +305,20 @@ fn generate_variant_type(data_enum: &DataEnum, scale_info: &Ident) -> TokenStrea | |
| Fields::Named(ref fs) => { | ||
| let fields = generate_fields(&fs.named); | ||
| quote! { | ||
| :: #scale_info::build::Fields::named() | ||
| #scale_info::build::Fields::named() | ||
| #( #fields )* | ||
| } | ||
| } | ||
| Fields::Unnamed(ref fs) => { | ||
| let fields = generate_fields(&fs.unnamed); | ||
| quote! { | ||
| :: #scale_info::build::Fields::unnamed() | ||
| #scale_info::build::Fields::unnamed() | ||
| #( #fields )* | ||
| } | ||
| } | ||
| Fields::Unit => { | ||
| quote! { | ||
| :: #scale_info::build::Fields::unit() | ||
| #scale_info::build::Fields::unit() | ||
| } | ||
| } | ||
| }; | ||
|
|
@@ -308,7 +334,7 @@ fn generate_variant_type(data_enum: &DataEnum, scale_info: &Ident) -> TokenStrea | |
| }); | ||
| quote! { | ||
| variant( | ||
| :: #scale_info ::build::Variants::new() | ||
| #scale_info ::build::Variants::new() | ||
| #( #variants )* | ||
| ) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to keep this optional crate feature? What purpose does it have?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is a good question. I fail to see the reason to keep this as a feature. @ascjones you ok making it non-optional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just saw Giles's PR with this stuff in and fwiw I think it doesn't need to be behind a feature flag.