use std::{ops, sync};
#[derive(Default, Debug)]
pub struct Mutex<T>(sync::Mutex<T>);
impl<T> Mutex<T> {
pub fn new(value: T) -> Self {
Self(sync::Mutex::new(value))
}
pub fn lock(&self) -> MutexGuard<'_, T> {
let guard = self.0.lock().unwrap();
MutexGuard(guard)
}
pub fn into_inner(self) -> T {
self.0.into_inner().unwrap()
}
}
pub struct MutexGuard<'a, T>(sync::MutexGuard<'a, T>);
impl<'a, T> ops::Deref for MutexGuard<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'a, T> ops::DerefMut for MutexGuard<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
#[derive(Default, Debug)]
pub struct RwLock<T>(sync::RwLock<T>);
impl<T> RwLock<T> {
pub fn new(value: T) -> Self {
Self(sync::RwLock::new(value))
}
pub fn read(&self) -> RwLockReadGuard<'_, T> {
let guard = self.0.read().unwrap();
RwLockReadGuard(guard)
}
pub fn write(&self) -> RwLockWriteGuard<'_, T> {
let guard = self.0.write().unwrap();
RwLockWriteGuard(guard)
}
}
pub struct RwLockReadGuard<'a, T>(sync::RwLockReadGuard<'a, T>);
impl<'a, T> ops::Deref for RwLockReadGuard<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
pub struct RwLockWriteGuard<'a, T>(sync::RwLockWriteGuard<'a, T>);
impl<'a, T> ops::Deref for RwLockWriteGuard<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'a, T> ops::DerefMut for RwLockWriteGuard<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}