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

Record metadata for a struct implementing a trait. #2204

Merged
merged 1 commit into from
Oct 15, 2024

Conversation

mhammond
Copy link
Member

@mhammond mhammond commented Aug 6, 2024

Record metadata for a struct implementing a trait.

#[uniffi::export]
impl MyTrait for MyObject { ... }

Previously worked as it ignored MyTrait. This adds new metadata to record it,
allowing foreign bindings to implement things like inheritance.

Includes Python generating an inheritance chain to reflect this relationship.

This will not generate correct code if a struct declares more than 1 trait,
and there's some undesirable re-wrapping when traits from these objects gets
passed back and forward, but seems to work surprisingly well.

On the path to #2196.

@mhammond mhammond requested a review from gruberb August 6, 2024 13:42
@mhammond mhammond requested a review from a team as a code owner August 6, 2024 13:42
@mhammond mhammond force-pushed the push-yolpttzuvznp branch 3 times, most recently from 5411e96 to fa42fd1 Compare September 4, 2024 02:02
@mhammond
Copy link
Member Author

mhammond commented Sep 4, 2024

Bindings all turned out to be fairly easy - which in some ways is "bad" because it highlights an inefficiency here, but good because it seems fine and useful.

@mhammond mhammond force-pushed the push-yolpttzuvznp branch 2 times, most recently from d177e59 to 7163779 Compare September 4, 2024 02:38
@mhammond mhammond force-pushed the push-yolpttzuvznp branch 3 times, most recently from 317c1dd to 8fbccb5 Compare October 4, 2024 01:33
@mhammond
Copy link
Member Author

mhammond commented Oct 4, 2024

The other languages actually aren't working :) So just Python for now.

Copy link
Contributor

@bendk bendk left a comment

Choose a reason for hiding this comment

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

Looks great and I'm pretty amazed how easy it was. I just noticed a couple minor things.

I think this is a breaking change since the metadata format is going to change right? If so, please bump UNIFFI_CONTRACT_VERSION and let's wait to merge until I can get a 0.28.2 release out.

docs/manual/src/proc_macro/index.md Outdated Show resolved Hide resolved
fixtures/coverall/tests/bindings/test_coverall.py Outdated Show resolved Hide resolved
@@ -23,7 +23,7 @@ pub mod codes {
pub const UNIFFI_TRAIT: u8 = 11;
pub const TRAIT_INTERFACE: u8 = 12;
pub const CALLBACK_TRAIT_INTERFACE: u8 = 13;
//pub const UNKNOWN: u8 = 255;
pub const AS_TRAIT: u8 = 14;
Copy link
Contributor

Choose a reason for hiding this comment

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

AS_TRAIT doesn't communicate what this is doing to me, I keep thinking of AsRef<T> and expecting some sort of casting to be happening. That's essentially what happens when you use the object as a trait, but here we're just tracking the list. What about something like TRAIT_IMPLS?

@@ -8,7 +8,7 @@
{% if ci.is_name_used_as_error(name) %}
class {{ impl_name }}(Exception):
{%- else %}
class {{ impl_name }}:
class {{ impl_name }}({% for t in obj.as_trait_impls() %}{{ t.trait_name|class_name }},{% endfor %}):
Copy link
Contributor

Choose a reason for hiding this comment

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

🤯 it's amazing that this is all we need to support this feature.

@mhammond
Copy link
Member Author

mhammond commented Oct 7, 2024

I think this is a breaking change since the metadata format is going to change right? If so, please bump UNIFFI_CONTRACT_VERSION and let's wait to merge until I can get a 0.28.2 release out.

I'm a little unclear on this - I don't think it changes any existing metadata item formats, but does add a new one - does that qualify as needing this? I'd have though we expect consumers to ignore what they don't understand?

@bendk
Copy link
Contributor

bendk commented Oct 7, 2024

I think this is a breaking change since the metadata format is going to change right? If so, please bump UNIFFI_CONTRACT_VERSION and let's wait to merge until I can get a 0.28.2 release out.

I'm a little unclear on this - I don't think it changes any existing metadata item formats, but does add a new one - does that qualify as needing this? I'd have though we expect consumers to ignore what they don't understand?

I'm not clear either, but I think this line will fail and that will bubble up to make the entire metadata parsing step fail. I could definitely be wrong about that, but I think that's how it works.

I agree that it makes more sense to ignore things you don't understand. Metadata::read should probably be changed to return Result<Option<Self>> .

@mhammond
Copy link
Member Author

mhammond commented Oct 7, 2024

I see, thanks for the explanation!

@mhammond mhammond force-pushed the push-yolpttzuvznp branch 2 times, most recently from 0fdf117 to fbe3026 Compare October 14, 2024 17:06
@mhammond mhammond requested review from gruberb and bendk October 14, 2024 17:06
```
#[uniffi::export]
impl MyTrait for MyObject { ... }
```

Previously worked as it ignored `MyTrait`. This adds new metadata to record it,
allowing foreign bindings to implement things like inheritance.

Includes Python generating an inheritance chain to reflect this relationship.

This will not generate correct code if a struct declares more than 1 trait,
and there's some undesirable re-wrapping when traits from these objects gets
passed back and forward, but seems to work surprisingly well.

Fixes mozilla#2196.
Copy link
Contributor

@bendk bendk left a comment

Choose a reason for hiding this comment

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

Thanks for updating this, the changes look great.

@mhammond mhammond merged commit c4963ec into mozilla:main Oct 15, 2024
5 checks passed
@mhammond mhammond deleted the push-yolpttzuvznp branch October 15, 2024 19:31
mhammond added a commit to mhammond/uniffi-rs that referenced this pull request Nov 1, 2024
Builds on mozilla#2204 which landed the metadata and Python support.
mhammond added a commit to mhammond/uniffi-rs that referenced this pull request Nov 1, 2024
Builds on mozilla#2204 which landed the metadata and Python support.
mhammond added a commit to mhammond/uniffi-rs that referenced this pull request Nov 7, 2024
Builds on mozilla#2204 which landed the metadata and Python support.

Also fixes minor issues with `CallbackInterface` types.
mhammond added a commit that referenced this pull request Nov 7, 2024
Builds on #2204 which landed the metadata and Python support.

Also fixes minor issues with `CallbackInterface` types.
mhammond added a commit to mhammond/uniffi-rs that referenced this pull request Dec 22, 2024
mhammond added a commit to mhammond/uniffi-rs that referenced this pull request Dec 22, 2024
mhammond added a commit that referenced this pull request Dec 24, 2024
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.

3 participants