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

quote! and quote_spanned! macro should take &mut TokenStream #255

Closed
arafatrahman862 opened this issue Jun 5, 2023 · 1 comment
Closed

Comments

@arafatrahman862
Copy link

Hey! dtolnay, Thanks for your great work! ❤️

I think quote! and quote_spanned! macro should take &mut TokenStream instead of creating new one.

Instead of this:

let import = items.iter().map(|item| quote_spanned!(item.span()=> use #item));
quote! { 
    #(#import)* 
    // ...
}

I think it should look something like this:

// `to_tokens_fn` accept closure and return a struct that implement `ToTokens`

let import = to_tokens_fn(|tokens| { 
    for item in items {
        quote_spanned!(tokens, item.span()=> use #item);
    }
});

impl ToTokens for <Struct> {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        quote!(tokens, { 
            #import
            // ...
        })
    }
}

This approach should be more performance, Because it avoid cloning.

Sorry if this is duplicate.

@dtolnay
Copy link
Owner

dtolnay commented Jun 5, 2023

I would prefer not to support this in this crate.

@dtolnay dtolnay closed this as completed Jun 5, 2023
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