mozilla / uniffi-rs

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

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

Closed bendk closed 1 week ago

bendk commented 2 weeks ago

(This is the continuation of #2087, now that #2150 has been merged)

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

Sorry, I had an issue with me jj repo that caused me to delete the branch. Github isn't letting me re-open the PR, so I created a new one: https://github.com/mozilla/uniffi-rs/pull/2317.