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

Remote interface types #2126

Open
bendk opened this issue May 24, 2024 · 0 comments
Open

Remote interface types #2126

bendk opened this issue May 24, 2024 · 0 comments

Comments

@bendk
Copy link
Contributor

bendk commented May 24, 2024

We currently support remote records and enums, but we don't support remote interface types. #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 { }
}
  • The remote keyword indicates that anyhow::Error is remote so UniFFI should generate the scaffolding for this type, but only implement the FFI traits for the local UniFfiTag. It should not output the impl item and the methods, those are actually defined in the anyhow crate. Note: in this case to_string is not an inherent method, but it's still defined since anyhow::Error implements Display.
  • Function bodies are ignored and users should just write an empty block for them.

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:

  • Define an extension trait
  • Implement the trait for anyhow::Error using the user-supplied code
  • Generate the scaffolding for the type (I think we still need to use the local UniFfiTag
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

No branches or pull requests

1 participant