r/rust•
JackHackee

Is there comptime reflection in Rust proc-macro?

Rust compiler is surely powerful. However, sometimes we have more business knowledge that the compiler is not aware of. The business knowledge can be used to rewrite part of the program but one has to do it manually. I know of proc-macro, but sometimes I need to pass values between different invocations, inspect fields of rust and generate different sets of code. Surely I can derive struts, but I would like to do interstruct things in proc-macro. I tried to parse struct definition and save it in a global variable but unfortunately, each invocation is independent. I know several dirty ways to do this, like using filesystems and local webserver. Are there better ways to achieve comptime reflection?

AFAIK, mang languages like Zig, C++, and Scala can achieve it.

Posted Mar 11, 2023, 1:49 AM UTC

2111
Loading comments…