dtolnay / quote

Rust quasi-quoting
Apache License 2.0
1.32k stars 90 forks source link

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

Closed arafatrahman862 closed 1 year ago

arafatrahman862 commented 1 year ago

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 commented 1 year ago

I would prefer not to support this in this crate.