Box::into_raw(Box::new(Box::new(func)));
= note: expected type `()`
found type `*mut std::boxed::Box<F>`
let func: &mut Box<FnMut(i32) -> ()> = transmute(user);
// Trait object with a stable address
let func = Box::new(func) as Box<FnMut(i32)>;
// Thin pointer
let func = Box::new(func);
// Raw pointer
let func = Box::into_raw(func);
Box<FnMut(i32) -> ()>
返回类型
()
Box<FnMut(i32)>
让func:&mut box<fnmut(i32)->()>=transmute(用户);
很难避免
transmute
extern "C" fn c_callback(data: i32, user: *mut libc::c_void) {
let user = user as *mut Box<FnMut(i32)>;
unsafe {
(*user)(data);
}
}
类型别名
type CallbackFn = Box<FnMut(i32)>;
let user = user as *mut CallbackFn;
let func = Box::new(func) as CallbackFn;
另请参见: