//! Wrappers for synchronization containers. use core::ops::Deref; /// Alias for `Arc` pub type Shared = std::sync::Arc; /// A mutex that can safely be in async contexts and avoids deadlocks. #[derive(Default, Debug)] pub struct SharedMutex(Shared>); impl Clone for SharedMutex { fn clone(&self) -> Self { Self(self.0.clone()) } } impl Deref for SharedMutex { type Target = Shared>; fn deref(&self) -> &Self::Target { &self.0 } } impl SharedMutex { /// Creates a new `SharedMutex` with the given value. pub fn new(t: T) -> Self { Self(Shared::new(parking_lot::Mutex::new(t))) } /// Apply a function to the inner value and return a value. pub fn apply(&self, f: impl FnOnce(&mut T) -> R) -> R { let mut t = self.0.lock(); f(&mut t) } } impl From for SharedMutex { fn from(t: T) -> Self { Self::new(t) } }