简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM