[][src]Struct ruspiro_singleton::Singleton

pub struct Singleton<T> { /* fields omitted */ }

The Singleton wrapper stores any type

Methods

impl<T> Singleton<T>[src]

pub const fn new(data: T) -> Singleton<T>[src]

Create a new singleton instance to be used in a static variable. Only const fn constructors are allows here. If this is not sufficient the singleton may be further wrapped by a lazy_static! available as external crate from crates.io

pub fn take_for<F, R>(&self, f: F) -> R where
    F: FnOnce(&mut T) -> R, 
[src]

Take the stored singleton for whatever operation and prevent usage by other cores Safe access to the singleton mutable instance is guarantied inside the given closure.

pub unsafe fn use_weak_for<F, R>(&self, f: F) -> R where
    F: FnOnce(&T) -> R, 
[src]

Unsafe weak access to a singleton for a specific operation. Access by other cores is not permitted. This access does not enforce any lock nor guarantees safe atomic access to the instance. However, it is usefull in read-only access scenarios like inside interrupt handlers to ensure they do not depend on any lock that could lead to a dead-lock situation. The access to the singleton is imutable to enforce read-only access to the same.

Example

fn sample() {
    unsafe {
        MY_SINGLETON.use_weak_for(|my| {
            // do something with [my]
            let _ = my.any_imutable_function();
        })
    };
}

Auto Trait Implementations

impl<T> Unpin for Singleton<T> where
    T: Unpin

impl<T> Send for Singleton<T> where
    T: Send

Blanket Implementations

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]