Take the example below:
static COUNT: AtomicUsize = AtomicUsize::new(0);
#[proc_macro_attribute]
pub fn count_usages(_attr: TokenStream, item: TokenStream) -> TokenStream {
let c = COUNT.fetch_add(1, Ordering::AcqRel);
println!("Do stuff with c: {}", c);
item
}
Although the order attributes are processed may differ, will the final count be the same each time for cases such as:
A practical use case (mine in particular) is generating a compile-time pseudo-static variable memory layout that will be reused in multiple memory managers within a statically linked executable.
While some things might work with the current implementation, procedural macros should not use global variables and expect them to be preserved between calls.
There is currently no official mechanism for storing state between invocations of a procedural macro.
The issue “Crate local state for procedural macros?” mentions some of these points:
do_it!
needs data from all config!
invocations, that's a problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.