BuFFI: Generate ergonomic C++ APIs for your Rust code
Hey everyone! We have just released the first version of BuFFI, a tool to generate ergonomic (and safe!) C++ APIs to call your Rust code from C++.
https://crates.io/crates/buffi
The name is a combination of "buffer" and "FFI", because one of the core concepts is to replace every type in the C-API with byte buffers. This allows us to simplify the "extern C" functions on the Rust side as well as the functions on the C++ side. I recently gave presentations at EuroRust and RustLab explaining our work and I've included some slides here:
https://github.com/GiGainfosystems/BuFFI/blob/main/knowledge/RustLab2024.pdf
At my workplace this has allowed us to remove hundreds of lines of boilerplate and has eliminated many sources of possible memory leaks or erroneous pointer handling. You can simply add a macro to your pure Rust API and after calling the BuFFI function to generate the necessary code, you are already good to go. A basic example is included as well.
Since this is the first release of BuFFI, some things are still a WIP. Make sure to check out the README file to learn more about these points. We are also working on more examples, e.g. for async functions (check the slides to see how async functions are handled!).
The current release supports the 1.82.0 toolchain (check the README for an explanation why this is the current situation) and going forward, every time a new stable version of Rust is released, we will try to release a new version of BuFFI to stay compatible. Make sure to grab the right version for you!
We do expect some incompatibilities eventually as other projects give this a go, but we are optimistic that most cases can be quickly figured out together, so make sure to open an issue if you run into any problems.