1pub( crate ) mod private
3{
4 use crate::protected::*;
5 use once_cell::sync::Lazy;
7 use std::sync::Mutex;
8 use dashmap::DashMap;
9 use std::sync::Arc;
10
11 #[ derive( Debug ) ]
13 pub struct Registry< Context >
14 where
15 Context : ContextInterface,
16 {
17 contexts : DashMap< Id, Context >,
18 contexts_with_name : DashMap< String, Id >,
19 current_context_name : Option< String >,
20 }
21
22 impl< Context > Registry< Context >
23 where
24 Context : ContextInterface,
25 {
26
27 pub const fn new() -> Lazy< Arc< Mutex< Registry< Context > > > >
29 {
30 Lazy::new( ||
31 {
32 let contexts = DashMap::new();
33 let contexts_with_name = DashMap::new();
34 let current_context_name = None;
35 Arc::new( Mutex::new( Registry::< Context >
36 {
37 contexts,
38 contexts_with_name,
39 current_context_name,
40 }))
41 })
42 }
43
44 pub fn current( _registry : &mut Lazy< Arc< Mutex< Registry< Context > > > > ) -> Context::Changer
46 {
47 let registry = _registry.lock().unwrap();
48 let mut current_name : Option< String > = registry.current_context_name.clone();
49 if current_name.is_none()
50 {
51 current_name = Some( "default".into() )
52 }
53 let current_name = current_name.unwrap();
54 if registry.contexts_with_name.contains_key( ¤t_name )
55 {
56 let id = *registry.contexts_with_name.get( ¤t_name ).unwrap().value();
57 registry.contexts.get_mut( &id ).unwrap().value_mut().changer()
58 }
59 else
60 {
61 let context : Context = make!();
62 let id = context.id();
63 registry.contexts_with_name.insert( current_name.clone(), context.id() );
64 registry.contexts.insert( id, context );
65 registry.contexts.get_mut( &id ).unwrap().value_mut().changer()
66 }
67 }
68
69 }
70
71}
72
73crate::mod_interface!
74{
75
76 orphan use Registry;
77
78}