global_value_manager/
lib.rs

1
2mod global_value_manager;
3mod global_value_mut;
4mod global_value;
5mod global_entry;
6pub mod prelude;
7
8
9pub use global_value::GlobalValue;
10pub use global_value_mut::GlobalValueMut;
11pub use global_value_manager::GlobalValueManager;
12
13use prelude::*;
14lazy_static::lazy_static! {
15    pub(crate) static ref MANAGER: ShardedLock<HashMap<TypeId, Entry>> = Default::default();
16}
17
18
19
20mod test {
21    #[allow(unused)]
22    use super::prelude::*;
23
24    #[test]
25    fn test() {
26        #[derive(Clone)]
27        struct B(i32);
28
29        GlobalValueManager::update(Arc::new(B(21)));
30        let mut instance = GlobalValue::<B>::new();
31        let mut instance2 = GlobalValue::<B>::new();
32        // test initial value
33        assert_eq!(instance.0, 21);
34        assert_eq!(instance2.0, 21);
35
36        GlobalValueManager::update(Arc::new(B(55)));
37
38        // check internal value has not changed
39        assert_eq!(instance.0, 21);
40        assert_eq!(instance2.0, 21);
41
42        // update value
43        instance.update();
44        instance2.update();
45
46        // check internal value has changed
47        assert_eq!(instance.0, 55);
48        assert_eq!(instance2.0, 55);
49
50        // test drop behaviour
51        {
52            let mut b = GlobalValueManager::get_mut::<B>().unwrap();
53            b.0 = 500;
54        }
55
56        // check internal value has not changed
57        assert_eq!(instance.0, 55);
58        assert_eq!(instance2.0, 55);
59
60        // update value
61        instance.update();
62        instance2.update();
63
64        // check internal value has changed
65        assert_eq!(instance.0, 500);
66        assert_eq!(instance2.0, 500);
67    }
68}