From 499a44ab3bcbea116dd1025d90796faaa5dfc7fe Mon Sep 17 00:00:00 2001 From: Gary Guo Date: Sat, 14 Dec 2019 23:34:55 +0800 Subject: [PATCH 1/2] Use ?Sized in Mutex and RwLock --- src/sync/mutex.rs | 30 ++++++++++++++++-------------- src/sync/rwlock.rs | 44 +++++++++++++++++++++++--------------------- 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/sync/mutex.rs b/src/sync/mutex.rs index 4d2cf25..3782bb6 100644 --- a/src/sync/mutex.rs +++ b/src/sync/mutex.rs @@ -39,14 +39,14 @@ use crate::task::{Context, Poll}; /// # /// # }) /// ``` -pub struct Mutex { +pub struct Mutex { locked: AtomicBool, wakers: WakerSet, value: UnsafeCell, } -unsafe impl Send for Mutex {} -unsafe impl Sync for Mutex {} +unsafe impl Send for Mutex {} +unsafe impl Sync for Mutex {} impl Mutex { /// Creates a new mutex. @@ -65,7 +65,9 @@ impl Mutex { value: UnsafeCell::new(t), } } +} +impl Mutex { /// Acquires the lock. /// /// Returns a guard that releases the lock when dropped. @@ -189,7 +191,7 @@ impl Mutex { /// let mutex = Mutex::new(10); /// assert_eq!(mutex.into_inner(), 10); /// ``` - pub fn into_inner(self) -> T { + pub fn into_inner(self) -> T where T: Sized { self.value.into_inner() } @@ -216,7 +218,7 @@ impl Mutex { } } -impl fmt::Debug for Mutex { +impl fmt::Debug for Mutex { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { struct Locked; impl fmt::Debug for Locked { @@ -238,19 +240,19 @@ impl From for Mutex { } } -impl Default for Mutex { +impl Default for Mutex { fn default() -> Mutex { Mutex::new(Default::default()) } } /// A guard that releases the lock when dropped. -pub struct MutexGuard<'a, T>(&'a Mutex); +pub struct MutexGuard<'a, T: ?Sized>(&'a Mutex); -unsafe impl Send for MutexGuard<'_, T> {} -unsafe impl Sync for MutexGuard<'_, T> {} +unsafe impl Send for MutexGuard<'_, T> {} +unsafe impl Sync for MutexGuard<'_, T> {} -impl Drop for MutexGuard<'_, T> { +impl Drop for MutexGuard<'_, T> { fn drop(&mut self) { // Use `SeqCst` ordering to synchronize with `WakerSet::insert()` and `WakerSet::update()`. self.0.locked.store(false, Ordering::SeqCst); @@ -260,19 +262,19 @@ impl Drop for MutexGuard<'_, T> { } } -impl fmt::Debug for MutexGuard<'_, T> { +impl fmt::Debug for MutexGuard<'_, T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&**self, f) } } -impl fmt::Display for MutexGuard<'_, T> { +impl fmt::Display for MutexGuard<'_, T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { (**self).fmt(f) } } -impl Deref for MutexGuard<'_, T> { +impl Deref for MutexGuard<'_, T> { type Target = T; fn deref(&self) -> &T { @@ -280,7 +282,7 @@ impl Deref for MutexGuard<'_, T> { } } -impl DerefMut for MutexGuard<'_, T> { +impl DerefMut for MutexGuard<'_, T> { fn deref_mut(&mut self) -> &mut T { unsafe { &mut *self.0.value.get() } } diff --git a/src/sync/rwlock.rs b/src/sync/rwlock.rs index bc3f640..a748607 100644 --- a/src/sync/rwlock.rs +++ b/src/sync/rwlock.rs @@ -49,15 +49,15 @@ const READ_COUNT_MASK: usize = !(ONE_READ - 1); /// # /// # }) /// ``` -pub struct RwLock { +pub struct RwLock { state: AtomicUsize, read_wakers: WakerSet, write_wakers: WakerSet, value: UnsafeCell, } -unsafe impl Send for RwLock {} -unsafe impl Sync for RwLock {} +unsafe impl Send for RwLock {} +unsafe impl Sync for RwLock {} impl RwLock { /// Creates a new reader-writer lock. @@ -77,7 +77,9 @@ impl RwLock { value: UnsafeCell::new(t), } } +} +impl RwLock { /// Acquires a read lock. /// /// Returns a guard that releases the lock when dropped. @@ -316,7 +318,7 @@ impl RwLock { /// let lock = RwLock::new(10); /// assert_eq!(lock.into_inner(), 10); /// ``` - pub fn into_inner(self) -> T { + pub fn into_inner(self) -> T where T: Sized { self.value.into_inner() } @@ -343,7 +345,7 @@ impl RwLock { } } -impl fmt::Debug for RwLock { +impl fmt::Debug for RwLock { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { struct Locked; impl fmt::Debug for Locked { @@ -365,19 +367,19 @@ impl From for RwLock { } } -impl Default for RwLock { +impl Default for RwLock { fn default() -> RwLock { RwLock::new(Default::default()) } } /// A guard that releases the read lock when dropped. -pub struct RwLockReadGuard<'a, T>(&'a RwLock); +pub struct RwLockReadGuard<'a, T: ?Sized>(&'a RwLock); -unsafe impl Send for RwLockReadGuard<'_, T> {} -unsafe impl Sync for RwLockReadGuard<'_, T> {} +unsafe impl Send for RwLockReadGuard<'_, T> {} +unsafe impl Sync for RwLockReadGuard<'_, T> {} -impl Drop for RwLockReadGuard<'_, T> { +impl Drop for RwLockReadGuard<'_, T> { fn drop(&mut self) { let state = self.0.state.fetch_sub(ONE_READ, Ordering::SeqCst); @@ -388,19 +390,19 @@ impl Drop for RwLockReadGuard<'_, T> { } } -impl fmt::Debug for RwLockReadGuard<'_, T> { +impl fmt::Debug for RwLockReadGuard<'_, T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&**self, f) } } -impl fmt::Display for RwLockReadGuard<'_, T> { +impl fmt::Display for RwLockReadGuard<'_, T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { (**self).fmt(f) } } -impl Deref for RwLockReadGuard<'_, T> { +impl Deref for RwLockReadGuard<'_, T> { type Target = T; fn deref(&self) -> &T { @@ -409,12 +411,12 @@ impl Deref for RwLockReadGuard<'_, T> { } /// A guard that releases the write lock when dropped. -pub struct RwLockWriteGuard<'a, T>(&'a RwLock); +pub struct RwLockWriteGuard<'a, T: ?Sized>(&'a RwLock); -unsafe impl Send for RwLockWriteGuard<'_, T> {} -unsafe impl Sync for RwLockWriteGuard<'_, T> {} +unsafe impl Send for RwLockWriteGuard<'_, T> {} +unsafe impl Sync for RwLockWriteGuard<'_, T> {} -impl Drop for RwLockWriteGuard<'_, T> { +impl Drop for RwLockWriteGuard<'_, T> { fn drop(&mut self) { self.0.state.store(0, Ordering::SeqCst); @@ -427,19 +429,19 @@ impl Drop for RwLockWriteGuard<'_, T> { } } -impl fmt::Debug for RwLockWriteGuard<'_, T> { +impl fmt::Debug for RwLockWriteGuard<'_, T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&**self, f) } } -impl fmt::Display for RwLockWriteGuard<'_, T> { +impl fmt::Display for RwLockWriteGuard<'_, T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { (**self).fmt(f) } } -impl Deref for RwLockWriteGuard<'_, T> { +impl Deref for RwLockWriteGuard<'_, T> { type Target = T; fn deref(&self) -> &T { @@ -447,7 +449,7 @@ impl Deref for RwLockWriteGuard<'_, T> { } } -impl DerefMut for RwLockWriteGuard<'_, T> { +impl DerefMut for RwLockWriteGuard<'_, T> { fn deref_mut(&mut self) -> &mut T { unsafe { &mut *self.0.value.get() } } From 732ef10f9812e1581132af5e077a7b27de414dd2 Mon Sep 17 00:00:00 2001 From: Gary Guo Date: Sat, 14 Dec 2019 23:42:14 +0800 Subject: [PATCH 2/2] Make code compile --- src/sync/mutex.rs | 6 +++--- src/sync/rwlock.rs | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/sync/mutex.rs b/src/sync/mutex.rs index 3782bb6..c62b561 100644 --- a/src/sync/mutex.rs +++ b/src/sync/mutex.rs @@ -93,12 +93,12 @@ impl Mutex { /// # }) /// ``` pub async fn lock(&self) -> MutexGuard<'_, T> { - pub struct LockFuture<'a, T> { + pub struct LockFuture<'a, T: ?Sized> { mutex: &'a Mutex, opt_key: Option, } - impl<'a, T> Future for LockFuture<'a, T> { + impl<'a, T: ?Sized> Future for LockFuture<'a, T> { type Output = MutexGuard<'a, T>; fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { @@ -125,7 +125,7 @@ impl Mutex { } } - impl Drop for LockFuture<'_, T> { + impl Drop for LockFuture<'_, T> { fn drop(&mut self) { // If the current task is still in the set, that means it is being cancelled now. if let Some(key) = self.opt_key { diff --git a/src/sync/rwlock.rs b/src/sync/rwlock.rs index a748607..08d8ed8 100644 --- a/src/sync/rwlock.rs +++ b/src/sync/rwlock.rs @@ -101,12 +101,12 @@ impl RwLock { /// # }) /// ``` pub async fn read(&self) -> RwLockReadGuard<'_, T> { - pub struct ReadFuture<'a, T> { + pub struct ReadFuture<'a, T: ?Sized> { lock: &'a RwLock, opt_key: Option, } - impl<'a, T> Future for ReadFuture<'a, T> { + impl<'a, T: ?Sized> Future for ReadFuture<'a, T> { type Output = RwLockReadGuard<'a, T>; fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { @@ -133,7 +133,7 @@ impl RwLock { } } - impl Drop for ReadFuture<'_, T> { + impl Drop for ReadFuture<'_, T> { fn drop(&mut self) { // If the current task is still in the set, that means it is being cancelled now. if let Some(key) = self.opt_key { @@ -226,12 +226,12 @@ impl RwLock { /// # }) /// ``` pub async fn write(&self) -> RwLockWriteGuard<'_, T> { - pub struct WriteFuture<'a, T> { + pub struct WriteFuture<'a, T: ?Sized> { lock: &'a RwLock, opt_key: Option, } - impl<'a, T> Future for WriteFuture<'a, T> { + impl<'a, T: ?Sized> Future for WriteFuture<'a, T> { type Output = RwLockWriteGuard<'a, T>; fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { @@ -258,7 +258,7 @@ impl RwLock { } } - impl Drop for WriteFuture<'_, T> { + impl Drop for WriteFuture<'_, T> { fn drop(&mut self) { // If the current task is still in the set, that means it is being cancelled now. if let Some(key) = self.opt_key {