cxx
cxx copied to clipboard
Exposing trait methods to C++
We can expose a trait object to C++ something like this:
trait MyData {
fn get_data(&self) -> Result<u64>;
}
type DynMyData = Box<dyn MyData>;
#[cxx::bridge]
mod ffi {
extern "Rust" {
type DynMyData;
fn read_data() -> Result<Box<DynMyData>>;
fn get_data(d: &DynMyData) -> Result<u64>;
}
}
fn get_data(d: &DynMyData) -> Result<u64> {
d.get_data();
}
But the trait has many methods and it gets quite tedious writing them all out like that. Bit of a long shot - kind of getting into SWIG territory - but Is there any way to expose the trait methods to C++ automatically?