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

[feature]Support insert method call #274

Closed
yyy33 opened this issue Jun 2, 2024 · 2 comments
Closed

[feature]Support insert method call #274

yyy33 opened this issue Jun 2, 2024 · 2 comments

Comments

@yyy33
Copy link

yyy33 commented Jun 2, 2024

// You can see that every time I interpolate types I need to define an extra variable
//and then clone it
fn automap(n: usize) -> TokenStream2 {
    let types = (1..=n).map(|n| format_ident!("T{}", n));
    let types2 = types.clone();
    let fields = (1..=n).map(|n| format_ident!("map{}", n));
    quote! {
        pub struct AutoMap<#(#types),*> {
            #( #fields: BaseMap<#types2> ),*
        }
    }
}
//I want to define an additional gen_types function and then call it when interpolating
fn automap(n: usize) -> TokenStream2 {
    let fields = (1..=n).map(|n| format_ident!("map{}", n));
    quote! {
        //`#gen_types(n)` call the `gen_types(n)` and insert the result
        pub struct AutoMap<#gen_types(n)> {
            #( #fields: BaseMap<#gen_types(n)> ),*
        }
    }
}

fn gen_types(n: usize) -> TokenStream2 {
    let i = (1..=n).map(|n| format_ident!("T{}", n));
    quote!{ #(#i),* }
}
@dtolnay
Copy link
Owner

dtolnay commented Jun 2, 2024

I would prefer not to build this into this crate. I think the original snippet using types.clone() is all right as is.

You can also write let types: Vec<_> = (1..=n).map(...).collect(); and use #types twice.

@dtolnay dtolnay closed this as not planned Won't fix, can't repro, duplicate, stale Jun 2, 2024
@yyy33
Copy link
Author

yyy33 commented Jun 2, 2024

let types: Vec<_> = (1..=n).map(...).collect();

It worked. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants