Sci*_*eSE 5 closures ffi traits rust
我正在使用Rust中的WinAPI,并且有一些函数(例如EnumWindows())需要回调.回调通常接受一个额外的参数(类型LPARAM是别名i64),您可以使用它将一些自定义数据传递给回调.
我已经将Vec<T>对象作为LPARAM 发送到WinAPI回调,它工作正常.例如,在我的情况下lparam,将一个值"解包" 到一个Vec<RECT>看起来像这样:
unsafe extern "system" fn enumerate_callback(hwnd: HWND, lparam: LPARAM) -> BOOL {
let rects = lparam as *mut Vec<RECT>;
}
Run Code Online (Sandbox Code Playgroud)
我现在必须传递一个闭包,而不是传递一个向量.我不能使用函数指针,因为我的闭包必须捕获一些变量,如果我使用了函数则无法访问它们.在C++中,我会std::function<>用于我的特定任务,我认为在Rust中相应的抽象是一个闭包.
我的解包代码如下所示:
unsafe extern "system" fn enumerate_callback(hwnd: HWND, lparam: LPARAM) -> BOOL {
let cb: &mut FnMut(HWND) -> bool = &mut *(lparam as *mut c_void as *mut FnMut(HWND) -> bool);
// ...
}
Run Code Online (Sandbox Code Playgroud)
SSCCE:
use std::os::raw::c_void;
fn enum_wnd_proc(some_value: i32, lparam: i32) {
let closure: &mut FnMut(i32) -> bool =
unsafe { (&mut *(lparam as *mut c_void as *mut FnMut(i32) -> bool)) };
println!("predicate() executed and returned: {}", closure(some_value));
}
fn main() {
let sum = 0;
let mut closure = |some_value: i32| -> bool {
sum += some_value;
sum >= 100
};
let lparam = (&mut closure as *mut c_void as *mut FnMut(i32) -> bool) as i32;
enum_wnd_proc(20, lparam);
}
Run Code Online (Sandbox Code Playgroud)
(游乐场)
我收到这些错误:
error[E0277]: expected a `std::ops::FnMut<(i32,)>` closure, found `std::ffi::c_void`
--> src/main.rs:5:26
|
5 | unsafe { (&mut *(lparam as *mut c_void as *mut FnMut(i32) -> bool)) };
| ^^^^^^^^^^^^^^^^^^^^^ expected an `FnMut<(i32,)>` closure, found `std::ffi::c_void`
|
= help: the trait `std::ops::FnMut<(i32,)>` is not implemented for `std::ffi::c_void`
= note: required for the cast to the object type `dyn std::ops::FnMut(i32) -> bool`
error[E0606]: casting `&mut [closure@src/main.rs:12:23: 15:6 sum:_]` as `*mut std::ffi::c_void` is invalid
--> src/main.rs:17:19
|
17 | let lparam = (&mut closure as *mut c_void as *mut FnMut(i32) -> bool) as i32;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0606]: casting `*mut dyn std::ops::FnMut(i32) -> bool` as `i32` is invalid
--> src/main.rs:17:18
|
17 | let lparam = (&mut closure as *mut c_void as *mut FnMut(i32) -> bool) as i32;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: cast through a thin pointer first
error[E0277]: expected a `std::ops::FnMut<(i32,)>` closure, found `std::ffi::c_void`
--> src/main.rs:17:19
|
17 | let lparam = (&mut closure as *mut c_void as *mut FnMut(i32) -> bool) as i32;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected an `FnMut<(i32,)>` closure, found `std::ffi::c_void`
|
= help: the trait `std::ops::FnMut<(i32,)>` is not implemented for `std::ffi::c_void`
= note: required for the cast to the object type `dyn std::ops::FnMut(i32) -> bool`
Run Code Online (Sandbox Code Playgroud)
我想知道:
i64值以将其传递给该回调的正确方法是什么?我正在使用Rust的稳定版本.
She*_*ter 10
首先,代码的一些逻辑错误:
在许多平台(如64位)上转换指针是不正确的i32.指针可以使用所有这些位.截断指针然后在截断的地址处调用函数将导致Really Bad Things.通常,您希望使用机器大小的整数(usize或isize).
该sum值必须是可变的.
问题的关键在于闭包是具体类型,它们占用了程序员不知道的大小,但编译器已知.C函数仅限于采用机器大小的整数.
因为闭包实现了其中一个Fn*特征,所以我们可以引用闭包的特征实现来生成特征对象.引用一个特征会导致胖指针包含两个指针大小的值.在这种情况下,它包含一个指向已关闭数据的指针和一个指向vtable的指针,这是实现该特征的具体方法.
一般来说,为了或任何参考Box一个的动态大小的类型类型是要产生一个脂肪指针.
在64位计算机上,胖指针总共为128位,并将其转换为机器大小的指针会再次截断数据,从而导致真正糟糕的事情发生.
与计算机科学中的其他所有内容一样,解决方案是添加更多抽象层:
use std::os::raw::c_void;
fn enum_wnd_proc(some_value: i32, lparam: usize) {
let trait_obj_ref: &mut &mut FnMut(i32) -> bool = unsafe {
let closure_pointer_pointer = lparam as *mut c_void;
&mut *(closure_pointer_pointer as *mut _)
};
println!(
"predicate() executed and returned: {}",
trait_obj_ref(some_value)
);
}
fn main() {
let mut sum = 0;
let mut closure = |some_value: i32| -> bool {
println!("I'm summing {} + {}", sum, some_value);
sum += some_value;
sum >= 100
};
let mut trait_obj: &mut FnMut(i32) -> bool = &mut closure;
let trait_obj_ref = &mut trait_obj;
let closure_pointer_pointer = trait_obj_ref as *mut _ as *mut c_void;
let lparam = closure_pointer_pointer as usize;
enum_wnd_proc(20, lparam);
}
Run Code Online (Sandbox Code Playgroud)
我们对fat指针进行第二次引用,它会创建一个瘦指针.该指针只有一个整数的机器整数.
也许图表会有所帮助(或受到伤害)?
Reference -> Trait object -> Concrete closure
8 bytes 16 bytes ?? bytes
Run Code Online (Sandbox Code Playgroud)
因为我们正在使用原始指针,所以现在程序员有责任确保闭包的使用时间超过了它!如果enum_wnd_proc将指针存储在某处,则必须非常小心,在关闭时不使用它.
作为旁注,mem::transmute在投射特征对象时使用:
use std::mem;
let closure_pointer_pointer: *mut c_void = unsafe { mem::transmute(trait_obj) };
Run Code Online (Sandbox Code Playgroud)
产生更好的错误消息:
error[E0512]: transmute called with types of different sizes
--> src/main.rs:26:57
|
26 | let closure_pointer_pointer: *mut c_void = unsafe { mem::transmute(trait_obj) };
| ^^^^^^^^^^^^^^
|
= note: source type: &mut dyn std::ops::FnMut(i32) -> bool (128 bits)
= note: target type: *mut std::ffi::c_void (64 bits)
Run Code Online (Sandbox Code Playgroud)
也可以看看
| 归档时间: |
|
| 查看次数: |
1766 次 |
| 最近记录: |