我试图通过使用静态变量的C回调提供闭包。我能够使用Fn
类型工作,但我想通过FnMut
使其工作,为库的用户提供更多功能。
这就是我所拥有的:
lazy_static! {
static ref CALLBACK: Mutex<RefCell<Box<FnMut(Result<&str>) + Send>>> = Mutex::new(RefCell::new(Box::new(|_|())));
}
fn wrap_cb<F: Fn(Result<&str>)>(f: Option<F>) -> Option<unsafe extern "C" fn(*mut c_char, size_t)> {
match f {
Some(_) => {
unsafe extern "C" fn wrapped(msg: *mut c_char, len: size_t) {
let s = std::str::from_utf8(std::slice::from_raw_parts(msg as *const u8, len))
.map_err(Error::from);
let x = CALLBACK.lock().unwrap();
x.borrow_mut()(s);
}
Some(wrapped)
}
None => None,
}
}
这给出了错误:
error[E0596]: cannot borrow immutable `Box` content as mutable
--> src/wpactrl.rs:56:17
|
56 | x.borrow_mut()(s);
| ^^^^^^^^^^^^^^ cannot borrow as mutable
看起来“无法借用不可变的Box内容为可变”的问题减少为:
fn invoke(m: &Mutex<RefCell<Box<FnMut()>>>) {
let r = m.lock().unwrap();
r.borrow_mut()();
}
我还没弄清楚为什么会这样,但如果改为:
fn invoke(m: &Mutex<RefCell<Box<FnMut()>>>) {
let r = m.lock().unwrap();
let f = &mut *r.borrow_mut();
f();
}