Stateful embedded library in Rust

103 Views Asked by At

I want to develop a library in Rust for microcontrollers that holds some state information. This state data cannot be transferred to the caller of the library. I am using #![no_std]. The library should be suitable for bare metal applications and RTOS like Zephyr OS or FreeRTOS. My approach so far was to use lazy_static:

use heapless::Vec;

lazy_static! {
    static ref nodes: Vec<u8, 100> = Vec::new();
}



fn foo(){
 nodes.push(1); 
}

This example doesn't compile. I am getting the following error:

cannot borrow data in dereference of `nodes` as mutable
trait `DerefMut` is required to modify through a dereference, but it is not implemented for `nodes`

Googling this error I fond out that a mutex is needed, see link. I don't know how to achieve this in #![no_std] code.

However more important is the question: What is the idiomatic way of handling global state when #![no_std] is used?

0

There are 0 best solutions below