Function r3::bind::bind_uninit

source ·
pub const fn bind_uninit<'pool, T, C>(
    cfg: &mut Cfg<'pool, C>
) -> Bind<'pool, C::System, MaybeUninit<T>>where
    T: 'static,
    C: CfgStatic,
Expand description

A shorthand for Bind::define().init(MaybeUninit::uninit).finish(cfg).

This can be used to create a storage with the 'static lifetime duration that is initialized lazily.

Example

The following example uses bind_uninit as an alternative to cortex_m::singleton! with no runtime checking.

use r3::{
    kernel::{StaticTask, StaticTimer},
    bind::{bind, bind_uninit},
    time::Duration,
    prelude::*,
};
use core::mem::MaybeUninit as Mu;

const fn configure_app<C>(cfg: &mut Cfg<C>)
where
    C: ~const traits::CfgBase<System = System>,
{
    bind(
        (bind_uninit(cfg).take_mut(), bind_uninit(cfg).take_mut()),
        |cell0: &'static mut Mu<_>, cell1: &'static mut Mu<_>| {
            // Put a value in `cell0` and get a `'static` reference to it
            let ref0: &'static mut i32 = cell0.write(42);

            // And so on
            let ref1: &'static mut &'static mut i32 = cell1.write(ref0);

            assert_eq!(ref1, &mut &mut 42);
        }
    ).unpure().finish(cfg);
}