mozilla / uniffi-rs

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

Remote interface types #2126

Open bendk opened 1 month ago

bendk commented 1 month ago

We currently support remote records and enums, but we don't support remote interface types. https://github.com/mozilla/uniffi-rs/pull/2087 starts that support since it allows the interface types to be used in function signatures, however it doesn't allow users to actually define any methods on remote interface types.

We should fix that, I'm thinking the syntax could look like this (using anyhow::Error since it's the main example driving this right now):

#[uniffi::export(remote)]
impl anyhow::Error {
   fn to_string(&self) -> String { }
}

If possible, we should also implement the Rust trait system. That would let users write:

#[uniffi::export(remote, Display)]
impl anyhow::Error { }

It will often be the case that remote methods are not supported by UniFFI. In that case, users can use an extension trait to define their own methods on the type, then expose them with uniffi::export(remote). Maybe we could provide some syntactic sugar to easily do that:

#[uniffi::export(remote_extend, Display)]
impl anyhow::Error {
  // the `is` method can't be exposed directly by UniFFI, since it's generic.  But we can define a set of `is_*` methods for specific types.
  fn is_foo_error(&self) -> bool {
     self.is::<foo::Error>()
  }
}

In this case, UniFFI will: