From d2f81b506a469481ab0b62aaeaf48fc8c60e4c66 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sat, 13 Jun 2020 03:32:51 +0900 Subject: sync: allow unsized types in Mutex and RwLock (#2615) --- tokio/src/sync/mutex.rs | 49 +++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 22 deletions(-) (limited to 'tokio/src/sync/mutex.rs') diff --git a/tokio/src/sync/mutex.rs b/tokio/src/sync/mutex.rs index 5e8bf68a..c5f839ee 100644 --- a/tokio/src/sync/mutex.rs +++ b/tokio/src/sync/mutex.rs @@ -97,11 +97,10 @@ use std::sync::Arc; /// [`std::sync::Mutex`]: struct@std::sync::Mutex /// [`Send`]: trait@std::marker::Send /// [`lock`]: method@Mutex::lock - #[derive(Debug)] -pub struct Mutex { - c: UnsafeCell, +pub struct Mutex { s: semaphore::Semaphore, + c: UnsafeCell, } /// A handle to a held `Mutex`. @@ -112,7 +111,7 @@ pub struct Mutex { /// /// The lock is automatically released whenever the guard is dropped, at which /// point `lock` will succeed yet again. -pub struct MutexGuard<'a, T> { +pub struct MutexGuard<'a, T: ?Sized> { lock: &'a Mutex, } @@ -131,17 +130,17 @@ pub struct MutexGuard<'a, T> { /// point `lock` will succeed yet again. /// /// [`Arc`]: std::sync::Arc -pub struct OwnedMutexGuard { +pub struct OwnedMutexGuard { lock: Arc>, } // As long as T: Send, it's fine to send and share Mutex between threads. // If T was not Send, sending and sharing a Mutex would be bad, since you can // access T through Mutex. -unsafe impl Send for Mutex where T: Send {} -unsafe impl Sync for Mutex where T: Send {} -unsafe impl<'a, T> Sync for MutexGuard<'a, T> where T: Send + Sync {} -unsafe impl Sync for OwnedMutexGuard where T: Send + Sync {} +unsafe impl Send for Mutex where T: ?Sized + Send {} +unsafe impl Sync for Mutex where T: ?Sized + Send {} +unsafe impl Sync for MutexGuard<'_, T> where T: ?Sized + Send + Sync {} +unsafe impl Sync for OwnedMutexGuard where T: ?Sized + Send + Sync {} /// Error returned from the [`Mutex::try_lock`] function. /// @@ -183,7 +182,7 @@ fn bounds() { check_static_val(arc_mutex.lock_owned()); } -impl Mutex { +impl Mutex { /// Creates a new lock in an unlocked state ready for use. /// /// # Examples @@ -193,7 +192,10 @@ impl Mutex { /// /// let lock = Mutex::new(5); /// ``` - pub fn new(t: T) -> Self { + pub fn new(t: T) -> Self + where + T: Sized, + { Self { c: UnsafeCell::new(t), s: semaphore::Semaphore::new(1), @@ -330,7 +332,10 @@ impl Mutex { /// assert_eq!(n, 1); /// } /// ``` - pub fn into_inner(self) -> T { + pub fn into_inner(self) -> T + where + T: Sized, + { self.c.into_inner() } } @@ -352,32 +357,32 @@ where // === impl MutexGuard === -impl<'a, T> Drop for MutexGuard<'a, T> { +impl Drop for MutexGuard<'_, T> { fn drop(&mut self) { self.lock.s.release(1) } } -impl<'a, T> Deref for MutexGuard<'a, T> { +impl Deref for MutexGuard<'_, T> { type Target = T; fn deref(&self) -> &Self::Target { unsafe { &*self.lock.c.get() } } } -impl<'a, T> DerefMut for MutexGuard<'a, T> { +impl DerefMut for MutexGuard<'_, T> { fn deref_mut(&mut self) -> &mut Self::Target { unsafe { &mut *self.lock.c.get() } } } -impl<'a, T: fmt::Debug> fmt::Debug for MutexGuard<'a, T> { +impl fmt::Debug for MutexGuard<'_, T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&**self, f) } } -impl<'a, T: fmt::Display> fmt::Display for MutexGuard<'a, T> { +impl fmt::Display for MutexGuard<'_, T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Display::fmt(&**self, f) } @@ -385,32 +390,32 @@ impl<'a, T: fmt::Display> fmt::Display for MutexGuard<'a, T> { // === impl OwnedMutexGuard === -impl Drop for OwnedMutexGuard { +impl Drop for OwnedMutexGuard { fn drop(&mut self) { self.lock.s.release(1) } } -impl Deref for OwnedMutexGuard { +impl Deref for OwnedMutexGuard { type Target = T; fn deref(&self) -> &Self::Target { unsafe { &*self.lock.c.get() } } } -impl DerefMut for OwnedMutexGuard { +impl DerefMut for OwnedMutexGuard { fn deref_mut(&mut self) -> &mut Self::Target { unsafe { &mut *self.lock.c.get() } } } -impl fmt::Debug for OwnedMutexGuard { +impl fmt::Debug for OwnedMutexGuard { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&**self, f) } } -impl fmt::Display for OwnedMutexGuard { +impl fmt::Display for OwnedMutexGuard { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Display::fmt(&**self, f) } -- cgit v1.2.3