Interoperability

Interoperability between Rust and C code is always dependenton transforming data between the two languages.For this purposes there are two dedicated modulesin the stdlib calledstd::ffi andstd::os::raw.

std::os::raw deals with low-level primitive types that canbe converted implicitly by the compilerbecause the memory layout between Rust and Cis similar enough or the same.

std::ffi provides some utility for converting more complextypes such as Strings, mapping both &str and Stringto C-types that are easier and safer to handle.

Neither of these modules are available in core, but you can find a #![no_std]compatible version of std::ffi::{CStr,CString} in the cstr_core crate, andmost of the std::os::raw types in the cty crate.

Rust typeIntermediateC type
StringCStringchar
&strCStrconst char
()c_voidvoid
u32 or u64c_uintunsigned int
etc

As mentioned above, primitive types can be convertedby the compiler implicitly.

  1. unsafe fn foo(num: u32) {
  2. let c_num: c_uint = num;
  3. let r_num: u32 = c_num;
  4. }

Interoperability with other build systems

A common requirement for including Rust in your embedded project is combiningCargo with your existing build system, such as make or cmake.

We are collecting examples and use cases for this on our issue tracker inissue #61.

Interoperability with RTOSs

Integrating Rust with an RTOS such as FreeRTOS or ChibiOS is still a work inprogress; especially calling RTOS functions from Rust can be tricky.

We are collecting examples and use cases for this on our issue tracker inissue #62.