如何存储和使用`Arc <dyn FnMut>`?

时间:2019-08-13 17:23:26

标签: rust closures reference-counting

我不知道如何在枚举中存储FnMut。我使用std::sync::Arc#[derive(Clone)]

#[allow(non_camel_case_types)]
#[derive(Hash,PartialEq,Eq,Clone,Debug)]
struct _lua_data {
    val: std::sync::Arc<_lua_data_unpack>,
}
...
#[allow(non_camel_case_types)]
#[derive(Hash,PartialEq,Eq,Debug,Clone)]
enum _lua_data_unpack {
    Function(_lua_data_unpack_function),
    ...
}
#[allow(non_camel_case_types)]
#[derive(Clone)]
struct _lua_data_unpack_function {
    val: std::sync::Arc<FnMut(Vec<_lua_data>) -> _lua_data>,
}
...
fn _lua_call(f: _lua_data, args: Vec<_lua_data>) -> _lua_data {
    if let _lua_data_unpack::Function(v) = (*f.clone().val).clone() {
        let mut a: std::sync::Arc<FnMut(Vec<_lua_data>) -> _lua_data> = v.val.clone();
        (&mut a)(args).clone()
    } else {
        panic!("attempt to call a value that isn't a function")
    }
}

Complete code without _lua_call

error[E0596]: cannot borrow data in a `&` reference as mutable
   --> prelude.rs:123:9
    |
123 |         (&mut a)(args).clone()
    |         ^^^^^^^^ cannot borrow as mutable

我在哪里错了?

0 个答案:

没有答案