【发布时间】:2015-09-13 23:40:18
【问题描述】:
我发现这样做的唯一方法是使用不安全的单例函数:
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
}
}
有没有更好的方法?或许我们可以在plugin_registrar 函数中做点什么?
全局可变状态是指一个可变变量,可以被多个过程宏和/或属性使用。
更新:
我正在编写一个编译器插件来提供一个sql! 过程宏。我有一个 #[sql_table] 属性可用于结构,以便我可以获取 SQL 表的名称和列。
我需要全局可变状态来保存属性中struct的名称和字段,以便程序宏可以检查所有标识符是否存在。
【问题讨论】:
-
你的意思是plugin for the Rust compiler,比如语法扩展?
-
是的,我的意思是 Rust 编译器的插件。
标签: rust