-
Notifications
You must be signed in to change notification settings - Fork 65
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
Adding derive() macro support to SharedStructs #198
Adding derive() macro support to SharedStructs #198
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice looks great. Left a couple minor comments then we can land.
@@ -90,7 +90,15 @@ impl SwiftBridgeModule { | |||
} | |||
}; | |||
|
|||
let automatic_derives; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're moving away from automatic derives, so derives
is fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -19,6 +19,7 @@ pub(crate) struct SharedStruct { | |||
pub fields: StructFields, | |||
pub swift_name: Option<LitStr>, | |||
pub already_declared: bool, | |||
pub derives: Option<Vec<TokenStream>>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of Option<Vec<TokenStream>>
let's use something like:
pub(crate) struct StructDerives {
pub copy: bool,
pub clone: bool,
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would kind of limit us to only these kind of derives, if we want to add new ones (like Debug
) we would need to open a PR and add them. It also has the potential of growing quite a lot. I don't see why it would be an issue to have the more generic Vec<TokenStream>
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for explaining your thinking. Makes sense.
The issue is that we only want to support derives that we know are well behaved.
Supporting arbitrary derives might be possible, but requires more research.
As it stands now, supporting arbitrary derives can lead to undefined behavior.
It's incredibly unlikely, but theoretically possible.
Before we support arbitrary derives we want to make UB completely impossible.
Essentially, #[derive(SomeCustomDerive)]
could emit something like type u32 = some_crate::NotAU32
and cause swift-bridge
to think that it sees a u32
not realizing that a type alias got generated to make u32
a completely different type.
It should be possible to guard against this, but until we guard against it (and any other potential derive related UB scenarios) we can't allow arbitrary derives.
This discussion has more detail #190 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense, will go with your suggestion then
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Verify that we properly parse multiple comma separated struct attributes. | ||
#[test] | ||
fn parses_multiple_struct_attributes() { | ||
let tokens = quote! { | ||
#[swift_bridge::bridge] | ||
mod ffi { | ||
#[swift_bridge(swift_name = "FfiFoo", swift_repr = "class")] | ||
#[derive(Copy, Clone)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we remove this derive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm actually testing here whether #[swift_bridge]
and #[derive]
work together here. I think it's a good test to have, so I'll just leave this test as is and duplicate it to check if they work ok together
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let tokens = quote! { | ||
#[swift_bridge::bridge] | ||
mod ffi { | ||
#[derive(Copy, Clone)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice
Just missing a codegen test and an integration test (don't need the Swift side .. just a Rust integration test is fine for now and in the future we can emit a clone function on the Swift side...) See the following files in https://github.com/chinedufn/swift-bridge/pull/194/files for an example that you can copy/paste/modify
|
@chinedufn should I wait for that PR to be merged to add the tests on this file you mentioned, or should I create a separate file like |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Thanks!!
|
||
let ty2 = module.types.types()[1].unwrap_shared_struct(); | ||
|
||
assert_eq!(ty2.derives.copy, false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice
@@ -0,0 +1,28 @@ | |||
#[swift_bridge::bridge] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome tests. Might've been nice to actually use the Copy/Clone
impls outside of the module down below, but this is fine.
Creating a file in this PR sounds good! |
just updated with the tests |
Looks good. I'll merge once tests are passing (looks like you need to run |
…ft-bridge into feature/shared-struct-derive
Thank you! |
This would be great to have on |
This PR adds support for
#[derive(...)]
macros toSharedStruct
s.Example
This will successfully add a derived
Copy
andClone
trait to the struct declared.