use std::{
ops::{Deref, DerefMut},
sync,
};
pub struct Mutex<T: ?Sized>(sync::Mutex<T>);
impl<T> Mutex<T> {
#[inline]
pub fn new(t: T) -> Mutex<T> {
Mutex(sync::Mutex::new(t))
}
}
impl<T: ?Sized> Mutex<T> {
#[inline]
pub fn lock(&self) -> MutexGuard<'_, T> {
MutexGuard(self.0.lock().unwrap_or_else(|e| e.into_inner()))
}
}
impl<T> Default for Mutex<T>
where
T: Default,
{
#[inline]
fn default() -> Self {
Mutex::new(T::default())
}
}
#[must_use]
pub struct MutexGuard<'a, T: ?Sized + 'a>(sync::MutexGuard<'a, T>);
impl<T: ?Sized> Deref for MutexGuard<'_, T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
self.0.deref()
}
}
impl<T: ?Sized> DerefMut for MutexGuard<'_, T> {
#[inline]
fn deref_mut(&mut self) -> &mut T {
self.0.deref_mut()
}
}