133d722a9Sopenharmony_ci{{#title Core concepts — Rust ♡ C++}} 233d722a9Sopenharmony_ci# Core concepts 333d722a9Sopenharmony_ci 433d722a9Sopenharmony_ciThis page is a brief overview of the major concepts of CXX, enough so that you 533d722a9Sopenharmony_cirecognize the shape of things as you read the tutorial and following chapters. 633d722a9Sopenharmony_ci 733d722a9Sopenharmony_ciIn CXX, the language of the FFI boundary involves 3 kinds of items: 833d722a9Sopenharmony_ci 933d722a9Sopenharmony_ci- **Shared structs** — data structures whose fields are made visible to 1033d722a9Sopenharmony_ci both languages. The definition written within cxx::bridge in Rust is usually 1133d722a9Sopenharmony_ci the single source of truth, though there are ways to do sharing based on a 1233d722a9Sopenharmony_ci bindgen-generated definition with C++ as source of truth. 1333d722a9Sopenharmony_ci 1433d722a9Sopenharmony_ci- **Opaque types** — their fields are secret from the other language. 1533d722a9Sopenharmony_ci These cannot be passed across the FFI by value but only behind an indirection, 1633d722a9Sopenharmony_ci such as a reference `&`, a Rust `Box`, or a C++ `unique_ptr`. Can be a type 1733d722a9Sopenharmony_ci alias for an arbitrarily complicated generic language-specific type depending 1833d722a9Sopenharmony_ci on your use case. 1933d722a9Sopenharmony_ci 2033d722a9Sopenharmony_ci- **Functions** — implemented in either language, callable from the other 2133d722a9Sopenharmony_ci language. 2233d722a9Sopenharmony_ci 2333d722a9Sopenharmony_ci```rust,noplayground,focuscomment 2433d722a9Sopenharmony_ci# #[cxx::bridge] 2533d722a9Sopenharmony_ci# mod ffi { 2633d722a9Sopenharmony_ci // Any shared structs, whose fields will be visible to both languages. 2733d722a9Sopenharmony_ci# struct BlobMetadata { 2833d722a9Sopenharmony_ci# size: usize, 2933d722a9Sopenharmony_ci# tags: Vec<String>, 3033d722a9Sopenharmony_ci# } 3133d722a9Sopenharmony_ci# 3233d722a9Sopenharmony_ci# extern "Rust" { 3333d722a9Sopenharmony_ci // Zero or more opaque types which both languages can pass around 3433d722a9Sopenharmony_ci // but only Rust can see the fields. 3533d722a9Sopenharmony_ci# type MultiBuf; 3633d722a9Sopenharmony_ci# 3733d722a9Sopenharmony_ci // Functions implemented in Rust. 3833d722a9Sopenharmony_ci# fn next_chunk(buf: &mut MultiBuf) -> &[u8]; 3933d722a9Sopenharmony_ci# } 4033d722a9Sopenharmony_ci# 4133d722a9Sopenharmony_ci# unsafe extern "C++" { 4233d722a9Sopenharmony_ci // One or more headers with the matching C++ declarations for the 4333d722a9Sopenharmony_ci // enclosing extern "C++" block. Our code generators don't read it 4433d722a9Sopenharmony_ci // but it gets #include'd and used in static assertions to ensure 4533d722a9Sopenharmony_ci // our picture of the FFI boundary is accurate. 4633d722a9Sopenharmony_ci# include!("demo/include/blobstore.h"); 4733d722a9Sopenharmony_ci# 4833d722a9Sopenharmony_ci // Zero or more opaque types which both languages can pass around 4933d722a9Sopenharmony_ci // but only C++ can see the fields. 5033d722a9Sopenharmony_ci# type BlobstoreClient; 5133d722a9Sopenharmony_ci# 5233d722a9Sopenharmony_ci // Functions implemented in C++. 5333d722a9Sopenharmony_ci# fn new_blobstore_client() -> UniquePtr<BlobstoreClient>; 5433d722a9Sopenharmony_ci# fn put(&self, parts: &mut MultiBuf) -> u64; 5533d722a9Sopenharmony_ci# fn tag(&self, blobid: u64, tag: &str); 5633d722a9Sopenharmony_ci# fn metadata(&self, blobid: u64) -> BlobMetadata; 5733d722a9Sopenharmony_ci# } 5833d722a9Sopenharmony_ci# } 5933d722a9Sopenharmony_ci``` 6033d722a9Sopenharmony_ci 6133d722a9Sopenharmony_ciWithin the `extern "Rust"` part of the CXX bridge we list the types and 6233d722a9Sopenharmony_cifunctions for which Rust is the source of truth. These all implicitly refer to 6333d722a9Sopenharmony_cithe `super` module, the parent module of the CXX bridge. You can think of the 6433d722a9Sopenharmony_citwo items listed in the example above as being like `use super::MultiBuf` and 6533d722a9Sopenharmony_ci`use super::next_chunk` except re-exported to C++. The parent module will either 6633d722a9Sopenharmony_cicontain the definitions directly for simple things, or contain the relevant 6733d722a9Sopenharmony_ci`use` statements to bring them into scope from elsewhere. 6833d722a9Sopenharmony_ci 6933d722a9Sopenharmony_ciWithin the `extern "C++"` part, we list types and functions for which C++ is the 7033d722a9Sopenharmony_cisource of truth, as well as the header(s) that declare those APIs. In the future 7133d722a9Sopenharmony_ciit's possible that this section could be generated bindgen-style from the 7233d722a9Sopenharmony_ciheaders but for now we need the signatures written out; static assertions verify 7333d722a9Sopenharmony_cithat they are accurate. 7433d722a9Sopenharmony_ci 7533d722a9Sopenharmony_ci<br><br> 7633d722a9Sopenharmony_ci 7733d722a9Sopenharmony_ciBe aware that the design of this library is intentionally restrictive and 7833d722a9Sopenharmony_ciopinionated! It isn't a goal to be flexible enough to handle an arbitrary 7933d722a9Sopenharmony_cisignature in either language. Instead this project is about carving out a highly 8033d722a9Sopenharmony_ciexpressive set of functionality about which we can make powerful safety 8133d722a9Sopenharmony_ciguarantees today and extend over time. You may find that it takes some practice 8233d722a9Sopenharmony_cito use CXX bridge effectively as it won't work in all the ways that you may be 8333d722a9Sopenharmony_ciused to. 8433d722a9Sopenharmony_ci 8533d722a9Sopenharmony_ci<br> 86