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

时间:2016-08-17 11:43:49

标签: rust closures traits ffi

我在Rust中使用WinAPI,并且有一些函数(如EnumWindows())需要回调。回调通常接受另一个参数(类型为LPARAM,它是i64的别名),您可以使用该参数将一些自定义数据传递给回调。

我已将Vec<T>个对象作为LPARAM发送到WinAPI回调,它运行正常。例如&#34;拆包&#34;在我的情况下,lparam的{​​{1}}值看起来像这样:

Vec<RECT>

我现在必须传递一个闭包,而不是传递一个向量。我不能使用函数指针,因为我的闭包必须捕获一些变量,如果我使用了函数则无法访问它们。在C ++中,我会将unsafe extern "system" fn enumerate_callback(hwnd: HWND, lparam: LPARAM) -> BOOL { let rects = lparam as *mut Vec<RECT>; } 用于我的特定任务,我认为在Rust中相应的抽象是一个闭包。

我的解包代码如下:

std::function<>

SSCCE:

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

Playground

我收到这些错误:

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

我想知道:

  1. 有没有办法将函数/闭包传递给不同的函数并执行那些&#34; C-like&#34;施放?
  2. 将闭包转换为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` 值以将其传递给该回调的正确方法是什么?
  3. 我使用的是稳定版的Rust。

1 个答案:

答案 0 :(得分:9)

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

  1. 在许多平台(如64位)上向i32投射指针不正确。指针可以使用所有这些位。截断指针然后在截断的地址处调用函数将导致Really Bad Things。通常,您希望使用机器大小的整数(usizeisize)。

  2. sum值必须是可变的。

  3. 问题的关键在于闭包是具体类型,它们占用程序员不知道的大小,但编译器已知。 C函数仅限于采用机器大小的整数。

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

    通常,对dynamically-sized type typeBox的任何引用都将生成一个胖指针。

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

    我们对fat指针进行第二次引用,它会创建一个瘦指针。该指针只有一个整数的机器整数。

    也许图表有助于(或受到伤害)?

    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

    另见