Macro that receives the entire crate as a TokenStream
⚓ Rust 📅 2025-09-20 👤 surdeus 👁️ 7You can do something like this:
#[duration_units]
fn foo() -> Duration {
30min + 20sec * 3
}
Let's say you wanted to use this syntax everywhere in your crate. You would have to annotate every function with this attribute!
Inner attributes make life a little easier. You could have a #![duration_units] and put it at the top of every module. Then it just applies the same transformation
What about if you didnt even need to do that? What if we had "Crate macros" that receive the entire crate as a single TokenStream and transformed it into another TokenStream
Does anyone know if it's possible to hack this with e.g. build.rs or something? Not sure if that's possible but worth thinking about - if anyone has some ideas.
With crate macros you could fully extend Rust's syntax in very interesting ways - e.g. adding custom literals (as much as the parser allows it, so no custom operators)
6 posts - 3 participants
🏷️ Rust_feed