jhugman / uniffi-bindgen-react-native

A uniffi bindings generator for calling Rust from react-native
Other
0 stars 0 forks source link

Handle namespaced type references from other files/packages #68

Open zzorba opened 3 weeks ago

zzorba commented 3 weeks ago

Does UBRN handle namespaced names? Not if they are used in the same file (package?)

Theoretically I could have code that refers to two different rust packages of the same name, from different packages, for example both matrix_sdk::client::Client and matrix_sdk_ffi::client::Client would have the short name of Client. Or I could have a local class and an imported class referred to in the same package, one with a qualified name.

Sample rust code:

#[derive(uniffi::Enum, Clone, Debug)]
pub enum ClientBuilder {
    Foo,
    Bar,
}

#[derive(Clone, uniffi::Enum)]
pub enum WrappingEnum {
    TotallyFine,
    FirstClient(ClientBuilder),
    SecondClient(Arc<matrix_sdk_ffi::client_builder::ClientBuilder>),
}

Thankfully, this doesn't produce incorrect code today, but it does produce an assertion in UBRN and the generate process fails as a result.

jhugman commented 3 weeks ago

I think the difficulty here is that of API design.

If you have a top-level package called entry, that exports types from foo-crate and bar-crate crates, then getting types Foo from foo and Bar from bar is relatively easy:

import { Foo, Bar } from "entry"; 

If Baz is in both foo-crate and bar-crate how should the user get at it?

import { fooCrate, barCrate } from "entry"; 
const { Baz } = fooCrate;
import { Baz_from_fooCrate as Baz } from "entry"; 

The internals i.e. how the different Baz types are referred to one another in the generated code is relatively straight forward, now we have a full idea of the types in all components (see type_map).