swift-bridge
swift-bridge copied to clipboard
Vec of shared struct
Currently something like the following Swift code:
class Test {
func testFunction(things: RustVec<Thing>) { /* Do stuff */ }
}
and this Rust code:
#[swift_bridge::bridge]
mod ffi {
#[swift_bridge(swift_repr = "struct")]
pub struct Thing {
pub some_str: String
}
extern "Swift" {
type Test;
#[swift_bridge(init)]
fn new() -> Test;
fn testFunction(&self, things: Vec<Thing>);
}
}
// Then somewhere else
let test = ffi::Test::new();
test.testFunction(vec![Thing { some_str: "foo".to_string() }, /* ... */]);
doesn't work, evidently due to the shared struct Thing
not implmenting Vectorizable
on the Swift side. Am I missing something and this sort of thing is possible? If not, could support for this be added?
Hey.
Yup you're right that Vec<SharedStruct>
isn't implemented yet. You aren't missing anything.
Right now we only support Vec<OpaqueRustType>
https://github.com/chinedufn/swift-bridge/blob/4d0a18f698c8244e874337f6fc9644b13f037e92/crates/swift-bridge-ir/src/codegen/codegen_tests/vec_codegen_tests.rs#L226-L236
https://github.com/chinedufn/swift-bridge/blob/b26dcf9c307d0bf77af96d5c29a6a479ad179a88/crates/swift-integration-tests/src/vec.rs#L1-L17
https://github.com/chinedufn/swift-bridge/blob/b26dcf9c307d0bf77af96d5c29a6a479ad179a88/SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunnerTests/VecTests.swift#L72-L81
Normally I'd try to get this to you right away but I'm pushing up against a deadline right now.
I can implement support for this over the weekend if that works for you?
Otherwise if it's urgent I'd be happy to provide guidance on how to implement this.
Thanks for opening this issue!
I can implement support for this over the weekend if that works for you?
Yes that’d be great! Thank you!
I'm in need of this too, would be awesome to get added!
@chinedufn this is blocking me currently so I'd be happy to try and implement, do you have any pointers as to how to do so?
Hey! Thanks for the heads up. I'll write up some detailed instructions either tonight or tomorrow.
Hey. Sorry I'll have to get to this today or tomorrow! Will definitely write these for you just need to finish a couple more unrelated things. Thanks!
Thanks for waiting! I've written up a guide to adding support for Vec<TransparentStruct>
. Please let me know if you have any questions and I'll be more than happy to help.
Implementing Support for Vec<TransparentStruct>
Background
-
Read the docs on adding support for a new signature https://chinedufn.github.io/swift-bridge/contributing/adding-support-for-a-signature/index.html
-
Read some of the existing
Vec
tests- https://github.com/chinedufn/swift-bridge/blob/c1fd7177b5d3a8918e34b6bc96ae29437cfc1487/SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunnerTests/VecTests.swift#L11
- https://github.com/chinedufn/swift-bridge/blob/c1fd7177b5d3a8918e34b6bc96ae29437cfc1487/crates/swift-bridge-ir/src/codegen/codegen_tests/vec_codegen_tests.rs#L7-L20
Integration Tests
Add an integration test for Vec<TransparentStruct>
.
You can use Vec<TransparentEnum>
as a guide:
- https://github.com/chinedufn/swift-bridge/blob/c1fd7177b5d3a8918e34b6bc96ae29437cfc1487/SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunnerTests/VecTests.swift#L84-L94
- https://github.com/chinedufn/swift-bridge/blob/c1fd7177b5d3a8918e34b6bc96ae29437cfc1487/crates/swift-integration-tests/src/vec.rs#L23-L27
Codegen Tests
Add codegen tests, similar to the ones for transparent enums
- https://github.com/chinedufn/swift-bridge/blob/c1fd7177b5d3a8918e34b6bc96ae29437cfc1487/crates/swift-bridge-ir/src/codegen/codegen_tests/vec_codegen_tests.rs#L282-L423
- https://github.com/chinedufn/swift-bridge/blob/c1fd7177b5d3a8918e34b6bc96ae29437cfc1487/crates/swift-bridge-ir/src/codegen/codegen_tests/vec_codegen_tests.rs#L425-L479
- https://github.com/chinedufn/swift-bridge/blob/c1fd7177b5d3a8918e34b6bc96ae29437cfc1487/crates/swift-bridge-ir/src/codegen/codegen_tests/vec_codegen_tests.rs#L481-L538
Vec support tests
-
Add a
mod vec_of_transparent_struct
- https://github.com/chinedufn/swift-bridge/blob/c1fd7177b5d3a8918e34b6bc96ae29437cfc1487/crates/swift-bridge-ir/src/codegen/generate_rust_tokens/vec.rs#L1-L2
-
Model it after the
mod vec_of_transparent_enum
- https://github.com/chinedufn/swift-bridge/blob/819e39160ec5b38dce2e60e2d39d078617ad158d/crates/swift-bridge-ir/src/codegen/generate_rust_tokens/vec/vec_of_transparent_enum.rs#L5-L12
-
Same idea with the Swift impl
- https://github.com/chinedufn/swift-bridge/blob/72e1759e0504e3a5a32587605c98e410f12ea9e7/crates/swift-bridge-ir/src/codegen/generate_swift/shared_enum.rs#L71-L102
Getting everything working
You should be able to run the tests and tweak code in https://github.com/chinedufn/swift-bridge/blob/2bffcb08c1f0ca8bbcba9c78c5ae28a9cddfb632/crates/swift-bridge-ir/src/bridged_type.rs until the tests pass
If there is currently nobody working on this, I would like to try to implement this (probably next week).
All yours! Please don’t hesitate to ask questions here if you have any.
All yours!
Please don’t hesitate to ask questions here if you have any.
Unfortunately I got some project deadlines the next weeks that require more work than expected, so I am not able to start working on this any time soon.