Closed gabbifish closed 6 months ago
If the proposed fix above looks good, I'd be happy to open the PR for it!
Thanks for reporting this and including steps to reproduce.
We don't want RustString(String)
to be #[repr(C)]
since std::string::String
isn't FFI safe.
For example, the following program:
fn main() {
}
#[repr(C)]
pub struct RustString(String);
pub extern "C" fn return_rust_string() -> RustString {
unimplemented!()
}
Would print the following in stderr
when compiled:
Compiling playground v0.0.1 (/playground)
warning: `extern` fn uses type `String`, which is not FFI-safe
--> src/main.rs:8:43
|
8 | pub extern "C" fn return_rust_string() -> RustString {
| ^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct
= note: this struct has unspecified layout
= note: `#[warn(improper_ctypes_definitions)]` on by default
Fortunately, we don't actually pass the RustString
across the FFI boundary.
Instead, we pass a pointer to the RustString
, which is totally FFI safe.
I'll PR a fix tonight.
Thanks again for reporting this.
The warning was fixed here https://github.com/chinedufn/swift-bridge/pull/254
Thank you so much!!
I exposed a transparent struct in Rust, which is used by a swift function exposed back to rust. This produces the error
Seems this is related to RustStrings being used in a Swift function exposed to Rust, and the solution looks as simple as
Example code to reproduce level (rust):
and on the swift side:
(code is provided under the MIT license)