sak
sak

Reputation: 3297

Is there any way to supply generic arguments to a function-like proc macro?

So let's say I have a proc macro defined like this:

#[proc_macro]
pub fn my_macro(input: TokenStream) -> TokenStream { ... }

Is there any way to introduce a generic argument? For instance, I would like to be able to do something like this:

#[proc_macro]
pub fn my_macro<T>(input: TokenStream) -> TokenStream { 

    ...
    T::do_something_with(input);
    ...
}

And at the call site:

my_macro::<Foo>! { some input }

Is there any way to achieve something like this?

Upvotes: 2

Views: 71

Answers (1)

Kevin Reid
Kevin Reid

Reputation: 43782

No, this is not possible, for two reasons:

  • The syntax for function-like proc macros does not include generic parameters.
  • If there was such a thing, the generic parameters would necessarily come as annother TokenStream — not something you can call a function on. The types of the program where the macro is used don't exist yet — they're still being parsed.

Upvotes: 4

Related Questions