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
use parking_lot::ReentrantMutex;
use std::any::TypeId;
use std::cell::RefCell;
use std::collections::HashMap;
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> {
pub fn call_once<Type, Init>(&'static self, f: Init) -> &'static T
where
Type: 'static,
Init: FnOnce() -> T,
{
let map = self.map.lock();
if let Some(r) = map.borrow().get(&TypeId::of::<Type>()) {
return r;
}
let reference = Box::leak(Box::new(f()));
let old = map.borrow_mut().insert(TypeId::of::<Type>(), reference);
if old.is_some() {
panic!("StaticTypeMap value was reinitialized. This is a bug.")
}
reference
}
}