C++ code can call functions defined in Rust, provided that the parameter and return types are supported by Crubit:
As a special case, functions also support reference parameters to supported types, with some restrictions to ensure safety. See References.
Given the following Rust crate:
cs/file:examples/rust/function/example.rs function:add_two_integers
Crubit will generate the following function declaration, which calls into accompanying glue code:
cs/file:examples/rust/function/example_generated.h function:add_two_integers
unsafe
functionsC++ does not have an unsafe
marker at this time. In the future, Crubit may introduce a way to mark unsafe
functions to help increase the reliability of C++ callers.
In general, Rust references are not exposed to C++. However, some Rust functions which accept reference parameters do get mapped to C++ functions accepting C++ references:
'static
, for example.mut
reference parameter, it is the only reference parameter.This set of rules is intended to describe a safe subset of Rust functions, which do not introduce substantial aliasing risk to a mixed C++/Rust codebase.
For example, the following Rust functions will receive C++ bindings, and can be called from C++:
fn foo(&self) {} fn foo(_: &mut i32) {} fn foo(_: &i32, _: &i32) {}
However, none of the below will receive bindings:
fn foo(_: &'static i32) {} // 'static lifetime is bound fn foo(_: &&i32) {} // Reference in non-parameter type fn foo(_: &mut i32, _: &i32) {} // More than one reference, one of which is mut fn foo(_: &'a i32) {} // 'a is not a lifetime parameter of `foo`
Returned references are still not supported, and references which are bound to some lifetime (e.g. 'static
) are also still not supported.
If you wish to accept more than one reference/pointer in C++, a raw pointer (*const T
, *mut T
) can be used instead. However, all of the usual unsafe
caveats apply.