I'm working with WinAPI in Rust and there are some functions (like EnumWindows()
) which require a callback. The callback usually accepts an additional argument (of type LPARAM
which is an alias for i64
), which you can use to pass some custom data to the callback.
I have sent Vec<T>
objects as LPARAM to the WinAPI callbacks and it worked fine. For instance "unpacking" an lparam
value to a Vec<RECT>
looked like this in my case:
unsafe extern "system" fn enumerate_callback(hwnd: HWND, lparam: LPARAM) -> BOOL {
let rects = lparam as *mut Vec<RECT>;
}
Instead of passing a vector, I now have to pass a closure. I can not use a function pointer as my closure has to capture some variables, which would not be accessible if I used a function. In C++, I would use std::function<>
for my particular task, I think that in Rust the corresponding abstraction is a closure.
My code for unpacking looks like this:
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);
}
I get these errors:
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`
I would like to know:
- Is there a way to pass a function/closure to a different function and perform those "C-like" casts?
- What is the proper way to cast a closure to a
i64
value for passing it to that callback?
I'm using the stable version of Rust.