rust-diplomat / diplomat

Experimental Rust tool for generating FFI definitions allowing many other languages to call Rust code
https://rust-diplomat.github.io/book/
Other
520 stars 48 forks source link

Support nested modules / namespacing #103

Closed sffc closed 1 month ago

sffc commented 2 years ago
#[diplomat::bridge]
pub mod ffi {
    pub mod foo {
        pub struct Bar {
            pub x: u32,
        }
    }
}

The above code should generate a namespaced Bar object. Right now the whole module foo just gets ignored, and if I annotate it with another #[diplomat::bridge], it just puts Bar in the global (capi) namespace.

Manishearth commented 2 years ago

My hope was that namespacing could be done with an attribute, so you do something like #[diplomat::namespace(foo)] and it gets namespaced under foo, but you can still organize your code however you'd like.

It's also worth discussing if the namespacing should be done for everyone or if we should have a #[diplomat::cpp::namespace(foo)] attribute instead

it just puts Bar in the global (capi) namespace.

This isn't correct, the capi namespace is for the C headers. It puts it in the global namespace.

Manishearth commented 2 years ago

I think what we should do here is have diplomat::cpp::namespace(icu4x) and diplomat::c::namespace(ICU4X) (and nothing for JS), where the C++ implementation uses a C++ namespace (and can be applied to the bridge module or the individual type), and the C implementation just tacks on a prefix.

Manishearth commented 2 years ago

If we do this we should do this in the holistic backend attribute system designed in #235

Manishearth commented 1 year ago

n.b. currently we do not do any attribute merging in the AST except for CFGs (https://github.com/rust-diplomat/diplomat/blob/133a10534cad9f29e7b9ddfc1aea687c5e3b8274/core/src/ast/attrs.rs#L24)

For namespacing it would be nice to use a similar mechanism to "inherit" namespace and rename attributes from the module def.

robertbastian commented 11 months ago

I think there are two things here:

I think we should deal with these separately. For Dart I don't need any fancy modularisation (because Dart doesn't support that), but I do want generate a Dart API without ICU4X.. prefixes. So in icu_capi I'd like to change

#[diplomat::bridge]
mod ffi {
    struct ICU4XLocale(...)
}

to

#[diplomat::bridge(prefix = "ICU4X")]
mod ffi {
    struct Locale(...)
}

to allow using nicer names in Dart.

Manishearth commented 11 months ago

I'd call it c_prefix but yeah

Manishearth commented 8 months ago

Splitting out C++ into https://github.com/rust-diplomat/diplomat/issues/427

Manishearth commented 1 month ago

Closing in favor of https://github.com/rust-diplomat/diplomat/issues/688