#![allow(unsafe_code)]
use core::fmt;
use core::marker::PhantomData;
use core::mem::ManuallyDrop;
use core::ops::{Deref, DerefMut};
use core::ptr;
use std::sync::Arc;
use super::RwLock;
use crate::sync::raw_rwlock::RawRwLock;
#[must_use = "dropping the guard releases the lock at once"]
pub struct RwLockReadGuard<'a, T: ?Sized> {
raw: &'a RawRwLock,
data: *const T,
marker: PhantomData<&'a T>,
}
unsafe impl<T: ?Sized + Sync> Send for RwLockReadGuard<'_, T> {}
unsafe impl<T: ?Sized + Send + Sync> Sync for RwLockReadGuard<'_, T> {}
impl<'a, T: ?Sized> RwLockReadGuard<'a, T> {
pub(super) fn new(lock: &'a RwLock<T>) -> Self {
Self {
raw: &lock.raw,
data: lock.value.get(),
marker: PhantomData,
}
}
fn into_parts(self) -> (&'a RawRwLock, *const T) {
let spent = ManuallyDrop::new(self);
(spent.raw, spent.data)
}
#[inline]
pub fn map<F, U>(this: Self, f: F) -> RwLockReadGuard<'a, U>
where
U: ?Sized,
F: FnOnce(&T) -> &U,
{
let data = ptr::from_ref(f(&*this));
let (raw, _) = this.into_parts();
RwLockReadGuard {
raw,
data,
marker: PhantomData,
}
}
#[inline]
pub fn try_map<F, U>(this: Self, f: F) -> Result<RwLockReadGuard<'a, U>, Self>
where
U: ?Sized,
F: FnOnce(&T) -> Option<&U>,
{
let data = match f(&*this) {
Some(data) => ptr::from_ref(data),
None => return Err(this),
};
let (raw, _) = this.into_parts();
Ok(RwLockReadGuard {
raw,
data,
marker: PhantomData,
})
}
}
impl<T: ?Sized> Deref for RwLockReadGuard<'_, T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &*self.data }
}
}
impl<T: ?Sized> Drop for RwLockReadGuard<'_, T> {
fn drop(&mut self) {
self.raw.read_unlock();
}
}
impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLockReadGuard<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}
impl<T: ?Sized + fmt::Display> fmt::Display for RwLockReadGuard<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&**self, f)
}
}
#[must_use = "dropping the guard releases the lock at once"]
pub struct OwnedRwLockReadGuard<T: ?Sized, U: ?Sized = T> {
lock: Arc<RwLock<T>>,
data: *const U,
_p: PhantomData<T>,
}
unsafe impl<T: ?Sized + Send + Sync, U: ?Sized + Sync> Send for OwnedRwLockReadGuard<T, U> {}
unsafe impl<T: ?Sized + Send + Sync, U: ?Sized + Send + Sync> Sync for OwnedRwLockReadGuard<T, U> {}
impl<T: ?Sized> OwnedRwLockReadGuard<T> {
pub(super) fn new(lock: Arc<RwLock<T>>) -> Self {
Self {
data: lock.value.get(),
lock,
_p: PhantomData,
}
}
}
impl<T: ?Sized, U: ?Sized> OwnedRwLockReadGuard<T, U> {
fn into_parts(self) -> (Arc<RwLock<T>>, *const U) {
let spent = ManuallyDrop::new(self);
(unsafe { ptr::read(&spent.lock) }, spent.data)
}
#[inline]
pub fn map<F, V>(this: Self, f: F) -> OwnedRwLockReadGuard<T, V>
where
V: ?Sized,
F: FnOnce(&U) -> &V,
{
let data = ptr::from_ref(f(&*this));
let (lock, _) = this.into_parts();
OwnedRwLockReadGuard {
lock,
data,
_p: PhantomData,
}
}
#[inline]
pub fn try_map<F, V>(this: Self, f: F) -> Result<OwnedRwLockReadGuard<T, V>, Self>
where
V: ?Sized,
F: FnOnce(&U) -> Option<&V>,
{
let data = match f(&*this) {
Some(data) => ptr::from_ref(data),
None => return Err(this),
};
let (lock, _) = this.into_parts();
Ok(OwnedRwLockReadGuard {
lock,
data,
_p: PhantomData,
})
}
#[inline]
pub fn rwlock(this: &Self) -> &Arc<RwLock<T>> {
&this.lock
}
}
impl<T: ?Sized, U: ?Sized> Deref for OwnedRwLockReadGuard<T, U> {
type Target = U;
fn deref(&self) -> &U {
unsafe { &*self.data }
}
}
impl<T: ?Sized, U: ?Sized> Drop for OwnedRwLockReadGuard<T, U> {
fn drop(&mut self) {
self.lock.raw.read_unlock();
}
}
impl<T: ?Sized, U: ?Sized + fmt::Debug> fmt::Debug for OwnedRwLockReadGuard<T, U> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}
impl<T: ?Sized, U: ?Sized + fmt::Display> fmt::Display for OwnedRwLockReadGuard<T, U> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&**self, f)
}
}
#[must_use = "dropping the guard releases the lock at once"]
pub struct RwLockWriteGuard<'a, T: ?Sized> {
raw: &'a RawRwLock,
data: *mut T,
marker: PhantomData<&'a mut T>,
}
unsafe impl<T: ?Sized + Send + Sync> Send for RwLockWriteGuard<'_, T> {}
unsafe impl<T: ?Sized + Send + Sync> Sync for RwLockWriteGuard<'_, T> {}
impl<'a, T: ?Sized> RwLockWriteGuard<'a, T> {
pub(super) fn new(lock: &'a RwLock<T>) -> Self {
Self {
raw: &lock.raw,
data: lock.value.get(),
marker: PhantomData,
}
}
fn into_parts(self) -> (&'a RawRwLock, *mut T) {
let spent = ManuallyDrop::new(self);
(spent.raw, spent.data)
}
#[inline]
pub fn map<F, U>(mut this: Self, f: F) -> RwLockMappedWriteGuard<'a, U>
where
U: ?Sized,
F: FnOnce(&mut T) -> &mut U,
{
let data = ptr::from_mut(f(&mut *this));
let (raw, _) = this.into_parts();
RwLockMappedWriteGuard {
raw,
data,
marker: PhantomData,
}
}
#[inline]
pub fn downgrade_map<F, U>(this: Self, f: F) -> RwLockReadGuard<'a, U>
where
U: ?Sized,
F: FnOnce(&T) -> &U,
{
let data = ptr::from_ref(f(&*this));
let (raw, _) = this.into_parts();
let guard = RwLockReadGuard {
raw,
data,
marker: PhantomData,
};
raw.downgrade();
guard
}
#[inline]
pub fn try_map<F, U>(mut this: Self, f: F) -> Result<RwLockMappedWriteGuard<'a, U>, Self>
where
U: ?Sized,
F: FnOnce(&mut T) -> Option<&mut U>,
{
let data = match f(&mut *this) {
Some(data) => ptr::from_mut(data),
None => return Err(this),
};
let (raw, _) = this.into_parts();
Ok(RwLockMappedWriteGuard {
raw,
data,
marker: PhantomData,
})
}
#[inline]
pub fn try_downgrade_map<F, U>(this: Self, f: F) -> Result<RwLockReadGuard<'a, U>, Self>
where
U: ?Sized,
F: FnOnce(&T) -> Option<&U>,
{
let data = match f(&*this) {
Some(data) => ptr::from_ref(data),
None => return Err(this),
};
let (raw, _) = this.into_parts();
let guard = RwLockReadGuard {
raw,
data,
marker: PhantomData,
};
raw.downgrade();
Ok(guard)
}
#[inline]
pub fn into_mapped(this: Self) -> RwLockMappedWriteGuard<'a, T> {
Self::map(this, |me| me)
}
pub fn downgrade(self) -> RwLockReadGuard<'a, T> {
let (raw, data) = self.into_parts();
let guard = RwLockReadGuard {
raw,
data,
marker: PhantomData,
};
raw.downgrade();
guard
}
}
impl<T: ?Sized> Deref for RwLockWriteGuard<'_, T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &*self.data }
}
}
impl<T: ?Sized> DerefMut for RwLockWriteGuard<'_, T> {
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut *self.data }
}
}
impl<T: ?Sized> Drop for RwLockWriteGuard<'_, T> {
fn drop(&mut self) {
self.raw.write_unlock();
}
}
impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLockWriteGuard<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}
impl<T: ?Sized + fmt::Display> fmt::Display for RwLockWriteGuard<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&**self, f)
}
}
#[must_use = "dropping the guard releases the lock at once"]
pub struct RwLockMappedWriteGuard<'a, T: ?Sized> {
raw: &'a RawRwLock,
data: *mut T,
marker: PhantomData<&'a mut T>,
}
unsafe impl<T: ?Sized + Send + Sync> Send for RwLockMappedWriteGuard<'_, T> {}
unsafe impl<T: ?Sized + Send + Sync> Sync for RwLockMappedWriteGuard<'_, T> {}
impl<'a, T: ?Sized> RwLockMappedWriteGuard<'a, T> {
fn into_raw(self) -> &'a RawRwLock {
ManuallyDrop::new(self).raw
}
#[inline]
pub fn map<F, U>(mut this: Self, f: F) -> RwLockMappedWriteGuard<'a, U>
where
U: ?Sized,
F: FnOnce(&mut T) -> &mut U,
{
let data = ptr::from_mut(f(&mut *this));
RwLockMappedWriteGuard {
raw: this.into_raw(),
data,
marker: PhantomData,
}
}
#[inline]
pub fn try_map<F, U>(mut this: Self, f: F) -> Result<RwLockMappedWriteGuard<'a, U>, Self>
where
U: ?Sized,
F: FnOnce(&mut T) -> Option<&mut U>,
{
let data = match f(&mut *this) {
Some(data) => ptr::from_mut(data),
None => return Err(this),
};
Ok(RwLockMappedWriteGuard {
raw: this.into_raw(),
data,
marker: PhantomData,
})
}
}
impl<T: ?Sized> Deref for RwLockMappedWriteGuard<'_, T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &*self.data }
}
}
impl<T: ?Sized> DerefMut for RwLockMappedWriteGuard<'_, T> {
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut *self.data }
}
}
impl<T: ?Sized> Drop for RwLockMappedWriteGuard<'_, T> {
fn drop(&mut self) {
self.raw.write_unlock();
}
}
impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLockMappedWriteGuard<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}
impl<T: ?Sized + fmt::Display> fmt::Display for RwLockMappedWriteGuard<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&**self, f)
}
}
#[must_use = "dropping the guard releases the lock at once"]
pub struct OwnedRwLockWriteGuard<T: ?Sized> {
lock: Arc<RwLock<T>>,
data: *mut T,
_p: PhantomData<T>,
}
unsafe impl<T: ?Sized + Send + Sync> Send for OwnedRwLockWriteGuard<T> {}
unsafe impl<T: ?Sized + Send + Sync> Sync for OwnedRwLockWriteGuard<T> {}
impl<T: ?Sized> OwnedRwLockWriteGuard<T> {
pub(super) fn new(lock: Arc<RwLock<T>>) -> Self {
Self {
data: lock.value.get(),
lock,
_p: PhantomData,
}
}
fn into_parts(self) -> (Arc<RwLock<T>>, *mut T) {
let spent = ManuallyDrop::new(self);
(unsafe { ptr::read(&spent.lock) }, spent.data)
}
#[inline]
pub fn map<F, U>(mut this: Self, f: F) -> OwnedRwLockMappedWriteGuard<T, U>
where
U: ?Sized,
F: FnOnce(&mut T) -> &mut U,
{
let data = ptr::from_mut(f(&mut *this));
let (lock, _) = this.into_parts();
OwnedRwLockMappedWriteGuard {
lock,
data,
_p: PhantomData,
}
}
#[inline]
pub fn downgrade_map<F, U>(this: Self, f: F) -> OwnedRwLockReadGuard<T, U>
where
U: ?Sized,
F: FnOnce(&T) -> &U,
{
let data = ptr::from_ref(f(&*this));
let (lock, _) = this.into_parts();
let guard = OwnedRwLockReadGuard {
lock,
data,
_p: PhantomData,
};
guard.lock.raw.downgrade();
guard
}
#[inline]
pub fn try_map<F, U>(mut this: Self, f: F) -> Result<OwnedRwLockMappedWriteGuard<T, U>, Self>
where
U: ?Sized,
F: FnOnce(&mut T) -> Option<&mut U>,
{
let data = match f(&mut *this) {
Some(data) => ptr::from_mut(data),
None => return Err(this),
};
let (lock, _) = this.into_parts();
Ok(OwnedRwLockMappedWriteGuard {
lock,
data,
_p: PhantomData,
})
}
#[inline]
pub fn try_downgrade_map<F, U>(this: Self, f: F) -> Result<OwnedRwLockReadGuard<T, U>, Self>
where
U: ?Sized,
F: FnOnce(&T) -> Option<&U>,
{
let data = match f(&*this) {
Some(data) => ptr::from_ref(data),
None => return Err(this),
};
let (lock, _) = this.into_parts();
let guard = OwnedRwLockReadGuard {
lock,
data,
_p: PhantomData,
};
guard.lock.raw.downgrade();
Ok(guard)
}
#[inline]
pub fn into_mapped(this: Self) -> OwnedRwLockMappedWriteGuard<T> {
Self::map(this, |me| me)
}
pub fn downgrade(self) -> OwnedRwLockReadGuard<T> {
let (lock, data) = self.into_parts();
let guard = OwnedRwLockReadGuard {
lock,
data,
_p: PhantomData,
};
guard.lock.raw.downgrade();
guard
}
#[inline]
pub fn rwlock(this: &Self) -> &Arc<RwLock<T>> {
&this.lock
}
}
impl<T: ?Sized> Deref for OwnedRwLockWriteGuard<T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &*self.data }
}
}
impl<T: ?Sized> DerefMut for OwnedRwLockWriteGuard<T> {
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut *self.data }
}
}
impl<T: ?Sized> Drop for OwnedRwLockWriteGuard<T> {
fn drop(&mut self) {
self.lock.raw.write_unlock();
}
}
impl<T: ?Sized + fmt::Debug> fmt::Debug for OwnedRwLockWriteGuard<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}
impl<T: ?Sized + fmt::Display> fmt::Display for OwnedRwLockWriteGuard<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&**self, f)
}
}
#[must_use = "dropping the guard releases the lock at once"]
pub struct OwnedRwLockMappedWriteGuard<T: ?Sized, U: ?Sized = T> {
lock: Arc<RwLock<T>>,
data: *mut U,
_p: PhantomData<T>,
}
unsafe impl<T: ?Sized + Send + Sync, U: ?Sized + Send + Sync> Send
for OwnedRwLockMappedWriteGuard<T, U>
{
}
unsafe impl<T: ?Sized + Send + Sync, U: ?Sized + Send + Sync> Sync
for OwnedRwLockMappedWriteGuard<T, U>
{
}
impl<T: ?Sized, U: ?Sized> OwnedRwLockMappedWriteGuard<T, U> {
fn into_lock(self) -> Arc<RwLock<T>> {
let spent = ManuallyDrop::new(self);
unsafe { ptr::read(&spent.lock) }
}
#[inline]
pub fn map<V, F>(mut this: Self, f: F) -> OwnedRwLockMappedWriteGuard<T, V>
where
V: ?Sized,
F: FnOnce(&mut U) -> &mut V,
{
let data = ptr::from_mut(f(&mut *this));
OwnedRwLockMappedWriteGuard {
lock: this.into_lock(),
data,
_p: PhantomData,
}
}
#[inline]
pub fn try_map<V, F>(mut this: Self, f: F) -> Result<OwnedRwLockMappedWriteGuard<T, V>, Self>
where
V: ?Sized,
F: FnOnce(&mut U) -> Option<&mut V>,
{
let data = match f(&mut *this) {
Some(data) => ptr::from_mut(data),
None => return Err(this),
};
Ok(OwnedRwLockMappedWriteGuard {
lock: this.into_lock(),
data,
_p: PhantomData,
})
}
#[inline]
pub fn rwlock(this: &Self) -> &Arc<RwLock<T>> {
&this.lock
}
}
impl<T: ?Sized, U: ?Sized> Deref for OwnedRwLockMappedWriteGuard<T, U> {
type Target = U;
fn deref(&self) -> &U {
unsafe { &*self.data }
}
}
impl<T: ?Sized, U: ?Sized> DerefMut for OwnedRwLockMappedWriteGuard<T, U> {
fn deref_mut(&mut self) -> &mut U {
unsafe { &mut *self.data }
}
}
impl<T: ?Sized, U: ?Sized> Drop for OwnedRwLockMappedWriteGuard<T, U> {
fn drop(&mut self) {
self.lock.raw.write_unlock();
}
}
impl<T: ?Sized, U: ?Sized + fmt::Debug> fmt::Debug for OwnedRwLockMappedWriteGuard<T, U> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}
impl<T: ?Sized, U: ?Sized + fmt::Display> fmt::Display for OwnedRwLockMappedWriteGuard<T, U> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&**self, f)
}
}