Generating ergonomic C++ APIs using Rustdoc, procedural macros, and Serde

How to integrate your new shiny Rust code into an existing codebase is a common problem. For this you might need to provide an API so that your Rust code can be accessed from the non-Rust side, for example from a C++ codebase. Rust only provides tools out of the box to define a low level C API, and even that can be troublesome. In this talk we want to demonstrate how you can use procedural macros, Rustdoc's unstable JSON format, and Serde to generate an idiomatic and ergonomic C++ API based on your existing Rust API.

LEVEL: Advanced

Place
RustLab Discovery
Length
25 min
When
November 10th, 2024
13:00

Abstract

DESCRIPTION:

This talk introduces a novel approach to generate C++ APIs from an existing Rust API by using procedural macros, Rustdoc, and Serde. Functions and impl blocks which should be exposed in the C++ API are annotated with a procedural macro attribute. This macro generates low level extern "C" functions for the corresponding function definitions. Arguments and return values of these functions are passed as byte buffers in this case. Ultimately, Rustdoc's unstable JSON format is used to generate a set of low level bindings for the generated extern "C" functions and, on top of that, a set of ergonomic high level C++ bindings. An intermediate layer handles all the required (de-)serializing of the argument/return type byte buffers to acquire the corresponding native representations.


ADDITIONAL NOTES:

This talk demonstrates how you can generate a "nice to work with" C++ API based on a given Rust API. The proposed workflow is based on procedural macros to generate extern "C" functions, Serde for converting data into the right representation on each side of the FFI interface, and the unstable Rustdoc JSON format to collect and generate the necessary headers for the C++ side. The talk is based on a framework we have developed internally and have been using in production for over a year now. It will start with showing how we handled FFI generation in the past before moving to our new framework. After that it will show in detail how the new framework works internally and which improvements it has over other solutions. Overall our new framework allows for more ergonomic API surfaces on both sides of the FFI interface at the cost of some performance overhead for serialization/deserialization.
There will also be a comparison with existing tools like cbindgen and cxx.

RustLab is a conference made by Develer.
Develer is a company based in Campi Bisenzio, near Florence. Our motto is : "Technology to give life to your products". We produce hardware and software to create exceptional products and to improve industrial processes and people's well being.
In Develer we have passion for the new technologies and we offer our clients effective solutions that are also efficient, simple and safe for the end users. We also believe in a friendly and welcoming environment where anybody can give their contribution. This passion and this vision are what we've been driven to organize our conference "made by developers for developers".

Subscribe to our newsletter

We hate spam just as much as you do, which is why we promise to only send you relevant communications. We respect your privacy and will never share your information with third parties.
©2024 RustLab | The international conference on Rust in Florence-Design & devCantiere Creativo-Made withDatoCMS