The only way I have found to do this is with an unsafe singleton function:
fn singleton() -> &'static mut HashSet<String> {
static mut hash_map: *mut HashSet<String> = 0 as *mut HashSet<String>;
let map: HashSet<String> = HashSet::new();
unsafe {
if hash_map == 0 as *mut HashSet<String> {
hash_map = mem::transmute(Box::new(map));
}
&mut *hash_map
}
}
Is there a better way? Perhaps we could do something in the plugin_registrar
function?
By global mutable state, I mean a mutable variable that can be used by multiple procedural macros and/or attributes.
Update:
I am writing a compiler plugin to provide a sql!
procedural macro. I have a #[sql_table]
attribute to be used on structs so that I can get the name and columns of the SQL table.
I need global mutable state to save the name and fields of the struct
in the attribute, so that the procedural macro can check that all the identifiers exist.