Error handling “我该如何解决?”;可能无法安全地跨越展开边界转移”;为了勇敢?

Error handling “我该如何解决?”;可能无法安全地跨越展开边界转移”;为了勇敢?,error-handling,rust,ffi,Error Handling,Rust,Ffi,我正试图用Rust代码覆盖/包装Libcvprintf(格式,变量列表)函数。为此,我需要将VaList参数传递到不安全的代码中,该代码还需要捕获展开错误: #![feature(c_variadic)] extern crate libc; use libc::{c_char, c_int}; pub unsafe extern "C" fn vprintf(format: *const c_char, args: std::ffi::VaList) -> c_i

我正试图用Rust代码覆盖/包装Libc
vprintf(格式,变量列表)
函数。为此,我需要将
VaList
参数传递到不安全的代码中,该代码还需要捕获展开错误:

#![feature(c_variadic)]
extern crate libc;

use libc::{c_char, c_int};

pub unsafe extern "C" fn vprintf(format: *const c_char, args: std::ffi::VaList) -> c_int {
    if true {
        ::std::panic::catch_unwind(|| hook_fn(format, args)).ok()
    } else {
        None
    }
    .unwrap_or_else(|| hook_fn(format, args))
}

pub unsafe fn hook_fn(format: *const c_char, args: std::ffi::VaList) -> c_int {
    0
}

fn main() {
    println!("Hello, world!");
}

我的代码不编译:

error[E0277]:类型`&mut std::ffi::VaListImpl`可能无法安全地跨展开边界传输
|

=帮助:在`[closure@src/main.rs:8:36:8:60格式:&*const i8,args:std::ffi::VaList]`,特性“std::panic::UnwindSafe”未为“&mut std::ffi::VaListImpl”实现,但不为“&mut std::ffi::VaListImpl某些类型,尤其是ffi类型,如果在恐慌之后使用,可能导致未定义的行为。此安全性通过类型是否实现
UnwindSafe
,以及
VaList
是否实现来跟踪

错误消息的第一个“帮助”行对此进行了解释:

=帮助:在`[closure@src/main.rs:8:36:8:61格式:&*const i8,args:std::ffi::VaList]`,

特性“std::panic::UnwindSafe”没有为“&mut std::ffi::VaListImpl”实现,但没有为“&mut std::ffi::VaListImpl”实现。这有助于解决问题。 不确定这是否是解决此问题的正确方法。彼得·霍尔(Peter Halls)上述建议可能仍然是正确的。但它似乎并没有为我纠正这个错误

#![feature(c_variadic)]
extern crate libc;

use libc::{c_char, c_int};

pub unsafe extern "C" fn vprintf(format: *const c_char, args: std::ffi::VaList) -> c_int {
    let ap : VaListImpl = args.clone();
    if true {
        ::std::panic::catch_unwind(|| hook_fn(format, ap.clone())).ok()
    } else {
        None
    }
    .unwrap_or_else(|| hook_fn(format, args))
}

pub unsafe fn hook_fn(format: *const c_char, args: std::ffi::VaList) -> c_int {
    0
}

fn main() {
    println!("Hello, world!");
}

彼得,谢谢你的建议。这似乎对我不起作用。但是,我能够通过上面添加的代码来克服这个错误。彼得,顺便说一句,我认为你的解释是正确的。就是无法让&x解决方案为我工作。
#![feature(c_variadic)]
extern crate libc;

use libc::{c_char, c_int};

pub unsafe extern "C" fn vprintf(format: *const c_char, args: std::ffi::VaList) -> c_int {
    let ap : VaListImpl = args.clone();
    if true {
        ::std::panic::catch_unwind(|| hook_fn(format, ap.clone())).ok()
    } else {
        None
    }
    .unwrap_or_else(|| hook_fn(format, args))
}

pub unsafe fn hook_fn(format: *const c_char, args: std::ffi::VaList) -> c_int {
    0
}

fn main() {
    println!("Hello, world!");
}