blob: 8cae381b1d12292a37bdbe8700a318037219624b [file] [log] [blame] [view]
Lukasz Anforowiczdcdb524a2025-03-24 19:26:411# Rust FFI
2
3This document tries to provide guidance for C++/Rust FFI.
4CLs to improve this guidance are welcomed.
5
6## General guidance
7
8Chromium recommends using [the `cxx` crate](https://cxx.rs/) for C++/Rust FFI.
9For introductory guidance, please see
10[the `cxx` chapter](https://google.github.io/comprehensive-rust/chromium/interoperability-with-cpp.html)
11in the Chromium day of the Comprehensive Rust course.
12
13Chromium also supports the following tools:
14
15* [`bindgen`](https://rust-lang.github.io/rust-bindgen/) - see
16 `//build/rust/rust_bindgen.gni` for usage instructions.
17
18At this point Chromium's `//build/rust/*.gni` templates do not support other FFI
19tools like:
20
21* [`cbindgen`](https://github.com/mozilla/cbindgen)
22* [`crubit`](https://github.com/google/crubit)
23
24### `cxx` guidance
25
26#### Best practices
27
28* Generate C++ side of bindings into a project-specific or crate-specific
29 `namespace`. For example: `#[cxx::bridge(namespace = "some_cpp_namespace")]`.
30* Maintain binding declarations in a **single** `#[cxx::bridge]` declaration.
31 `cxx` supports reusing types across multiple `bridge`s, but there are some
32 rough edges.
33
34#### Suggestions
35
36TODO: Provide some examples or suggestions on how to structure FFI bindings
37(even if these suggestions wouldn't necessarily rise to the level of "best
38practices").