swift-bridge icon indicating copy to clipboard operation
swift-bridge copied to clipboard

Support Swift's `Comparable`

Open NiwakaDev opened this issue 2 years ago • 5 comments

I'd like to use Swift's Comparable, like so:

#[swift_bridge::bridge]
mod ffi {
    extern "Rust" {
        #[swift_bridge(Comparable)]
        type SomeType;
        //...
    }
}
//Swift

let p = SomeType()
let q = SomeType()

if p>q {
   //...
} else {
   //...
}

But, this might be a low priority.

NiwakaDev avatar Mar 15 '23 12:03 NiwakaDev

Sounds good.

Feel free to write a guide on how a new contributor can implement this and then we can label this as a good first issue. (You don't have to, just letting you know that you can if you want to give it a try).

Or if you need it feel free to implement it yourself.

chinedufn avatar Mar 20 '23 16:03 chinedufn

Implementation Guide

Here's a guide for supporting #[swift_bridge(Comparable)]

Test

It's a good idea to start by writing tests.

Integration test

Tests for Equatable and Hashable might be helpful as a reference.

Here's the test for Equatable:

https://github.com/chinedufn/swift-bridge/blob/637c7b353539c1122dacc3a64e7d5dc052a4fec9/SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunnerTests/OpaqueRustStructTests.swift#L72-L91

And here's the test for Hashable:

https://github.com/chinedufn/swift-bridge/blob/637c7b353539c1122dacc3a64e7d5dc052a4fec9/SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunnerTests/OpaqueRustStructTests.swift#L93-L131

In the above test code, you will notice RustEquatableType and RustHashableType. They're implemented on the Rust side.

Here's the implementation code for RustEquatableType: https://github.com/chinedufn/swift-bridge/blob/637c7b353539c1122dacc3a64e7d5dc052a4fec9/crates/swift-integration-tests/src/opaque_type_attributes/equatable.rs#L1-L25

By writing the above code, we can use RustEquatableType on the Swift side.

Codegen test

Similarly, tests for Equatable and Hashable might be helpful.

Equatable codegen test:

https://github.com/chinedufn/swift-bridge/blob/637c7b353539c1122dacc3a64e7d5dc052a4fec9/crates/swift-bridge-ir/src/codegen/codegen_tests/opaque_rust_type_codegen_tests.rs#L148-L209

Hashable codegen test: https://github.com/chinedufn/swift-bridge/blob/637c7b353539c1122dacc3a64e7d5dc052a4fec9/crates/swift-bridge-ir/src/codegen/codegen_tests/opaque_rust_type_codegen_tests.rs#L84-L146

Implementing support for Swift's Comparable

https://github.com/chinedufn/swift-bridge/blob/637c7b353539c1122dacc3a64e7d5dc052a4fec9/crates/swift-bridge-ir/src/parse/parse_extern_mod/opaque_type_attributes.rs#L29-L34

By adding comparable: bool to the struct OpaqueTypeSwiftBridgeAttributes, we can track whether an Opaque Rust Type should conform to Comparable.

To verify that comparable: bool is working correctly, we need to add a test for parsing #[swift_bridge(Comparable)], like so:

https://github.com/chinedufn/swift-bridge/blob/637c7b353539c1122dacc3a64e7d5dc052a4fec9/crates/swift-bridge-ir/src/parse/parse_extern_mod.rs#L787-L811

NiwakaDev avatar Mar 21 '23 06:03 NiwakaDev

Feel free to write a guide on how a new contributor can implement this we can label this and label it as a good first issue. (You don't have to, just letting you know that you can if you want to give it a try).

I don't have an access permission for labeling, so could you please label this as a good first issue on my behalf?

NiwakaDev avatar Mar 21 '23 06:03 NiwakaDev

Thanks a lot for writing this implementation guide.

Across the board, your work on swift-bridge has been excellent and I'm very appreciative of all of your great contributions.

I've invited you to be an official collaborator on the project. You can go ahead and add the label, if you decide accept.

Cheers!

chinedufn avatar Mar 21 '23 16:03 chinedufn

Thank you for the invitation!!!! I'm honored and grateful for the opportunity to contribute more as a collaborator!!!!!!

Thank you again!!!!

NiwakaDev avatar Mar 21 '23 23:03 NiwakaDev