diff options
author | Taiki Endo <te316e89@gmail.com> | 2020-06-13 03:32:51 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-13 03:32:51 +0900 |
commit | d2f81b506a469481ab0b62aaeaf48fc8c60e4c66 (patch) | |
tree | 57bb0415a90ce1eac544bd38ea7af7cbc0978368 /tokio/src/sync/mutex.rs | |
parent | 6b6e76080afc92450238df69c4edc12ee5f7518d (diff) |
sync: allow unsized types in Mutex and RwLock (#2615)
Diffstat (limited to 'tokio/src/sync/mutex.rs')
-rw-r--r-- | tokio/src/sync/mutex.rs | 49 |
1 files changed, 27 insertions, 22 deletions
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<T> { - c: UnsafeCell<T>, +pub struct Mutex<T: ?Sized> { s: semaphore::Semaphore, + c: UnsafeCell<T>, } /// A handle to a held `Mutex`. @@ -112,7 +111,7 @@ pub struct Mutex<T> { /// /// 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<T>, } @@ -131,17 +130,17 @@ pub struct MutexGuard<'a, T> { /// point `lock` will succeed yet again. /// /// [`Arc`]: std::sync::Arc -pub struct OwnedMutexGuard<T> { +pub struct OwnedMutexGuard<T: ?Sized> { lock: Arc<Mutex<T>>, } // As long as T: Send, it's fine to send and share Mutex<T> between threads. // If T was not Send, sending and sharing a Mutex<T> would be bad, since you can // access T through Mutex<T>. -unsafe impl<T> Send for Mutex<T> where T: Send {} -unsafe impl<T> Sync for Mutex<T> where T: Send {} -unsafe impl<'a, T> Sync for MutexGuard<'a, T> where T: Send + Sync {} -unsafe impl<T> Sync for OwnedMutexGuard<T> where T: Send + Sync {} +unsafe impl<T> Send for Mutex<T> where T: ?Sized + Send {} +unsafe impl<T> Sync for Mutex<T> where T: ?Sized + Send {} +unsafe impl<T> Sync for MutexGuard<'_, T> where T: ?Sized + Send + Sync {} +unsafe impl<T> Sync for OwnedMutexGuard<T> 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<T> Mutex<T> { +impl<T: ?Sized> Mutex<T> { /// Creates a new lock in an unlocked state ready for use. /// /// # Examples @@ -193,7 +192,10 @@ impl<T> Mutex<T> { /// /// 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<T> Mutex<T> { /// 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<T: ?Sized> Drop for MutexGuard<'_, T> { fn drop(&mut self) { self.lock.s.release(1) } } -impl<'a, T> Deref for MutexGuard<'a, T> { +impl<T: ?Sized> 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<T: ?Sized> 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<T: ?Sized + fmt::Debug> 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<T: ?Sized + fmt::Display> 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<T> Drop for OwnedMutexGuard<T> { +impl<T: ?Sized> Drop for OwnedMutexGuard<T> { fn drop(&mut self) { self.lock.s.release(1) } } -impl<T> Deref for OwnedMutexGuard<T> { +impl<T: ?Sized> Deref for OwnedMutexGuard<T> { type Target = T; fn deref(&self) -> &Self::Target { unsafe { &*self.lock.c.get() } } } -impl<T> DerefMut for OwnedMutexGuard<T> { +impl<T: ?Sized> DerefMut for OwnedMutexGuard<T> { fn deref_mut(&mut self) -> &mut Self::Target { unsafe { &mut *self.lock.c.get() } } } -impl<T: fmt::Debug> fmt::Debug for OwnedMutexGuard<T> { +impl<T: ?Sized + fmt::Debug> fmt::Debug for OwnedMutexGuard<T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&**self, f) } } -impl<T: fmt::Display> fmt::Display for OwnedMutexGuard<T> { +impl<T: ?Sized + fmt::Display> fmt::Display for OwnedMutexGuard<T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Display::fmt(&**self, f) } |