Generate unique type parameter in procedural derive macro

30 Views Asked by At

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?

1

There are 1 best solutions below

0
cafce25 On BEST ANSWER

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 from serde_derive:

fn serialize<__S>(&self, __serializer: __S) -> _serde::__private::Result<__S::Ok, __S::Error>
where
    __S: _serde::Serializer,
{
    // Elements that have skip_serializing will be unused.
    #[allow(unused_variables)]
    let (#(#fields_ident,)*) = self.data;
    #inner
}