Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Generate unique type parameter in procedural derive macro

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading