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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//! 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 {
//!         LateStatic::assign(&FOO, 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: Sync> 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.
    ///
    /// # Safety
    ///
    /// This is completely unsafe if there is even the slightest chance of another
    /// thread trying to dereference the variable.
    pub unsafe fn assign(instance: &LateStatic<T>, val: T) {
        let option: &mut Option<T> = &mut *instance.val.get();
        if option.is_some() {
            panic!("Second assignment to late static");
        } else {
            *option = Some(val);
        }
    }

    /// Invalidate the late static by removing its inner value.
    ///
    /// # Safety
    ///
    /// This is completely unsafe if there is even the slightest chance of another
    /// thread trying to dereference the variable.
    pub unsafe fn clear(instance: &LateStatic<T>) {
        if !Self::has_value(instance) {
            panic!("Tried to clear a late static without a value");
        }
        let option: &mut Option<T> = &mut *instance.val.get();
        *option = None;
    }

    /// Whether a value is assigned to this LateStatic.
    ///
    /// # Safety
    ///
    /// This is completely unsafe if there is even the slightest chance of another
    /// thread trying to dereference the variable.
    pub unsafe fn has_value(instance: &LateStatic<T>) -> bool {
        let option: &Option<T> = &*instance.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!(!LateStatic::has_value(&ASSIGN_ONCE_TEST));
            LateStatic::assign(&ASSIGN_ONCE_TEST, 42);
            assert!(LateStatic::has_value(&ASSIGN_ONCE_TEST));
        }
    }

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

    struct Foo {
        pub value: u32,
    }

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

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

    static mut DEREF_WITHOUT_VALUE: LateStatic<Foo> = LateStatic::new();
    #[test]
    #[should_panic]
    fn deref_without_value() {
        unsafe {
            #[allow(clippy::no_effect)]
            DEREF_WITHOUT_VALUE.value;
        }
    }

    static mut CLEAR_TEST: LateStatic<Foo> = LateStatic::new();
    #[test]
    fn clear() {
        unsafe {
            LateStatic::assign(&CLEAR_TEST, Foo { value: 42 });
            assert_eq!(CLEAR_TEST.value, 42);
            LateStatic::clear(&CLEAR_TEST);
            assert!(!LateStatic::has_value(&CLEAR_TEST));
        }
    }

    static mut CLEAR_WITHOUT_VALUE: LateStatic<Foo> = LateStatic::new();
    #[test]
    #[should_panic]
    fn clear_without_value() {
        unsafe {
            LateStatic::clear(&CLEAR_WITHOUT_VALUE);
        }
    }
}