Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add attribute for Equatable protocol #139

Merged
merged 3 commits into from
Jan 18, 2023

Conversation

NiwakaDev
Copy link
Collaborator

@NiwakaDev NiwakaDev commented Jan 17, 2023

This PR introduces the #[swift_bridge(Equatable)] attribute, which is used to generate a type that conforms to Equatable protocol.

Somthing like:

// Rust 
mod ffi {
    extern "Rust" {
        #[swift_bridge(Equatable)]
        type SomeType;
    }
}
// Generated Swift
class SomeType {
    // ...
}

// SomeType inherits SomeTypeRef. So, SomeType also conforms to Equatable protocol.
extension SomeTypeRef: Equatable {
    public static func == (lhs: SomeTypeRef, rhs: SomeTypeRef) -> Bool {
        __swift_bridge__$SomeTypeType$_partial_eq(rhs.ptr, lhs.ptr)
    }
}

Copy link
Owner

@chinedufn chinedufn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks perfect. Tests look great. Thanks so much for your work here. Great stuff.


Can you just update your PR body with an example of the feature.

Here's an example: #21

You can see more examples in the releases page https://github.com/chinedufn/swift-bridge/releases

Then I can merge this.

@chinedufn
Copy link
Owner

Oh, and we need to add this to the attributes documentation here

## Opaque Type Attributes
#### #[swift_bridge(already_declared)]
The `already_declared` attribute allows you to use the same type in multiple bridge modules.
```rust
use some_crate::App;
mod ffi {
extern "Rust" {
type App;
#[swift_bridge(init)]
fn new() -> App;
}
}
#[swift_bridge::bridge]
#[cfg(feature = "dev-utils")]
mod ffi_dev_utils {
extern "Rust" {
// We won't emit Swift and C type definitions for this type
// since we've already declared it elsewhere.
#[swift_bridge(already_declared)]
type App;
fn create_logged_in_user(&mut self, user_id: u8);
}
}
```
#### #[swift_bridge(Copy($SIZE))]
If you have an opaque Rust type that implements `Copy`, you will typically want to be

You can put Equatable below the Copy attribute

@NiwakaDev
Copy link
Collaborator Author

Can you just update your PR body with an example of the feature.

I did this. Please check my PR body!

@NiwakaDev
Copy link
Collaborator Author

NiwakaDev commented Jan 17, 2023

Oh, and we need to add this to the attributes documentation here

Sure. I'll address this tomorrow!

@NiwakaDev
Copy link
Collaborator Author

Hi, @chinedufn!

I addressed your review.

@chinedufn
Copy link
Owner

Thanks!

@chinedufn
Copy link
Owner

chinedufn commented Apr 15, 2024

Referenced this PR in the guide for #269

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants