cxx icon indicating copy to clipboard operation
cxx copied to clipboard

Implementing Rust method in C++ results in "not FFI-safe" error

Open topjohnwu opened this issue 11 months ago • 2 comments

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.

topjohnwu avatar Dec 03 '24 21:12 topjohnwu