2

我正在尝试为 Linux 扫描库 libsane 设置 Rust FFI。执行该sane_open函数返回4指示无效参数的错误代码。我相信这与我的参数声明有关。

// If successful, places a new handle in *h
// SANE_String_Const is const char* and SANE_Handle is void*
SANE_Status sane_open(SANE_String_Const devname, SANE_Handle *h);

Bindgen 生成以下内容:

pub type SANE_String_Const = *const ::std::os::raw::c_char
pub type SANE_Handle = *mut ::std::os::raw::c_void
pub fn sane_open(devname: SANE_String_Const, handle: *mut SANE_Handle) -> SANE_Status;

这是我所做的:

pub fn test_open() {
    unsafe {
        let mut version_code = 0;
        let result = sane_init(&mut version_code, None);
        assert_eq!(result, SANE_Status_SANE_STATUS_GOOD);

        let mut handle: SANE_Handle = std::ptr::null_mut();
        let dev = CString::new("net1;dev0").unwrap();
        let result = sane_open(dev.as_ptr(), &mut handle);
        assert_eq!(result, SANE_Status_SANE_STATUS_GOOD);

        sane_close(handle);
        sane_exit();
    }
}

请注意,为回调bindgen生成一个类型,其中意味着传递. 我在 C 中的最小工作示例完美无缺:Option<_>NoneNULL

/* Link with -l sane */
#include <sane/sane.h>
#include <stdio.h>
#include <assert.h>

int main() {
    SANE_Int version_code = 0;
    assert(sane_init(&version_code, NULL) == SANE_STATUS_GOOD);

    SANE_Handle h;
    SANE_Status r = sane_open("net1;dev0", &h);
    printf("Result: %d\n", r);
    if(r == 0)
        sane_close(h);
    sane_exit();
}

怎么了?为什么我的 Rust 代码表现不同?

4

0 回答 0