chinedufn / swift-bridge

swift-bridge facilitates Rust and Swift interop.
https://chinedufn.github.io/swift-bridge
Apache License 2.0
842 stars 62 forks source link

Support bridging `Option<SwiftClass>` in `extern "Rust"` function #268

Closed chinedufn closed 6 months ago

chinedufn commented 7 months ago

It is not currently possible to bridge Option<OpaqueSwiftType> in an extern "Rust" function.

For example, the following signature does not compile:

#[swift_bridge::bridge]
mod ffi {
    extern "Rust" {
        fn some_function(arg: Option<SwiftType>) -> Option<SwiftType>;
    }

    extern "Swift" {
        type SwiftType;
    }
}

Potential Solution

  1. Review the documentation for supporting a new signature https://github.com/chinedufn/swift-bridge/blob/master/book/src/contributing/adding-support-for-a-signature/README.md

  2. Add fn rust_reflect_option_swift_type(arg: Option<OptTestOpaqueSwiftType>) -> Option<OptTestOpaqueSwiftType> to the Option integration tests https://github.com/chinedufn/swift-bridge/blob/58f4a40f96bb050607c746376422ab3c62e0e771/crates/swift-integration-tests/src/option.rs#L1-L144

  3. Add a test that calls rust_reflect_option_swift_type

  4. Add a codegen test for a Rust fn accepting an Option<SwiftType> arg to option_codegen_tests.rs

  5. Add a codegen test for a Rust fn returning an Option<SwiftType> in option_codegen_tests.rs

  6. Get tests passing