Diffstat (limited to 'src/mock.rs')
| -rw-r--r-- | src/mock.rs | 81 |
1 files changed, 0 insertions, 81 deletions
diff --git a/src/mock.rs b/src/mock.rs index 89a7a64..e69de29 100644 --- a/src/mock.rs +++ b/src/mock.rs @@ -1,81 +0,0 @@ -use core::{ - any::{Any, TypeId}, - ops::Deref, -}; -use std::{ - collections::HashMap, - sync::{Mutex, MutexGuard, OnceLock, RwLock}, -}; - -pub mod builder; -pub mod protocol; - -pub struct StaticTypeMap { - map: OnceLock<RwLock<HashMap<TypeId, &'static (dyn Any + Send + Sync)>>>, -} - -impl StaticTypeMap { - pub const fn new() -> Self { - Self { - map: OnceLock::new(), - } - } - - pub fn get_or_init<T: Send + Sync + 'static, F: FnOnce() -> T>(&self, f: F) -> &'static T { - let map_init = || RwLock::new(HashMap::new()); - - let map = self.map.get_or_init(map_init).read().unwrap(); - - if let Some(once) = map.get(&TypeId::of::<T>()) { - return once.downcast_ref::<T>().unwrap(); - } - - drop(map); - - let mut map = self.map.get_or_init(map_init).write().unwrap(); - let once = &*Box::leak(Box::new(f())); - map.insert(TypeId::of::<T>(), once); - - once - } -} - -pub struct ContextLock<T> { - lock: Mutex<T>, - checkpoint: fn(&T), -} - -impl<T> ContextLock<T> { - pub const fn new(context: T, checkpoint: fn(&T)) -> Self { - Self { - lock: Mutex::new(context), - checkpoint, - } - } - - pub fn lock(&self) -> ContextGuard<'_, T> { - ContextGuard { - lock: self, - guard: self.lock.lock().unwrap(), - } - } -} - -pub struct ContextGuard<'a, T> { - lock: &'a ContextLock<T>, - guard: MutexGuard<'a, T>, -} - -impl<'a, T> Drop for ContextGuard<'a, T> { - fn drop(&mut self) { - (self.lock.checkpoint)(&*self.guard) - } -} - -impl<'a, T> Deref for ContextGuard<'a, T> { - type Target = T; - - fn deref(&self) -> &Self::Target { - &self.guard - } -} |