1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* mutstatic.rs
*
* Developed by Tim Walls <tim.walls@snowgoons.com>
* Copyright (c) All Rights Reserved, Tim Walls
*/
//! Mutable static singleton helper
#[doc(hidden)]
#[macro_export]
macro_rules! mut_singleton {
($type:ty, $name:ident, $accessor:ident, $constructor:expr) => {
static mut $name : Option<$type> = None;
/**
* Return a reference to a global singleton instance of $type.
* Any necessary inititalisation/construction will be performed on the
* first call to this method.
*/
#[allow(dead_code)]
#[inline(always)]
pub fn $accessor() -> &'static mut $type {
unsafe {
avr_oxide::hal::concurrency::interrupt::isolated(||{
if $name.is_none() {
core::ptr::replace(&mut $name, Some($constructor));
}
match &mut $name {
None => panic!(),
Some(instance) => instance
}
})
}
}
}
}