Skip to content

Commit

Permalink
Allow RwLock to work with ?Sized types
Browse files Browse the repository at this point in the history
  • Loading branch information
Phantomical committed Mar 16, 2024
1 parent 1c44a23 commit 9016f19
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/sync/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ use std::sync::{LockResult, TryLockError, TryLockResult};

/// Mock implementation of `std::sync::RwLock`
#[derive(Debug)]
pub struct RwLock<T> {
pub struct RwLock<T: ?Sized> {
object: rt::RwLock,
data: std::sync::RwLock<T>,
}

/// Mock implementation of `std::sync::RwLockReadGuard`
#[derive(Debug)]
pub struct RwLockReadGuard<'a, T> {
pub struct RwLockReadGuard<'a, T: ?Sized> {
lock: &'a RwLock<T>,
data: Option<std::sync::RwLockReadGuard<'a, T>>,
}

/// Mock implementation of `std::sync::rwLockWriteGuard`
#[derive(Debug)]
pub struct RwLockWriteGuard<'a, T> {
pub struct RwLockWriteGuard<'a, T: ?Sized> {
lock: &'a RwLock<T>,
/// `data` is an Option so that the Drop impl can drop the std guard and release the std lock
/// before releasing the loom mock lock, as that might cause another thread to acquire the lock
Expand All @@ -35,6 +35,13 @@ impl<T> RwLock<T> {
}
}

/// Consumes this `RwLock`, returning the underlying data.
pub fn into_inner(self) -> LockResult<T> {
Ok(self.data.into_inner().expect("loom::RwLock state corrupt"))
}
}

impl<T: ?Sized> RwLock<T> {
/// Locks this rwlock with shared read access, blocking the current
/// thread until it can be acquired.
///
Expand Down Expand Up @@ -110,11 +117,6 @@ impl<T> RwLock<T> {
pub fn get_mut(&mut self) -> LockResult<&mut T> {
Ok(self.data.get_mut().expect("loom::RwLock state corrupt"))
}

/// Consumes this `RwLock`, returning the underlying data.
pub fn into_inner(self) -> LockResult<T> {
Ok(self.data.into_inner().expect("loom::RwLock state corrupt"))
}
}

impl<T: Default> Default for RwLock<T> {
Expand All @@ -132,36 +134,36 @@ impl<T> From<T> for RwLock<T> {
}
}

impl<'a, T> ops::Deref for RwLockReadGuard<'a, T> {
impl<'a, T: ?Sized> ops::Deref for RwLockReadGuard<'a, T> {
type Target = T;

fn deref(&self) -> &T {
self.data.as_ref().unwrap().deref()
}
}

impl<'a, T: 'a> Drop for RwLockReadGuard<'a, T> {
impl<'a, T: ?Sized + 'a> Drop for RwLockReadGuard<'a, T> {
fn drop(&mut self) {
self.data = None;
self.lock.object.release_read_lock()
}
}

impl<'a, T> ops::Deref for RwLockWriteGuard<'a, T> {
impl<'a, T: ?Sized> ops::Deref for RwLockWriteGuard<'a, T> {
type Target = T;

fn deref(&self) -> &T {
self.data.as_ref().unwrap().deref()
}
}

impl<'a, T> ops::DerefMut for RwLockWriteGuard<'a, T> {
impl<'a, T: ?Sized> ops::DerefMut for RwLockWriteGuard<'a, T> {
fn deref_mut(&mut self) -> &mut T {
self.data.as_mut().unwrap().deref_mut()
}
}

impl<'a, T: 'a> Drop for RwLockWriteGuard<'a, T> {
impl<'a, T: ?Sized + 'a> Drop for RwLockWriteGuard<'a, T> {
fn drop(&mut self) {
self.data = None;
self.lock.object.release_write_lock()
Expand Down

0 comments on commit 9016f19

Please sign in to comment.