以下示例是我发现的一个最小示例,可以解释我遇到的问题:
use std::borrow::BorrowMut;
use std::ops::DerefMut;
#[derive(Debug, Clone)]
enum ConnectionState {
NotStarted,
}
type StateChangedCallback = Box<FnMut(ConnectionState) + Send + Sync>;
fn thread_func(mut on_state_changed: StateChangedCallback) {
let new_state = ConnectionState::NotStarted;
let f: &mut BorrowMut<StateChangedCallback> = &mut on_state_changed;
f.borrow_mut().deref_mut()(new_state);
}
fn main() {
let on_state_changed = Box::new(|new_state| {
println!("New state: {:?}", new_state);
});
let join_handle = std::thread::spawn(|| thread_func(on_state_changed));
join_handle.join().unwrap();
}
我有一个简单的线程,需要调用从main传递的回调。回调是签名Box<FnMut(ConnectionState) + Send + Sync>
,因为我想多次调用它。我设法调用回调的唯一方法是使用以下奇怪的语法:
let f: &mut BorrowMut<StateChangedCallback> = &mut on_state_changed;
f.borrow_mut().deref_mut()(new_state);
我进行了搜索,但没有找到合理的解释。我做错了吗?还是Rust的工作方式?
如果是这样,有人可以解释这种语法的原因吗?
答案 0 :(得分:1)
您使事情复杂化了。
您可能会解释,为什么会这样,因为签名中不涉及借贷,因此您必须borrow_mut()
。
您的函数thread_func
可以简化为:
fn thread_func(mut on_state_changed: StateChangedCallback) {
let new_state = ConnectionState::NotStarted;
on_state_changed(new_state);
}
请注意,与您的句子“我想多次调用它(回调)” 相反,您不能这样做,因为将闭包移动到了函数中。