要实现这一点,我们需要一种方法将实例存储到某种列表中,并将列表的索引返回到C++,因此它像一个指向锈结构的指针。
Box
在鲁斯特中,并将其返回到C++代码A
Box<T>
T: Sized
the same memory layout as a C pointer
如链接文档中所述,您的代码可以简单地如下所示:
// C++ header
// Returns ownership to the caller
extern "C" void *foo_new();
// Borrows mutably. The pointee cannot be changed by a different thread
// during the runtime of the function. The argument must be a pointer
// allocated with foo_new().
extern "C" void foo_transmogrify(void *);
// Takes ownership from the caller; no-op when invoked with NULL
extern "C" void foo_delete(void *);
#[repr(C)]
pub struct Foo {
glonk: bool,
}
#[no_mangle]
pub extern "C" fn foo_new() -> Box<Foo> {
Box::new(Foo { glonk: false })
}
#[no_mangle]
pub extern "C" fn foo_transmogrify(foo: &mut Foo) {
foo.glonk = true;
}
#[no_mangle]
pub extern "C" fn foo_delete(_: Option<Box<Foo>>) {}
注意,deallocation函数可以是空的。它将拥有
盒子