Comments (5)
This would require submodule declarations within the
ffi
module, wouldn't it?
I meant that you would put this outside of mod ffi
to organize the public Rust API. mod ffi
would then be private.
from cxx.
some of the structs might have identical names.
I am sure you figured this out but for the benefit of anyone else reading: this means you would need to use cxx_name
to deconflict them.
#[cxx::bridge]
mod bar {
#[namespace = "foo"]
extern "C++" {
#[cxx_name = "Foo"]
type FooFoo = crate::foo::Foo;
}
#[namespace = "bar"]
struct Foo {
foo: FooFoo,
}
}
from cxx.
I am not entirely sure what you mean by different namespaces, but if it's just the C++ namespace
that needs to be different, then like this:
#[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,
}
}
from cxx.
Thanks a lot for the quick reply! I need different Rust modules too because some of the structs might have identical names.
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; }
This would require submodule declarations within the ffi
module, wouldn't it? E.g. something like this:
#[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 mod foo
and mod bar
declaration.
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, } }
This works, thanks a lot! I didn't know that #[namespace]
attributes are supported on extern "C++"
blocks too.
from cxx.
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 ffi
module with its namespace name to avoid name conflicts. For the C++ API, i use the namespace
and cxx_name
attributes to recreate the namespace and original name. For the Rust API, I use re-exports as suggested above.
from cxx.
Related Issues (20)
- Advice/example on docs.rs strategy HOT 2
- Feature Request: Add support for nested Vector types HOT 1
- Building two CXX C++ bindings with the same symbols but in different namespaces. HOT 2
- Including bridge from another crate
- Error in configuring crates_universe when included as a Bazel module HOT 6
- Error compiling `cxx.cc` on Windows HOT 1
- Proposal: implementing native C++ std::error_code
- Link failure when using CXX-backed function from another crate HOT 1
- Unsupported element type of Vec<[u64; 3]>
- How to include C++ files whose directory is given by an environment variable? HOT 1
- Error compiling demo to wasm on macOS HOT 5
- Is there a way to expose a C++ class that is itself a wrapper on `std::shared_ptr`?
- How can I `include!` a file in OUT_DIR
- How can I return a vector of non-trivial type of C++ to Rust?
- LNK2019 unresolved symbol for Vec<SharedStruct> using MSVC
- Using CXX in multiple DLLs always results in undefined behavior?
- Does the core safety claim need rephrasing? HOT 6
- Example of using cxx in an external mixed bazel C++ & Rust environment?
- Undefined symbols for architecture arm64: "rust::cxxbridge1::String::String(char const *)" HOT 1
- Build fails with "unexpected token" error when DOCS_RS env var is set HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cxx.