Skip to content
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

Remove type parameter defaults #71

Merged
merged 5 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 3 additions & 9 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
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,15 +97,13 @@ fn generate_type(input: TokenStream2) -> Result<TokenStream2> {
}
});

let 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")),
};
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
5 changes: 5 additions & 0 deletions derive/src/trait_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ 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
25 changes: 25 additions & 0 deletions test_suite/tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,31 @@ fn scale_compact_types_work_in_enums() {
assert_type!(MutilatedMultiAddress<u8, u16>, ty);
}

#[test]
fn type_parameters_with_default_bound_works() {
trait Formy {
type Tip;
}
#[derive(TypeInfo)]
pub enum MetaFormy {}
impl Formy for MetaFormy {
type Tip = u8;
}

#[allow(unused)]
#[derive(TypeInfo)]
struct Bat<TTT: Formy = MetaFormy> {
one: TTT,
}

let ty = Type::builder()
.path(Path::new("Bat", "derive"))
.type_params(tuple_meta_type!(MetaFormy))
.composite(Fields::named().field_of::<MetaFormy>("one", "TTT"));

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

#[test]
fn whitespace_scrubbing_works() {
#[allow(unused)]
Expand Down