I have this macro code:
pub fn from_le_bytes(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = input.ident;
let generics = add_trait_bounds(input.generics, &parse_quote!(le_stream::FromLeBytes));
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
let body = impl_body(&input.data);
let expanded = quote! {
impl #impl_generics le_stream::FromLeBytes for #name #ty_generics #where_clause {
fn from_le_bytes<T>(bytes: &mut T) -> le_stream::Result<Self>
where
T: std::iter::Iterator<Item = u8>
{
#body
}
}
};
proc_macro::TokenStream::from(expanded)
}
The issue is, that sometimes in my code I also tend to use the generic type parameter T, which then will clash with the macro.
Is there a way to generate a unique type parameter name in the macro that is guaranteed to not clash with any type parameters in the code?
I don't think there is a way, commonly crates just prefix their type parameters with
__(double underscore), that makes them way less likely to clash with user parameters. Here is an example fromserde_derive: