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
//! A [`StaticTypeMap`] is a map that maps from a type to a value.

use parking_lot::ReentrantMutex;
use std::any::TypeId;
use std::cell::RefCell;
use std::collections::HashMap;

/// A map that stores static types.
pub struct StaticTypeMap<T: 'static> {
    map: ReentrantMutex<RefCell<HashMap<TypeId, &'static T>>>,
}

impl<T: 'static> Default for StaticTypeMap<T> {
    fn default() -> Self {
        Self {
            map: ReentrantMutex::new(RefCell::new(HashMap::default())),
        }
    }
}

impl<T: 'static> StaticTypeMap<T> {
    /// Initialize static value corresponding to provided type.
    ///
    /// Initialized value will stay on heap until program terminated.
    /// No drop method will be called.
    pub fn call_once<Type, Init>(&'static self, f: Init) -> &'static T
    where
        Type: 'static,
        Init: FnOnce() -> T,
    {
        // If already initialized, just return stored value
        let map = self.map.lock();
        if let Some(r) = map.borrow().get(&TypeId::of::<Type>()) {
            return r;
        }

        // leak it's value until program is terminated
        let reference = Box::leak(Box::new(f()));

        // Insert the value into the map
        let old = map.borrow_mut().insert(TypeId::of::<Type>(), reference);
        if old.is_some() {
            panic!("StaticTypeMap value was reinitialized. This is a bug.")
        }
        reference
    }
}