cxx
cxx copied to clipboard
Implementing Rust method in C++ results in "not FFI-safe" error
I'm trying to expose a Rust defined type and implement one of its method in C++
#[cxx::bridge]
pub mod ffi {
extern "Rust" {
type Test;
}
unsafe extern "C++" {
fn bar(self: &Test);
}
}
struct Test {
foo: String,
}
Results in:
error: `extern` block uses type `Test`, which is not FFI-safe
--> lib.rs:36:22
|
36 | fn bar(self: &Test);
| ^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct
= note: this struct has unspecified layout
The generated C++ bindings are correct and can be successfully compiled.
Read the error message, it explains what you need to do... slap a #[repr(C)] on the struct Test.
@hunger it shouldn't be needed, as the type is an opaque Rust type. There is no requirement about the Rust type when passing through opaque Rust types.
This is fixed in cxx 1.0.178.