use std::ops::{Deref, DerefMut};
#[cfg(feature = "parking_lot")]
type InnerRwLock<T> = parking_lot::RwLock<T>;
#[cfg(all(not(feature = "parking_lot"), feature = "sharded-lock"))]
type InnerRwLock<T> = crossbeam_utils::sync::ShardedLock<T>;
#[cfg(all(not(feature = "parking_lot"), not(feature = "sharded-lock")))]
type InnerRwLock<T> = std::sync::RwLock<T>;
#[cfg(feature = "parking_lot")]
type InnerRwLockReadGuard<'rwlock, T> = parking_lot::RwLockReadGuard<'rwlock, T>;
#[cfg(all(not(feature = "parking_lot"), feature = "sharded-lock"))]
type InnerRwLockReadGuard<'rwlock, T> = crossbeam_utils::sync::ShardedLockReadGuard<'rwlock, T>;
#[cfg(all(not(feature = "parking_lot"), not(feature = "sharded-lock")))]
type InnerRwLockReadGuard<'rwlock, T> = std::sync::RwLockReadGuard<'rwlock, T>;
#[cfg(feature = "parking_lot")]
type InnerRwLockWriteGuard<'rwlock, T> = parking_lot::RwLockWriteGuard<'rwlock, T>;
#[cfg(all(not(feature = "parking_lot"), feature = "sharded-lock"))]
type InnerRwLockWriteGuard<'rwlock, T> = crossbeam_utils::sync::ShardedLockWriteGuard<'rwlock, T>;
#[cfg(all(not(feature = "parking_lot"), not(feature = "sharded-lock")))]
type InnerRwLockWriteGuard<'rwlock, T> = std::sync::RwLockWriteGuard<'rwlock, T>;
#[derive(Default, Debug)]
#[repr(transparent)]
pub struct RwLock<T: ?Sized>(InnerRwLock<T>);
#[repr(transparent)]
#[must_use = "if unused the RwLock will immediately unlock"]
pub struct RwLockReadGuard<'rwlock, T: ?Sized>(InnerRwLockReadGuard<'rwlock, T>);
#[repr(transparent)]
#[must_use = "if unused the RwLock will immediately unlock"]
pub struct RwLockWriteGuard<'rwlock, T: ?Sized>(InnerRwLockWriteGuard<'rwlock, T>);
#[cfg(not(feature = "sharded-lock"))]
impl<T> RwLock<T> {
pub const fn new(t: T) -> Self {
Self(InnerRwLock::new(t))
}
}
#[cfg(feature = "sharded-lock")]
impl<T> RwLock<T> {
pub fn new(t: T) -> Self {
Self(InnerRwLock::new(t))
}
}
impl<T: ?Sized> RwLock<T> {
#[inline]
pub fn read(&self) -> RwLockReadGuard<'_, T> {
RwLockReadGuard({
#[cfg(feature = "parking_lot")]
{
self.0.read()
}
#[cfg(not(feature = "parking_lot"))]
self.0.read().unwrap()
})
}
#[inline]
pub fn write(&self) -> RwLockWriteGuard<'_, T> {
RwLockWriteGuard({
#[cfg(feature = "parking_lot")]
{
self.0.write()
}
#[cfg(not(feature = "parking_lot"))]
self.0.write().unwrap()
})
}
}
impl<T: ?Sized> Deref for RwLockReadGuard<'_, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T: ?Sized> Deref for RwLockWriteGuard<'_, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T: ?Sized> DerefMut for RwLockWriteGuard<'_, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}