

Safe Interoperability between Rust and C++ with CXX
source link: https://www.infoq.com/news/2020/12/cpp-rust-interop-cxx/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Safe Interoperability between Rust and C++ with CXX
Dec 06, 2020 3 min read
CXX enables calling C++ code from Rust and vice versa through safe low-level bindings so you do not have to create your foreign function interface on top of unsafe C-style signatures. InfoQ has taken the chance to speak with CXX creator David Tolnay.
CXX uses static analysis of the types and function signatures to protect both Rust's and C++'s invariants. Then it uses a pair of code generators to implement the boundary efficiently on both sides together with any necessary static assertions for later in the build process to verify correctness.

According to Tolnay, the bridge created using CXX has zero or negligible overhead, not requiring copying, serialization, memory allocation, or runtime checks.
CXX aims to make it possible to write perfectly idiomatic code on both sides of the bridge, meaning you can write idiomatic Rust code using idiomatic C++ code, and vice versa. To this aim, CXX provides built-in bindings for standard library types such as strings, vectors, and so on.
This is how a Rust program using CXX to call C++ code may look like:
#[cxx::bridge]
mod ffi {
unsafe extern "C++" {
include!("cxx-demo/include/blobstore.h");
type BlobstoreClient;
fn new_blobstore_client() -> UniquePtr<BlobstoreClient>;
}
}
fn main() {
let client = ffi::new_blobstore_client();
}
InfoQ has taken the chance to speak with CXX creator David Tolnay.
InfoQ: What is your background and how would you describe your involvement with the Rust project?
David Tolnay: Software engineer in the San Francisco area with a focus on codebase design: the libraries, patterns, and practices that enable developers to operate successfully in large (10 million+ line) codebases. I have been working with Rust since 2016, and have created and maintain many widely used Rust libraries as side projects. These days around 30% of all open source packages on the Rust package registry depend directly on at least one library by me.
InfoQ: What is the rationale for CXX? What was your motivation to create it?
Tolnay: Prior to CXX, Rust interop with C was fine (unsafe, though in a way that C developers would be comfortable with) while interop with C++ was "make it look as much like C as you can manage."
That state of the world was something teams with Rust expertise were generally able to navigate, calling C++ code where needed to get their job done. However, it was by no means pleasant to write or maintain.
Meanwhile teams just getting started with Rust really struggled. For someone introducing Rust into a C++ codebase, or especially someone coming into Rust with neither Rust nor C++ experience, being dumped into unsafe code so early on is a showstopper and a reason to decide against Rust.
CXX leverages the idea that Rust and modern C++ are way more similar than either is to C. We can interoperate at a suitable level of abstraction without unsafe C in between, and do so without sacrificing efficiency on the language boundary.
InfoQ: What is/was the biggest challenge in CXX design/implementation?
Tolnay: Delivering a good experience for Cargo users. Cargo is the build system commonly used by open source Rust projects. Whereas some build systems like Bazel are highly designed around multi-language builds, Cargo is not that.
It's taken a few thorough redesigns but CXX on Cargo now comes close to matching the experience of CXX in a natively polyglot build system. That pleasantly encompasses Rust depending on C++, C++ depending on Rust, and C++ depending on C++ from other crates.
InfoQ: How would you describe CXX current status? In which scenarios would you suggest its use at the moment?
Tolnay: I have rolled out CXX in a large hybrid-Rust–C++ codebase in which many engineers have used it successfully with positive reception. There is more to improve but it delivers a developer experience that I stand behind as my recommendation for all Rust–C++ interop.
InfoQ: What’s on CXX roadmap?
Tolnay: The biggest idea not yet explored is first-class async function support. We support async already but it's not quite as seamless as being able to directly
.await
an arbitrary async C++ function andco_await
an arbitrary async Rust function.
If you want to try CXX out, you can start with its tutorial and GitHub repository.
Recommend
-
15
CXX — safe FFI between Rust and C++ This library provides a safe mechanism for calling C++ code from Rust and Rust code from C++, not subject to the many ways that things can go wrong when usin...
-
9
The Relation between "Rust and Safe Programming" !! Knoldus Blog Audio Reading Time: 5 minutes The Rust
-
3
因为临时发现需要一个枚举量到字符串的转换器,所以干脆梳理了一遍古往今来的枚举类型的变化。 于是奇怪的冷知识又增加了。 枚举类型
-
11
This is a post that I’ve been meaning to write already for some time. It’s based on work I did and a presentation I gave at our Sca...
-
9
At KDAB, we have been investigating how to integrate Rust with Qt in a safe and idiomatic way. The solution we are currently working on is called CXX-Qt. It’s available in the
-
8
CXX库提供Rust和C+ 之间的安全相互调用 CXX库提供了一种安全的机制,用于从 Rust 调用 C++ 代码和从 C++ 调用 Rust 代码。它开辟了一个共性机制,其中 Rust 和 C++ 在语义上非常相似,并指导程序员在这个机制内有效地表达他们的语言边界。CXX 填...
-
7
序言Permalink 很早以前,我有计划要为 GTD 做一个定时报闹的库,原因在于大多数现在的 GTD 软件、TODO、行...
-
5
Introducing `cxx-async` Patrick Walton · August 19, 2022 Update 9/6/2022: Fixed a potential use-after-free on the C++ side if the future got dropped. Thanks to David Tolnay for poin...
-
5
One step forward, an easier interoperability between Rust and HaskellJanuary 26, 2023 · 11 min read
-
11
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK