-
Notifications
You must be signed in to change notification settings - Fork 242
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
Make foreign trait implementations opt-in (#1827) #1902
Conversation
I think this is the simplest way to fix the issue and making a distinction between these two things will be good going forward. Totally open to bikeshedding on the attribute names. |
8e13514
to
5588e5b
Compare
This patch is fantastic, loads of tests and exactly what needs to be done here, thanks! And sorry for the slow followup to your question in #1827, but as we discussed, I'm allowed to bike-shed names etc :) For the UniFFI consumer: I think we should move away from "callback". That's not really accurate for many use-cases with generic traits. Maybe just "foreign"? I propose for UDL:
and something like Internally: "callback" seems fine, "foreign" might marginally be better for consistency, whatever :) But it still seems very verbose - what about if you dropped the word "interface" everywhere if comes after "callback" in this patch - eg, all CallBackInterface/callback_interface/CALLBACK_INTERFACE etc just need "callback"? (or "foreign" ;) Internally is just nit-picking though, the "public" spelling is the important bit. I'd like to see other bike-sheds :) |
This sounds good to me. The one tweak I'd suggest is with the UDL keyword. |
Sounds great! I think I mildly prefer |
OTOH, I'm not sure plain-old |
Good point. I think either would work, but I'm currently liking |
let try_lift = if with_callback_interface { | ||
let trait_impl_ident = callback_interface::trait_impl_ident(&trait_name); | ||
quote! { | ||
fn try_lift(v: Self::FfiType) -> ::uniffi::deps::anyhow::Result<::std::sync::Arc<Self>> { | ||
Ok(::std::sync::Arc::new(<#trait_impl_ident>::new(v as u64))) | ||
} | ||
} | ||
} else { | ||
quote! { | ||
fn try_lift(v: Self::FfiType) -> ::uniffi::deps::anyhow::Result<::std::sync::Arc<Self>> { | ||
unsafe { | ||
Ok(*::std::boxed::Box::from_raw(v as *mut ::std::sync::Arc<Self>)) | ||
} | ||
} | ||
} | ||
}; |
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 is definitely the part I wasn't going to figure out how to do :P.
Any of the names seem like they work to me. The one that'd make it the clearest to me would be |
5588e5b
to
1010792
Compare
I ended up going with |
Not sure if this Q is still relevant but yes, |
1010792
to
dab13db
Compare
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.
@@ -1,3 +1,4 @@ | |||
# IMP: {{ "{:?}"|format(obj.imp()) }} |
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.
nit: either remove this line or make it a little less terse :)
This changes the default behavior for exported trait interfaces to not generate a foreign callback interface implementation for them. Users can opt-in to this using the `WithForeign` attribute for UDL and the `with_foreign` for proc-macros. The main reason for this change is that callback interfaces and trait interfaces aren't completely compatible. For example, `ByRef` is not supported for callback interfaces, but it is supported for trait interfaces that only have Rust implementations.
dab13db
to
e70164e
Compare
This changes the default behavior for exported trait interfaces to not generate a foreign callback interface implementation for them. Users can opt-in to this using the
TraitWithCallbackInterface
attribute for UDL and thewith_callback_interface
for proc-macros.The main reason for this change is that callback interfaces and trait interfaces aren't completely compatible. For example,
ByRef
is not supported for callback interfaces, but it is supported for trait interfaces that only have Rust implementations.