不能将不可变的'Box'内容借用为可变的

问题描述 投票:2回答:1

我试图通过使用静态变量的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
static rust closures mutex
1个回答
2
投票

看起来“无法借用不可变的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();
}
© www.soinside.com 2019 - 2024. All rights reserved.