The Token Stream
What a proc macro actually receives, and the crate layout it demands.
A procedural macro is a function that takes tokens and returns tokens. It runs at compile time, in its own crate:
[lib]
proc-macro = true
[dependencies]
syn = { version = "2", features = ["full"] }
quote = "1"
proc-macro2 = "1"
#[proc_macro_derive(Describe)]
pub fn derive_describe(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
// ...
}
The separate crate is not a style choice โ a proc-macro = true crate can only export macros,
because it is compiled for the host while your program is compiled for the target.
cargo expand shows you the generated code. You will use it constantly.