-
Notifications
You must be signed in to change notification settings - Fork 333
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
Split shared structs across multiple modules/namespaces #1301
Comments
I am not entirely sure what you mean by different namespaces, but if it's just the C++ #[cxx::bridge]
mod ffi {
#[namespace = "foo"]
struct Foo {
i: i32,
}
#[namespace = "bar"]
struct Bar {
foo: Foo,
}
} If you additionally want them in different Rust modules, you can do: pub mod foo {
pub use crate::ffi::foo::Foo;
}
pub mod bar {
pub use crate::ffi::bar::Bar;
} or, like this: #[cxx::bridge]
mod foo {
#[namespace = "foo"]
struct Foo {
i: i32,
}
}
// in a different file
#[cxx::bridge]
mod bar {
#[namespace = "foo"]
extern "C++" {
type Foo = crate::foo::Foo;
}
#[namespace = "bar"]
struct Bar {
foo: Foo,
}
} |
Thanks a lot for the quick reply! I need different Rust modules too because some of the structs might have identical names.
This would require submodule declarations within the #[cxx::bridge]
mod ffi {
mod foo {
pub struct Foo {
foo: i32,
}
}
mod bar {
pub struct Bar {
bar: super::foo::Foo,
}
}
} Unfortunately, this does result in "unsupported item" errors for both the
This works, thanks a lot! I didn't know that |
I meant that you would put this outside of |
I am sure you figured this out but for the benefit of anyone else reading: this means you would need to use #[cxx::bridge]
mod bar {
#[namespace = "foo"]
extern "C++" {
#[cxx_name = "Foo"]
type FooFoo = crate::foo::Foo;
}
#[namespace = "bar"]
struct Foo {
foo: FooFoo,
}
} |
Thanks! I ended up with with the following, maybe this is useful for someone else too: #[cxx::bridge]
mod ffi {
#[namespace = "namespace"]
#[cxx_name = "Name"]
struct namespace__Name {
field: other_namespace__OtherName,
}
#[namespace = "other_namespace"]
#[cxx_name = "OtherName"]
struct other_namespace__OtherName {
field: i32,
}
}
pub mod namespace {
pub use crate::ffi::namespace__Name as Name;
}
pub mod other_namespace {
pub use crate::ffi::other_namespace__OtherName as OtherName;
} So I prefix every struct name in the |
Hi, I want to create multiple shared structs in different namespaces. Some of the structs have fields that reference other structs. For example:
Is it possible to implement this using
cxx
? I tried the following:#[cxx::bridge]
attributes for the two modules with differentnamespace
argumentsfoo
module frombar
. However:use
statements are not allowedsuper::foo::Foo
are not allowed eitherextern "C++"
types, not shared types#[cxx::bridge]
module with submodulesfoo
andbar
into a common module, e.g. namedffi
#[cxx::bridge]
attribute to theffi
moduleIs there perhaps another way to achieve this? Thanks!
The text was updated successfully, but these errors were encountered: