From d0ff3ba5e4cde9d446ca9498fd0fc6f681a36397 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sat, 8 Jun 2019 15:20:02 +0900 Subject: [PATCH] Allow ?Sized types in some methods and structs --- futures-util/src/future/mod.rs | 2 +- futures-util/src/lock/mutex.rs | 40 +++++++++++---------- futures-util/src/stream/mod.rs | 10 +++--- futures-util/src/stream/next.rs | 10 +++--- futures-util/src/stream/select_next_some.rs | 8 ++--- futures-util/src/try_stream/mod.rs | 2 +- futures-util/src/try_stream/try_next.rs | 10 +++--- 7 files changed, 41 insertions(+), 41 deletions(-) diff --git a/futures-util/src/future/mod.rs b/futures-util/src/future/mod.rs index 6f6b85fa6f..4ba29e3a39 100644 --- a/futures-util/src/future/mod.rs +++ b/futures-util/src/future/mod.rs @@ -529,7 +529,7 @@ pub trait FutureExt: Future { /// A convenience for calling `Future::poll` on `Unpin` future types. fn poll_unpin(&mut self, cx: &mut Context<'_>) -> Poll - where Self: Unpin + Sized + where Self: Unpin { Pin::new(self).poll(cx) } diff --git a/futures-util/src/lock/mutex.rs b/futures-util/src/lock/mutex.rs index 6dd082abd0..df13413e4b 100644 --- a/futures-util/src/lock/mutex.rs +++ b/futures-util/src/lock/mutex.rs @@ -9,13 +9,13 @@ use std::sync::Mutex as StdMutex; use std::sync::atomic::{AtomicUsize, Ordering}; /// A futures-aware mutex. -pub struct Mutex { +pub struct Mutex { state: AtomicUsize, - value: UnsafeCell, waiters: StdMutex>, + value: UnsafeCell, } -impl fmt::Debug for Mutex { +impl fmt::Debug for Mutex { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let state = self.state.load(Ordering::SeqCst); f.debug_struct("Mutex") @@ -65,7 +65,9 @@ impl Mutex { waiters: StdMutex::new(Slab::new()), } } +} +impl Mutex { /// Attempt to acquire the lock immediately. /// /// If the lock is currently held, this will return `None`. @@ -116,13 +118,13 @@ impl Mutex { const WAIT_KEY_NONE: usize = usize::MAX; /// A future which resolves when the target mutex has been successfully acquired. -pub struct MutexLockFuture<'a, T> { +pub struct MutexLockFuture<'a, T: ?Sized> { // `None` indicates that the mutex was successfully acquired. mutex: Option<&'a Mutex>, wait_key: usize, } -impl fmt::Debug for MutexLockFuture<'_, T> { +impl fmt::Debug for MutexLockFuture<'_, T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("MutexLockFuture") .field("was_acquired", &self.mutex.is_none()) @@ -138,13 +140,13 @@ impl fmt::Debug for MutexLockFuture<'_, T> { } } -impl FusedFuture for MutexLockFuture<'_, T> { +impl FusedFuture for MutexLockFuture<'_, T> { fn is_terminated(&self) -> bool { self.mutex.is_none() } } -impl<'a, T> Future for MutexLockFuture<'a, T> { +impl<'a, T: ?Sized> Future for MutexLockFuture<'a, T> { type Output = MutexGuard<'a, T>; fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { @@ -180,7 +182,7 @@ impl<'a, T> Future for MutexLockFuture<'a, T> { } } -impl Drop for MutexLockFuture<'_, T> { +impl Drop for MutexLockFuture<'_, T> { fn drop(&mut self) { if let Some(mutex) = self.mutex { // This future was dropped before it acquired the mutex. @@ -195,11 +197,11 @@ impl Drop for MutexLockFuture<'_, T> { /// An RAII guard returned by the `lock` and `try_lock` methods. /// When this structure is dropped (falls out of scope), the lock will be /// unlocked. -pub struct MutexGuard<'a, T> { +pub struct MutexGuard<'a, T: ?Sized> { mutex: &'a Mutex, } -impl fmt::Debug for MutexGuard<'_, T> { +impl fmt::Debug for MutexGuard<'_, T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("MutexGuard") .field("value", &*self) @@ -208,7 +210,7 @@ impl fmt::Debug for MutexGuard<'_, T> { } } -impl Drop for MutexGuard<'_, T> { +impl Drop for MutexGuard<'_, T> { fn drop(&mut self) { let old_state = self.mutex.state.fetch_and(!IS_LOCKED, Ordering::AcqRel); if (old_state & HAS_WAITERS) != 0 { @@ -220,14 +222,14 @@ impl Drop for MutexGuard<'_, T> { } } -impl Deref for MutexGuard<'_, T> { +impl Deref for MutexGuard<'_, T> { type Target = T; fn deref(&self) -> &T { unsafe { &*self.mutex.value.get() } } } -impl DerefMut for MutexGuard<'_, T> { +impl DerefMut for MutexGuard<'_, T> { fn deref_mut(&mut self) -> &mut T { unsafe { &mut *self.mutex.value.get() } } @@ -235,16 +237,16 @@ impl DerefMut for MutexGuard<'_, T> { // Mutexes can be moved freely between threads and acquired on any thread so long // as the inner value can be safely sent between threads. -unsafe impl Send for Mutex {} -unsafe impl Sync for Mutex {} +unsafe impl Send for Mutex {} +unsafe impl Sync for Mutex {} // It's safe to switch which thread the acquire is being attempted on so long as // `T` can be accessed on that thread. -unsafe impl Send for MutexLockFuture<'_, T> {} +unsafe impl Send for MutexLockFuture<'_, T> {} // doesn't have any interesting `&self` methods (only Debug) -unsafe impl Sync for MutexLockFuture<'_, T> {} +unsafe impl Sync for MutexLockFuture<'_, T> {} // Safe to send since we don't track any thread-specific details-- the inner // lock is essentially spinlock-equivalent (attempt to flip an atomic bool) -unsafe impl Send for MutexGuard<'_, T> {} -unsafe impl Sync for MutexGuard<'_, T> {} +unsafe impl Send for MutexGuard<'_, T> {} +unsafe impl Sync for MutexGuard<'_, T> {} diff --git a/futures-util/src/stream/mod.rs b/futures-util/src/stream/mod.rs index b321ddae55..395939e5a2 100644 --- a/futures-util/src/stream/mod.rs +++ b/futures-util/src/stream/mod.rs @@ -181,7 +181,7 @@ pub trait StreamExt: Stream { /// # }); /// ``` fn next(&mut self) -> Next<'_, Self> - where Self: Sized + Unpin, + where Self: Unpin, { Next::new(self) } @@ -830,9 +830,7 @@ pub trait StreamExt: Stream { /// assert_eq!(sum, 7); /// # }); /// ``` - fn by_ref(&mut self) -> &mut Self - where Self: Sized - { + fn by_ref(&mut self) -> &mut Self { self } @@ -1142,7 +1140,7 @@ pub trait StreamExt: Stream { &mut self, cx: &mut Context<'_>, ) -> Poll> - where Self: Unpin + Sized + where Self: Unpin { Pin::new(self).poll_next(cx) } @@ -1196,7 +1194,7 @@ pub trait StreamExt: Stream { /// assert_eq!(total, 6); /// # }); /// ``` - fn select_next_some(&mut self) -> SelectNextSome<'_, Self> where Self: Sized + Unpin + FusedStream { + fn select_next_some(&mut self) -> SelectNextSome<'_, Self> where Self: Unpin + FusedStream { SelectNextSome::new(self) } } diff --git a/futures-util/src/stream/next.rs b/futures-util/src/stream/next.rs index d489c3ff29..f2ec60cdfc 100644 --- a/futures-util/src/stream/next.rs +++ b/futures-util/src/stream/next.rs @@ -7,25 +7,25 @@ use futures_core::task::{Context, Poll}; /// Future for the [`next`](super::StreamExt::next) method. #[derive(Debug)] #[must_use = "futures do nothing unless you `.await` or poll them"] -pub struct Next<'a, St> { +pub struct Next<'a, St: ?Sized> { stream: &'a mut St, } -impl Unpin for Next<'_, St> {} +impl Unpin for Next<'_, St> {} -impl<'a, St: Stream + Unpin> Next<'a, St> { +impl<'a, St: ?Sized + Stream + Unpin> Next<'a, St> { pub(super) fn new(stream: &'a mut St) -> Self { Next { stream } } } -impl FusedFuture for Next<'_, St> { +impl FusedFuture for Next<'_, St> { fn is_terminated(&self) -> bool { self.stream.is_terminated() } } -impl Future for Next<'_, St> { +impl Future for Next<'_, St> { type Output = Option; fn poll( diff --git a/futures-util/src/stream/select_next_some.rs b/futures-util/src/stream/select_next_some.rs index 335cf295b3..699ce6ebe2 100644 --- a/futures-util/src/stream/select_next_some.rs +++ b/futures-util/src/stream/select_next_some.rs @@ -8,23 +8,23 @@ use crate::stream::StreamExt; /// method. #[derive(Debug)] #[must_use = "futures do nothing unless you `.await` or poll them"] -pub struct SelectNextSome<'a, St> { +pub struct SelectNextSome<'a, St: ?Sized> { stream: &'a mut St, } -impl<'a, St> SelectNextSome<'a, St> { +impl<'a, St: ?Sized> SelectNextSome<'a, St> { pub(super) fn new(stream: &'a mut St) -> Self { SelectNextSome { stream } } } -impl FusedFuture for SelectNextSome<'_, St> { +impl FusedFuture for SelectNextSome<'_, St> { fn is_terminated(&self) -> bool { self.stream.is_terminated() } } -impl Future for SelectNextSome<'_, St> { +impl Future for SelectNextSome<'_, St> { type Output = St::Item; fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { diff --git a/futures-util/src/try_stream/mod.rs b/futures-util/src/try_stream/mod.rs index 048382a4fc..88c29717d0 100644 --- a/futures-util/src/try_stream/mod.rs +++ b/futures-util/src/try_stream/mod.rs @@ -303,7 +303,7 @@ pub trait TryStreamExt: TryStream { /// # }) /// ``` fn try_next(&mut self) -> TryNext<'_, Self> - where Self: Sized + Unpin, + where Self: Unpin, { TryNext::new(self) } diff --git a/futures-util/src/try_stream/try_next.rs b/futures-util/src/try_stream/try_next.rs index fe94623ec5..97d69a4057 100644 --- a/futures-util/src/try_stream/try_next.rs +++ b/futures-util/src/try_stream/try_next.rs @@ -7,25 +7,25 @@ use futures_core::task::{Context, Poll}; /// Future for the [`try_next`](super::TryStreamExt::try_next) method. #[derive(Debug)] #[must_use = "futures do nothing unless you `.await` or poll them"] -pub struct TryNext<'a, St: Unpin> { +pub struct TryNext<'a, St: ?Sized + Unpin> { stream: &'a mut St, } -impl Unpin for TryNext<'_, St> {} +impl Unpin for TryNext<'_, St> {} -impl<'a, St: TryStream + Unpin> TryNext<'a, St> { +impl<'a, St: ?Sized + TryStream + Unpin> TryNext<'a, St> { pub(super) fn new(stream: &'a mut St) -> Self { TryNext { stream } } } -impl FusedFuture for TryNext<'_, St> { +impl FusedFuture for TryNext<'_, St> { fn is_terminated(&self) -> bool { self.stream.is_terminated() } } -impl Future for TryNext<'_, St> { +impl Future for TryNext<'_, St> { type Output = Result, St::Error>; fn poll(