如何通过原始指针将闭包作为参数传递给 C 函数?
How do I pass a closure through raw pointers as an argument to a C function?
我在 Rust 中使用 WinAPI,有一些函数(比如 EnumWindows()
)需要回调。回调通常接受一个额外的参数(LPARAM
类型,它是 i64
的别名),您可以使用它来将一些自定义数据传递给回调。
我已经将 Vec<T>
个对象作为 LPARAM 发送到 WinAPI 回调并且它工作正常。例如 "unpacking" 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);
// ...
}
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);
}
我收到这些错误:
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`
我想知道:
- 有没有办法将 function/closure 传递给不同的函数并执行那些 "C-like" 转换?
- 将闭包转换为
i64
值以将其传递给回调的正确方法是什么?
我使用的是 Rust 稳定版。
首先,代码有一些逻辑错误:
在许多平台(如 64 位)上将指针转换为 i32
是 不正确的 。指针可以使用所有这些位。截断指针然后在截断的地址调用函数将导致真正糟糕的事情。通常你想使用机器大小的整数(usize
或 isize
)。
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)
另见
- Pass a Rust trait to C
我在 Rust 中使用 WinAPI,有一些函数(比如 EnumWindows()
)需要回调。回调通常接受一个额外的参数(LPARAM
类型,它是 i64
的别名),您可以使用它来将一些自定义数据传递给回调。
我已经将 Vec<T>
个对象作为 LPARAM 发送到 WinAPI 回调并且它工作正常。例如 "unpacking" 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);
// ...
}
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);
}
我收到这些错误:
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`
我想知道:
- 有没有办法将 function/closure 传递给不同的函数并执行那些 "C-like" 转换?
- 将闭包转换为
i64
值以将其传递给回调的正确方法是什么?
我使用的是 Rust 稳定版。
首先,代码有一些逻辑错误:
在许多平台(如 64 位)上将指针转换为
i32
是 不正确的 。指针可以使用所有这些位。截断指针然后在截断的地址调用函数将导致真正糟糕的事情。通常你想使用机器大小的整数(usize
或isize
)。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)
另见
- Pass a Rust trait to C