Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use the ImplGeneric from split_for_impl instead of custom munging
Avoid cloning&parsing the ast
  • Loading branch information
dvdplm committed Mar 5, 2021
commit 68d9a86ebcb8620b57a50b960712e453e7d01806
19 changes: 4 additions & 15 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#![cfg_attr(not(feature = "std"), no_std)]
// #![cfg_attr(not(feature = "std"), no_std)]
Copy link
Contributor

Choose a reason for hiding this comment

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

?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ouch, my bad.

But while we're at it: we should probably remove this anyway. The derive crate doesn't need to be no-std I think (only its output must be no-std). And as we're using proc-macro-crate already it's perhaps good to acknowledge the fact that scale-info-derive isn't actually no-std?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I'm not sure why that's there in the first place, but if we remove it should not be hidden in this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See #69 (comment) – I removed it in that PR.


extern crate alloc;
extern crate proc_macro;
Expand Down Expand Up @@ -74,18 +74,14 @@ fn generate(input: TokenStream2) -> Result<TokenStream2> {
}

fn generate_type(input: TokenStream2) -> Result<TokenStream2> {
let mut ast: DeriveInput = syn::parse2(input.clone())?;
let ast: DeriveInput = syn::parse2(input.clone())?;

let scale_info = crate_name_ident("scale-info")?;
let parity_scale_codec = crate_name_ident("parity-scale-codec")?;

let ident = &ast.ident;

ast.generics
.lifetimes_mut()
.for_each(|l| *l = parse_quote!('static));

let (_, ty_generics, _) = ast.generics.split_for_impl();
let (impl_generics, ty_generics, _) = ast.generics.split_for_impl();
let where_clause = trait_bounds::make_where_clause(
ident,
&ast.generics,
Expand All @@ -101,20 +97,13 @@ fn generate_type(input: TokenStream2) -> Result<TokenStream2> {
}
});

let mut ast: DeriveInput = syn::parse2(input.clone())?;
let build_type = match &ast.data {
Data::Struct(ref s) => generate_composite_type(s, &scale_info),
Data::Enum(ref e) => generate_variant_type(e, &scale_info),
Data::Union(_) => return Err(Error::new_spanned(input, "Unions not supported")),
};

// Remove any type parameter defaults, we don't want those. E.g. `impl<T: Stuff = WutEven>`
ast.generics.type_params_mut().for_each(|type_param| {
type_param.default = None;
});
let generic_types = ast.generics.type_params();
let type_info_impl = quote! {
impl <#( #generic_types ),*> :: #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()
Expand Down
3 changes: 3 additions & 0 deletions derive/src/trait_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ pub fn make_where_clause<'a>(
predicates: Punctuated::new(),
}
});
for lifetime in generics.lifetimes() {
where_clause.predicates.push(parse_quote!(#lifetime: 'static))
}

let type_params = generics.type_params();
let ty_params_ids = type_params
Expand Down
1 change: 1 addition & 0 deletions test_suite/tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ fn type_parameters_with_default_bound_works() {
.path(Path::new("Bat", "derive"))
.type_params(tuple_meta_type!(MetaFormy))
.composite(Fields::named().field_of::<MetaFormy>("one", "TTT"));

assert_type!(Bat<MetaFormy>, ty);
}

Expand Down