uniffi-rs
uniffi-rs copied to clipboard
Remote interface types
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 { }
}
- The
remotekeyword indicates thatanyhow::Erroris remote so UniFFI should generate the scaffolding for this type, but only implement the FFI traits for the localUniFfiTag. It should not output theimplitem and the methods, those are actually defined in theanyhowcrate. Note: in this caseto_stringis not an inherent method, but it's still defined sinceanyhow::ErrorimplementsDisplay. - 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::Errorusing the user-supplied code - Generate the scaffolding for the type (I think we still need to use the local
UniFfiTag