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

Fix returning Result<Vec<>, ...> from Rust #218

Closed jfaust closed 1 year ago

jfaust commented 1 year ago

Fixes #217

This supports bridges of the form:

#[swift_bridge::bridge]
mod ffi {
    extern "Rust" {
        type OpaqueRust;
        fn rust_func_return_result_of_vec_u32() -> Result<Vec<u32>, OpaqueRust>;
        fn rust_func_return_result_of_vec_opaque(
        ) -> Result<Vec<OpaqueRust>, OpaqueRust>;
    }
}

fn rust_func_return_result_of_vec_u32() -> Result<Vec<u32>, OpaqueRust> {
    Ok(vec![0, 1, 2])
}

fn rust_func_return_result_of_vec_opaque(
) -> Result<Vec<OpaqueRust>, OpaqueRust> {
    Ok(vec![
        OpaqueRust::new(...),
        OpaqueRust::new(...),
        OpaqueRust::new(...),
    ])
}
NiwakaDev commented 1 year ago

Thank you for your contribution!! I`m not the owner of this repository, but I left one minor feedback.

We need to add a codegen test because we'd like to test that __private__ResultPtrAndPtr is generated, not a custom result. Please see an implementation guide of #217.

NiwakaDev commented 1 year ago

@chinedufn

So, I think that this PR is fine with just the integration test and can land.

Sure!

jfaust commented 1 year ago

@chinedufn done!

chinedufn commented 1 year ago

Thanks!