use crate::utils_async::sync_types;
use std::sync::{Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::{convert, marker};
pub struct StdLock<T: marker::Send> {
mtx: Mutex<T>,
}
impl<T: marker::Send> sync_types::Lock<T> for StdLock<T> {
type Guard<'a>
= MutexGuard<'a, T>
where
T: 'a;
fn lock(&self) -> Self::Guard<'_> {
self.mtx.lock().unwrap()
}
}
impl<T: marker::Send> sync_types::ConstructibleLock<T> for StdLock<T> {
fn get_mut(&mut self) -> &mut T {
self.mtx.get_mut().unwrap()
}
}
impl<T: marker::Send> convert::From<T> for StdLock<T> {
fn from(value: T) -> Self {
Self { mtx: Mutex::new(value) }
}
}
pub struct StdRwLock<T: marker::Send + marker::Sync> {
rwlock: RwLock<T>,
}
impl<T: marker::Send + marker::Sync> sync_types::RwLock<T> for StdRwLock<T> {
type ReadGuard<'a>
= RwLockReadGuard<'a, T>
where
T: 'a;
type WriteGuard<'a>
= RwLockWriteGuard<'a, T>
where
T: 'a;
fn get_mut(&mut self) -> &mut T {
self.rwlock.get_mut().unwrap()
}
fn read(&self) -> Self::ReadGuard<'_> {
self.rwlock.read().unwrap()
}
fn write(&self) -> Self::WriteGuard<'_> {
self.rwlock.write().unwrap()
}
}
impl<T: marker::Send + marker::Sync> convert::From<T> for StdRwLock<T> {
fn from(value: T) -> Self {
Self {
rwlock: RwLock::new(value),
}
}
}
pub struct StdSyncTypes {}
impl sync_types::SyncTypes for StdSyncTypes {
type Lock<T: marker::Send> = StdLock<T>;
type RwLock<T: marker::Send + marker::Sync> = StdRwLock<T>;
type SyncRcPtrFactory = sync_types::GenericArcFactory;
}