如何从Rust FFI创建并返回C++结构?
问题描述:
我想创建并返回一个C++结构。当我尝试编译时,我目前收到cannot move out of dereference of raw pointer
错误。任何想法我如何能做到这一点?如何从Rust FFI创建并返回C++结构?
#![allow(non_snake_case)]
#![allow(unused_variables)]
extern crate octh;
// https://thefullsnack.com/en/string-ffi-rust.html
use std::ffi::CString;
#[no_mangle]
pub unsafe extern "C" fn Ghelloworld(
shl: *const octh::root::octave::dynamic_library,
relative: bool,
) -> *mut octh::root::octave_dld_function {
let name = CString::new("helloworld").unwrap();
let pname = name.as_ptr() as *const octh::root::std::string;
std::mem::forget(pname);
let doc = CString::new("Hello World Help String").unwrap();
let pdoc = doc.as_ptr() as *const octh::root::std::string;
std::mem::forget(pdoc);
return octh::root::octave_dld_function_create(Some(Fhelloworld), shl, pname, pdoc);
}
pub unsafe extern "C" fn Fhelloworld(
args: *const octh::root::octave_value_list,
nargout: ::std::os::raw::c_int,
) -> octh::root::octave_value_list {
let list: *mut octh::root::octave_value_list = ::std::ptr::null_mut();
octh::root::octave_value_list_new(list);
std::mem::forget(list);
return *list;
}
答
我试图创建并返回一个C++结构
你不能; C++(如Rust)没有稳定的ABI。 Rust没有办法指定一个结构有repr(C++)
,因此你不能创建这样的结构,更不用说返回它。
唯一稳定的ABI是C.你提出的一个可以定义结构为repr(C)
能够直接返回他们:
extern crate libc;
use std::ptr;
#[repr(C)]
pub struct ValueList {
id: libc::int32_t,
}
#[no_mangle]
pub extern "C" fn hello_world() -> ValueList {
let list_ptr = ::std::ptr::null_mut();
// untested, will cause segfault unless list_ptr is set
unsafe { ptr::read(list_ptr) }
}
这种方法是非常可疑的,虽然;通常你会看到它作为
#[no_mangle]
pub extern "C" fn hello_world() -> ValueList {
unsafe {
let mut list = mem::uninitialized();
list_initialize(&mut list);
list
}
}
参见:
我建议你阅读我的Rust FFI Omnibus。
注意:C++ *也*具有C FFI,因此任何与C对应的C++类/ struct都可以从Rust中使用,就像它从C. –
Thanks @Shepmaster。现在编译。它可能工作,但我的八度helloworld尚未,所以我不是100%肯定。 https://github.com/ctaggart/octh_examples/blob/master/src/lib.rs –
@CameronTaggart不知道,但你应该防止在所有功能上的名称混搭。 'Fhelloworld'中不是'#[no_mangle]'吗? –