#RusPiRo #singleton #raspberrypi

nightly no-std ruspiro-singleton

Simple and easy to use singleton pattern

9 unstable releases

0.4.1 Nov 16, 2020
0.4.0 Nov 9, 2020
0.3.1 Sep 11, 2020
0.3.0 Dec 11, 2019
0.0.2 Jul 29, 2019

#58 in No standard library

Download history 9/week @ 2020-08-04 23/week @ 2020-08-11 18/week @ 2020-08-18 19/week @ 2020-08-25 29/week @ 2020-09-01 156/week @ 2020-09-08 16/week @ 2020-09-15 12/week @ 2020-09-22 19/week @ 2020-09-29 15/week @ 2020-10-06 12/week @ 2020-10-13 18/week @ 2020-10-20 12/week @ 2020-10-27 35/week @ 2020-11-03 37/week @ 2020-11-10 36/week @ 2020-11-17

132 downloads per month
Used in 7 crates (6 directly)

Apache-2.0

13KB
105 lines

RusPiRo Singleton crate

This crate provides an easy to use singleton pattern that is safe to be used across cores.

Travis-CI Status Latest Version Documentation License

Usage

To use this crate simply add the dependency to your Cargo.toml file:

[dependencies]
ruspiro-singleton = "0.4.1"

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:Singleton<Demo> = Singleton::new(Demo::new());

// define the type to be accessible as singleton
struct Demo {
    pub count: u32,
}

// implement the type that should provided as singlton
impl Demo {
    pub const fn new() -> Self {
        Demo {
            count: 0,
        }
    }
}

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:Singleton<Box<Demo>> = Singleton::lazy(&|| {
    Box::new(
        Demo::new()
    )
});

// define the type to be accessible as singleton
struct Demo {
    pub count: u32,
}

// implement the type that should provided as singlton
impl Demo {
    pub const fn new() -> Self {
        Demo {
            count: 0,
        }
    };
}

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.

fn main() {
    // safely use the singleton inside the closure passed to [with_mut] to update it's contents
    DEMO.with_mut(|d| {
        d.count += 10;
    });

    // safely use the singleton inside the closure passed to [with_ref] if read-only access is required
    DEMO.with_mut(|d| {
        println!("Value: {}", d.count);
    });

    // you may also return a value from the singleton to work with it after the safe singleton access
    let val = DEMO.with_ref(|d| {
        if d.count != 0 {
            true
        } else {
            false
        }
    });
}

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.

Dependencies

~62KB