RusPiRo Singleton crate
This crate provides an easy to use singleton pattern that is safe to be used across cores.
Usage
To use this crate simply add the dependency to your Cargo.toml
file:
[]
= "0.4.3"
Once done on any rust file you can define a static variable as Singleton
of any type for safe cross core access in two different ways. The first variant requires to provide an instance of the data to be wrapped inside the singleton while defining the same.
// define the static variable
static DEMO: = new;
// define the type to be accessible as singleton
// implement the type that should provided as singlton
The second variant allows to pass a closure to the initialization of the Singleton
that will be evaluated at first access to the contents of it.
!HINT! Safe lazy initialization is ensured using atomics. On the Raspberry Pi atmomic operations require the MMU to be configured and active. Otherwise the executing CPU core will hang when trying to execute the atomic operation.
// define the static variable with an inizialization closure
static DEMO: = lazy;
// define the type to be accessible as singleton
// implement the type that should provided as singlton
To use the data sealed with the Singleton
call either of the two methods with_ref
and with_mut
providing a closure accessing the data immuable or mutable.
License
Licensed under Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) or MIT (LICENSE-MIT or http://opensource.org/licenses/MIT)) at your choice.