Call a proc macro like a normal function

Is it possible to call an exported proc macro "as a normal function"? That means, give it TokenStream and get a TokenStream back?

This is useful when one proc macro needs to "call" another proc macro and modify it's output, but there could be other use-cases as well.

Yes, these proc macros are called function-like procedural macros and can be called like declarative macros.

Misread your question, my bad.

I don't believe so. I think they get exported only as macros, not as "real" functions.

It's worth remembering that macro crates are not linked into your code like normal crates are, they're loaded by the compiler.

1 Like

You could possibly write your proc macro as two crates, one of which has the proc-macro type and defers its implementation to the other which is a lib that operates on proc_macro2::TokenStream. It's certainly not ideal, but...

1 Like

That's the best workaround, but I was wondering if there is a better way, or if it's planned in the future.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.