use std::{
fmt,
sync::{Mutex as StdMutex, MutexGuard, RwLock as StdRwLock, RwLockReadGuard, RwLockWriteGuard},
};
use serde::{Deserialize, Serialize};
#[derive(Default)]
pub struct Mutex<T: ?Sized>(StdMutex<T>);
impl<T> Mutex<T> {
pub const fn new(t: T) -> Self {
Self(StdMutex::new(t))
}
}
impl<T: ?Sized> Mutex<T> {
pub fn lock(&self) -> MutexGuard<'_, T> {
self.0.lock().expect("The Mutex should never be poisoned")
}
}
impl<T: ?Sized + fmt::Debug> fmt::Debug for Mutex<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
#[derive(Default, Serialize, Deserialize)]
#[serde(transparent)]
pub struct RwLock<T: ?Sized>(StdRwLock<T>);
impl<T> RwLock<T> {
pub const fn new(t: T) -> Self {
Self(StdRwLock::new(t))
}
}
impl<T: ?Sized> RwLock<T> {
pub fn write(&self) -> RwLockWriteGuard<'_, T> {
self.0.write().expect("The RwLock should never be poisoned")
}
pub fn read(&self) -> RwLockReadGuard<'_, T> {
self.0.read().expect("The RwLock should never be poisoned")
}
}
impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLock<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl<T> From<T> for RwLock<T> {
fn from(value: T) -> Self {
Self::new(value)
}
}