我正在使用 Iron 框架来创建一个简单的端点。我有端点需要访问的有状态的、可变的数据。
这是一些显示我意图的代码:
extern crate iron;
extern crate mount;
use iron::{Iron, Request, Response, IronResult};
use iron::status;
use mount::Mount;
static mut s_counter: Option<Counter> = None;
struct Counter {
pub count: u8
}
impl Counter {
pub fn new() -> Counter {
Counter {
count: 0
}
}
pub fn inc(&mut self) {
self.count += 1;
}
}
fn main() {
unsafe { s_counter = Some(Counter::new()); }
let mut mount = Mount::new();
mount.mount("/api/inc", inc);
println!("Server running on http://localhost:3000");
Iron::new(mount).http("127.0.0.1:3000").unwrap();
}
fn inc(req: &mut Request) -> IronResult<Response> {
let mut counter: Counter;
unsafe {
counter = match s_counter {
Some(counter) => counter,
None => { panic!("counter not initialized"); }
};
}
counter.inc();
let resp = format!("{}", counter.count);
Ok(Response::with((status::Ok, resp)))
}
此代码无法编译:
error: cannot move out of static item
我希望有更好的方法来做到这一点,不涉及任何不安全的代码或static mut
. 我的问题是,实现这一目标的惯用方法是什么?