avr-oxide 0.2.2

An extremely simple Rusty operating system for AVR microcontrollers
/* 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
          }
        })
      }
    }
  }
}