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
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//! Initialize variables at runtime which then behave like static variables.
//!
//! ```rust
//! extern crate late_static;
//! use late_static::LateStatic;
//!
//! struct Foo {
//!     pub value: u32,
//! }
//!
//! static FOO: LateStatic<Foo> = LateStatic::new();
//!
//! fn main() {
//!     unsafe {
//!         FOO.assign(Foo { value: 42 });
//!     }
//!     println!("{}", FOO.value);
//! }
//! ```
#![cfg_attr(not(test), no_std)]

use core::cell::UnsafeCell;

/// Static value that is manually initialized at runtime.
pub struct LateStatic<T> {
    val: UnsafeCell<Option<T>>
}

unsafe impl<T: Send> core::marker::Send for LateStatic<T> {}
unsafe impl<T: Send> core::marker::Sync for LateStatic<T> {}

impl<T> LateStatic<T> {
    /// Construct a LateStatic.
    pub const fn new() -> Self {
        LateStatic {
            val: UnsafeCell::new(None),
        }
    }

    /// Assign a value to the late static.
    ///
    /// This only works once. A second call to assign for a given variable will panic.
    ///
    /// This is completely unsafe if there is even the slightest chance of another
    /// thread trying to dereference the variable.
    pub unsafe fn assign(&self, val: T) {
        let option: &mut Option<T> = &mut *self.val.get();
        if option.is_some() {
            panic!("Second assignment to late static");
        }
        else {
            *option = Some(val);
        }
    }

    /// Whether a value is assigned to this LateStatic.
    pub unsafe fn is_assigned(&self) -> bool {
        let option: &Option<T> = &*self.val.get();
        option.is_some()
    }
}

impl<T> core::ops::Deref for LateStatic<T> {
    type Target = T;

    fn deref(&self) -> &T {
        unsafe {
            let option: &Option<T> = &*self.val.get();
            match option {
                Some(ref val) => val,
                None => panic!("Dereference of late static before a value was assigned"),
            }
        }
    }
}

impl<T> core::ops::DerefMut for LateStatic<T> {
    fn deref_mut(&mut self) -> &mut T {
        unsafe {
            let option: &mut Option<T> = &mut *self.val.get();
            match option {
                Some(ref mut val) => val,
                None => panic!("Dereference of late static before a value was assigned"),
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    static ASSIGN_ONCE_TEST: LateStatic<u32> = LateStatic::new();
    #[test]
    fn assign_once() {
        unsafe {
            assert!(!ASSIGN_ONCE_TEST.is_assigned());
            ASSIGN_ONCE_TEST.assign(42);
            assert!(ASSIGN_ONCE_TEST.is_assigned());
        }
    }


    static ASSIGN_TWICE_TEST: LateStatic<u32> = LateStatic::new();
    #[test]
    #[should_panic]
    fn assign_twice() {
        unsafe {
            ASSIGN_TWICE_TEST.assign(42);
            ASSIGN_TWICE_TEST.assign(37);
        }
    }

    struct Foo {
        pub value: u32,
    }

    static DEREF_CONST_TEST: LateStatic<Foo> = LateStatic::new();
    #[test]
    fn deref_const() {
        unsafe {
            DEREF_CONST_TEST.assign(Foo { value: 42 });
        }
        assert_eq!(DEREF_CONST_TEST.value, 42);
    }

    static mut DEREF_MUT_TEST: LateStatic<Foo> = LateStatic::new();
    #[test]
    fn deref_mut() {
        unsafe {
            DEREF_MUT_TEST.assign(Foo { value: 42 });
            assert_eq!(DEREF_MUT_TEST.value, 42);
            DEREF_MUT_TEST.value = 37;
            assert_eq!(DEREF_MUT_TEST.value, 37);
        }
    }
}