gpt4 book ai didi

rust - 如何通过原始指针将闭包作为参数传递给 C 函数?

转载 作者:行者123 更新时间:2023-11-29 07:49:30 24 4
gpt4 key购买 nike

我在 Rust 中使用 WinAPI,有些函数(如 EnumWindows() )需要回调。回调通常接受一个额外的参数(类型 LPARAMi64 的别名),您可以使用它来将一些自定义数据传递给回调。

我已经发送了Vec<T>对象作为 WinAPI 回调的 LPARAM,它工作正常。例如“解包”lparam Vec<RECT> 的值在我的例子中看起来像这样:

unsafe extern "system" fn enumerate_callback(hwnd: HWND, lparam: LPARAM) -> BOOL {
let rects = lparam as *mut Vec<RECT>;
}

我现在必须传递闭包,而不是传递向量。我不能使用函数指针,因为我的闭包必须捕获一些变量,如果我使用函数,这些变量将无法访问。在 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);
// ...
}

中南合作:

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);
}

( Playground )

我收到这些错误:

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`

我想知道:

  1. 有没有办法将一个函数/闭包传递给另一个函数并执行那些“类 C”转换?
  2. 将闭包转换为 i64 的正确方法是什么?将其传递给该回调的值(value)?

我使用的是稳定版的 Rust。

最佳答案

首先,代码有一些逻辑错误:

  1. 在许多平台(如 64 位)上将指针转换为 i32不正确的。指针可以使用所有这些位。截断指针然后在截断的地址调用函数将导致真正糟糕的事情。通常您希望使用机器大小的整数(usizeisize)。

  2. sum 值需要可变。

问题的实质是闭包是具体类型,占用的大小对于程序员来说是未知的,但对于编译器来说是已知的。 C 函数仅限于采用机器大小的整数。

因为闭包实现了 Fn* 特征之一,我们可以引用闭包对该特征的实现来生成一个特征对象。引用一个特征会导致一个胖指针,它包含两个指针大小的值。在这种情况下,它包含一个指向封闭数据的指针和一个指向 vtable 的指针,即实现特征的具体方法。

一般来说,对 dynamically-sized type type 的任何引用或 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);
}

我们对胖指针进行第二次引用,这会创建一个瘦指针。这个指针只有一个机器整数大小。

也许图表会有所帮助(或有害)?

Reference -> Trait object -> Concrete closure
8 bytes 16 bytes ?? bytes

因为我们使用的是原始指针,所以现在程序员有责任确保闭包在使用它的地方仍然存在!如果 enum_wnd_proc 将指针存储在某处,您必须非常小心不要在关闭闭包后使用它。


作为旁注,在转换特征对象时使用 mem::transmute:

use std::mem;
let closure_pointer_pointer: *mut c_void = unsafe { mem::transmute(trait_obj) };

产生更好的错误信息:

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)

Error E0512 .


另见

关于rust - 如何通过原始指针将闭包作为参数传递给 C 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38995701/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com