mozilla / uniffi-rs

a multi-language bindings generator for rust
https://mozilla.github.io/uniffi-rs/
Mozilla Public License 2.0
2.89k stars 232 forks source link

Common system for remote type handling (#1865) #2317

Closed bendk closed 1 week ago

bendk commented 2 weeks ago

Remote types present an issue because we can't implement FFI traits like Lower and Lift on them directly. The workaround is to only implement the FFI trait for the current crate. If another crate wants to use that impl, we need to implement it again by forwarding to the initial implementation.

Historically, UDL and proc-macros have approached this differently which has lead to a lot of extra complexity and confusion. This change aligns them on a common system: By default, we implement traits for all crates (a "blanket impl"). However, users can opt-in to only implementing it for the current crate ("a local impl`)

See the doc changes for a description of the new system.

This required a bunch of changes:

Added a motivating example where anyhow::Error is used as an error type. Changed the error type bound to Display + Debug rather than Error. For some reason anyhow::Error doesn't actually implement error.

One feature that we've lost in defining callback/trait interfaces using traits defined in a remote crate. I think this is okay because I doubt anyone is using that feature.

bendk commented 1 week ago

There are some docs/changelog/upgrading changes we can make before we release, but that's polish. Sounds good to me, let's merge now and follow-up on this later.