小编vij*_*joc的帖子

将C++异常映射到Result

我正在编写一个Rust库,它是C++库的包装器.

这是C++方面:

#define Result(type,name) typedef struct { type value; const char* message; } name

extern "C" 
{
    Result(double, ResultDouble);

    ResultDouble myFunc() 
    {
        try
        {
            return ResultDouble{value: cv::someOpenCvMethod(), message: nullptr};
        }
        catch( cv::Exception& e )
        {
            const char* err_msg = e.what();
            return ResultDouble{value: 0, message: err_msg};
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

和相应的Rust方面:

#[repr(C)]
struct CResult<T> {
    value: T,
    message: *mut c_char,
}

extern "C" {
    fn myFunc() -> CResult<c_double>;
}

pub fn my_func() -> Result<f64, Cow<'static, str>> {
    let result = unsafe { …
Run Code Online (Sandbox Code Playgroud)

c++ opencv ffi rust

4
推荐指数
1
解决办法
190
查看次数

标签 统计

c++ ×1

ffi ×1

opencv ×1

rust ×1