autocxx icon indicating copy to clipboard operation
autocxx copied to clipboard

autorust: generate cxx bindings for C++ -> Rust calls

Open adetaylor opened this issue 4 years ago • 3 comments

The purpose of autocxx is to skip the need to describe the C++/Rust interface in the cxx IDL.

At the moment, this works for existing C++ APIs.

The concept could be extended to existing Rust APIs, by adding some sort of attribute macro e.g.

#[autocxx::gen]
struct RustStruct {
  a: u32,
}

#[autocxx::gen]
pub fn doSomething(s: RustStruct) {
   ...
}

These too could contribute to the #[cxx::bridge] mod we generate, but functions would go into an extern "Rust" section rather than the current extern "C++" section.

We already have most of the pipeline here:

  • We parse the Rust file in a codegen tool using syn
  • That tool already generated a #[cxx::bridge] mod
  • We have macros which effectively replace the real Rust declarations with something else derived from that codegen tool.

This would be a significant direction change/enhancement for autocxx, so this isn't going to happen any time soon. Recording the idea for the record!

adetaylor avatar Jul 08 '21 20:07 adetaylor

Does this mean that there's no way to populate the ffi module with extern "Rust" section? Or is there a workaround to do it at the moment?

Geobert avatar Jul 09 '21 10:07 Geobert

Sorry for missing your question!

That's right, there is no way to do that automatically; however you can do this.

adetaylor avatar Jul 23 '21 16:07 adetaylor

No worries! Our C++ codebase is a bit too complex for autocxx so unfortunately we can't use it yet, but that's a great tip to have in mind!

Geobert avatar Jul 24 '21 09:07 Geobert