singleton_registry/
macros.rs1#![allow(dead_code)]
2
3#[macro_export]
34macro_rules! define_registry {
35 ($name:ident) => {
36 pub mod $name {
37 use std::sync::{Arc, LazyLock, Mutex};
38 use std::collections::HashMap;
39 use std::any::{TypeId, Any};
40
41 static STORAGE: LazyLock<Mutex<HashMap<TypeId, Arc<dyn Any + Send + Sync>>>> =
43 LazyLock::new(|| Mutex::new(HashMap::new()));
44
45 type TraceCallback = LazyLock<Mutex<Option<Arc<dyn Fn(&$crate::RegistryEvent) + Send + Sync>>>>;
48 static TRACE: TraceCallback = LazyLock::new(|| Mutex::new(None));
49
50 struct Api;
55
56 impl $crate::RegistryApi for Api {
57 fn storage() -> &'static LazyLock<Mutex<HashMap<TypeId, Arc<dyn Any + Send + Sync>>>> {
58 &STORAGE
59 }
60
61 fn trace() -> &'static TraceCallback {
62 &TRACE
63 }
64
65 }
68
69 const API: Api = Api;
71
72 pub fn register<T: Send + Sync + 'static>(value: T) {
76 use $crate::RegistryApi;
77 API.register(value)
78 }
79
80 pub fn register_arc<T: Send + Sync + 'static>(value: Arc<T>) {
82 use $crate::RegistryApi;
83 API.register_arc(value)
84 }
85
86 pub fn get<T: Send + Sync + 'static>() -> Result<Arc<T>, $crate::RegistryError> {
88 use $crate::RegistryApi;
89 API.get()
90 }
91
92 pub fn get_cloned<T: Send + Sync + Clone + 'static>() -> Result<T, $crate::RegistryError> {
94 use $crate::RegistryApi;
95 API.get_cloned()
96 }
97
98 pub fn contains<T: Send + Sync + 'static>() -> Result<bool, $crate::RegistryError> {
100 use $crate::RegistryApi;
101 API.contains::<T>()
102 }
103
104 pub fn set_trace_callback(callback: impl Fn(&$crate::RegistryEvent) + Send + Sync + 'static) {
106 use $crate::RegistryApi;
107 API.set_trace_callback(callback)
108 }
109
110 pub fn clear_trace_callback() {
112 use $crate::RegistryApi;
113 API.clear_trace_callback()
114 }
115
116 #[doc(hidden)]
118 pub fn clear() {
119 use $crate::RegistryApi;
120 API.clear()
121 }
122 }
123 };
124}
125
126#[cfg(test)]
127mod tests {
128 use std::sync::Arc;
130
131 #[test]
132 fn test_define_registry_macro() {
133 define_registry!(test_reg);
134
135 test_reg::register(100i32);
137 let value: Arc<i32> = test_reg::get().unwrap();
138 assert_eq!(*value, 100);
139
140 assert!(test_reg::contains::<i32>().unwrap());
142 assert!(!test_reg::contains::<f64>().unwrap());
143 }
144
145 #[test]
146 fn test_multiple_registries() {
147 define_registry!(reg_a);
148 define_registry!(reg_b);
149
150 reg_a::register(1i32);
152 reg_b::register(2i32);
153
154 let a_val: Arc<i32> = reg_a::get().unwrap();
156 let b_val: Arc<i32> = reg_b::get().unwrap();
157
158 assert_eq!(*a_val, 1);
159 assert_eq!(*b_val, 2);
160 }
161
162 #[test]
163 fn test_tracing() {
164 define_registry!(trace_test);
165
166 use std::sync::Mutex;
167 let events = Arc::new(Mutex::new(Vec::new()));
168 let events_clone = events.clone();
169
170 trace_test::set_trace_callback(move |event| {
171 events_clone.lock().unwrap().push(format!("{}", event));
172 });
173
174 trace_test::register(42i32);
175 let _: Arc<i32> = trace_test::get().unwrap();
176 let _ = trace_test::contains::<i32>();
177
178 let recorded = events.lock().unwrap();
179 assert_eq!(recorded.len(), 3);
180 assert!(recorded[0].contains("register"));
181 assert!(recorded[1].contains("get"));
182 assert!(recorded[2].contains("contains"));
183 }
184
185 #[test]
186 fn test_additional_functions() {
187 define_registry!(extra_test);
188
189 let val = Arc::new(99i32);
191 extra_test::register_arc(val);
192
193 let cloned: i32 = extra_test::get_cloned().unwrap();
195 assert_eq!(cloned, 99);
196
197 extra_test::set_trace_callback(|_| {});
199 extra_test::clear_trace_callback(); }
201}