Now that proc_macros have been stabilized, how does one create such a thing?
From what I've seen, there's the option of putting a #[proc_macro_attribute] annotation on a fn whatsitsname(attrs: TokenStream, code: TokenStream) -> TokenStream, but how can I register it? How can I add custom attributes?
The Rust compiler has a fairly complete test suite. When looking for examples of newly-introduced features, I frequently start there:
Here's a fully worked example:
Cargo.toml
We add a dependency on our macro-defining crate.
src/main.rs
We import the attribute macro and add it to a function.
my_macro/Cargo.toml
We specify
crate_typeasproc_macro.my_macro/src/lib.rs
We add
#[proc_macro_attribute]to each function that should be a macro.cargo run
The "hard" part is wrangling the
TokenStreaminto something useful and then outputting something equally useful. The crates syn and quote are the current gold standards for those two tasks. Dealing withTokenStreamis covered in the macros chapter of The Rust Programming Language as well as API documentation.There's also
#[proc_macro], which takes functions of the form:And can be invoked as
the_name_of_the_macro!(...).