Skip to content

Commit 6408473

Browse files
taiki-ecramertj
authored andcommitted
Allow ?Sized types in some methods and structs
1 parent db79aef commit 6408473

File tree

7 files changed

+41
-41
lines changed

7 files changed

+41
-41
lines changed

futures-util/src/future/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ pub trait FutureExt: Future {
529529

530530
/// A convenience for calling `Future::poll` on `Unpin` future types.
531531
fn poll_unpin(&mut self, cx: &mut Context<'_>) -> Poll<Self::Output>
532-
where Self: Unpin + Sized
532+
where Self: Unpin
533533
{
534534
Pin::new(self).poll(cx)
535535
}

futures-util/src/lock/mutex.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ use std::sync::Mutex as StdMutex;
99
use std::sync::atomic::{AtomicUsize, Ordering};
1010

1111
/// A futures-aware mutex.
12-
pub struct Mutex<T> {
12+
pub struct Mutex<T: ?Sized> {
1313
state: AtomicUsize,
14-
value: UnsafeCell<T>,
1514
waiters: StdMutex<Slab<Waiter>>,
15+
value: UnsafeCell<T>,
1616
}
1717

18-
impl<T> fmt::Debug for Mutex<T> {
18+
impl<T: ?Sized> fmt::Debug for Mutex<T> {
1919
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2020
let state = self.state.load(Ordering::SeqCst);
2121
f.debug_struct("Mutex")
@@ -65,7 +65,9 @@ impl<T> Mutex<T> {
6565
waiters: StdMutex::new(Slab::new()),
6666
}
6767
}
68+
}
6869

70+
impl<T: ?Sized> Mutex<T> {
6971
/// Attempt to acquire the lock immediately.
7072
///
7173
/// If the lock is currently held, this will return `None`.
@@ -116,13 +118,13 @@ impl<T> Mutex<T> {
116118
const WAIT_KEY_NONE: usize = usize::max_value();
117119

118120
/// A future which resolves when the target mutex has been successfully acquired.
119-
pub struct MutexLockFuture<'a, T> {
121+
pub struct MutexLockFuture<'a, T: ?Sized> {
120122
// `None` indicates that the mutex was successfully acquired.
121123
mutex: Option<&'a Mutex<T>>,
122124
wait_key: usize,
123125
}
124126

125-
impl<T> fmt::Debug for MutexLockFuture<'_, T> {
127+
impl<T: ?Sized> fmt::Debug for MutexLockFuture<'_, T> {
126128
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127129
f.debug_struct("MutexLockFuture")
128130
.field("was_acquired", &self.mutex.is_none())
@@ -138,13 +140,13 @@ impl<T> fmt::Debug for MutexLockFuture<'_, T> {
138140
}
139141
}
140142

141-
impl<T> FusedFuture for MutexLockFuture<'_, T> {
143+
impl<T: ?Sized> FusedFuture for MutexLockFuture<'_, T> {
142144
fn is_terminated(&self) -> bool {
143145
self.mutex.is_none()
144146
}
145147
}
146148

147-
impl<'a, T> Future for MutexLockFuture<'a, T> {
149+
impl<'a, T: ?Sized> Future for MutexLockFuture<'a, T> {
148150
type Output = MutexGuard<'a, T>;
149151

150152
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
@@ -180,7 +182,7 @@ impl<'a, T> Future for MutexLockFuture<'a, T> {
180182
}
181183
}
182184

183-
impl<T> Drop for MutexLockFuture<'_, T> {
185+
impl<T: ?Sized> Drop for MutexLockFuture<'_, T> {
184186
fn drop(&mut self) {
185187
if let Some(mutex) = self.mutex {
186188
// This future was dropped before it acquired the mutex.
@@ -195,11 +197,11 @@ impl<T> Drop for MutexLockFuture<'_, T> {
195197
/// An RAII guard returned by the `lock` and `try_lock` methods.
196198
/// When this structure is dropped (falls out of scope), the lock will be
197199
/// unlocked.
198-
pub struct MutexGuard<'a, T> {
200+
pub struct MutexGuard<'a, T: ?Sized> {
199201
mutex: &'a Mutex<T>,
200202
}
201203

202-
impl<T: fmt::Debug> fmt::Debug for MutexGuard<'_, T> {
204+
impl<T: ?Sized + fmt::Debug> fmt::Debug for MutexGuard<'_, T> {
203205
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
204206
f.debug_struct("MutexGuard")
205207
.field("value", &*self)
@@ -208,7 +210,7 @@ impl<T: fmt::Debug> fmt::Debug for MutexGuard<'_, T> {
208210
}
209211
}
210212

211-
impl<T> Drop for MutexGuard<'_, T> {
213+
impl<T: ?Sized> Drop for MutexGuard<'_, T> {
212214
fn drop(&mut self) {
213215
let old_state = self.mutex.state.fetch_and(!IS_LOCKED, Ordering::AcqRel);
214216
if (old_state & HAS_WAITERS) != 0 {
@@ -220,31 +222,31 @@ impl<T> Drop for MutexGuard<'_, T> {
220222
}
221223
}
222224

223-
impl<T> Deref for MutexGuard<'_, T> {
225+
impl<T: ?Sized> Deref for MutexGuard<'_, T> {
224226
type Target = T;
225227
fn deref(&self) -> &T {
226228
unsafe { &*self.mutex.value.get() }
227229
}
228230
}
229231

230-
impl<T> DerefMut for MutexGuard<'_, T> {
232+
impl<T: ?Sized> DerefMut for MutexGuard<'_, T> {
231233
fn deref_mut(&mut self) -> &mut T {
232234
unsafe { &mut *self.mutex.value.get() }
233235
}
234236
}
235237

236238
// Mutexes can be moved freely between threads and acquired on any thread so long
237239
// as the inner value can be safely sent between threads.
238-
unsafe impl<T: Send> Send for Mutex<T> {}
239-
unsafe impl<T: Send> Sync for Mutex<T> {}
240+
unsafe impl<T: ?Sized + Send> Send for Mutex<T> {}
241+
unsafe impl<T: ?Sized + Send> Sync for Mutex<T> {}
240242

241243
// It's safe to switch which thread the acquire is being attempted on so long as
242244
// `T` can be accessed on that thread.
243-
unsafe impl<T: Send> Send for MutexLockFuture<'_, T> {}
245+
unsafe impl<T: ?Sized + Send> Send for MutexLockFuture<'_, T> {}
244246
// doesn't have any interesting `&self` methods (only Debug)
245-
unsafe impl<T> Sync for MutexLockFuture<'_, T> {}
247+
unsafe impl<T: ?Sized> Sync for MutexLockFuture<'_, T> {}
246248

247249
// Safe to send since we don't track any thread-specific details-- the inner
248250
// lock is essentially spinlock-equivalent (attempt to flip an atomic bool)
249-
unsafe impl<T: Send> Send for MutexGuard<'_, T> {}
250-
unsafe impl<T: Sync> Sync for MutexGuard<'_, T> {}
251+
unsafe impl<T: ?Sized + Send> Send for MutexGuard<'_, T> {}
252+
unsafe impl<T: ?Sized + Sync> Sync for MutexGuard<'_, T> {}

futures-util/src/stream/mod.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ pub trait StreamExt: Stream {
181181
/// # });
182182
/// ```
183183
fn next(&mut self) -> Next<'_, Self>
184-
where Self: Sized + Unpin,
184+
where Self: Unpin,
185185
{
186186
Next::new(self)
187187
}
@@ -830,9 +830,7 @@ pub trait StreamExt: Stream {
830830
/// assert_eq!(sum, 7);
831831
/// # });
832832
/// ```
833-
fn by_ref(&mut self) -> &mut Self
834-
where Self: Sized
835-
{
833+
fn by_ref(&mut self) -> &mut Self {
836834
self
837835
}
838836

@@ -1142,7 +1140,7 @@ pub trait StreamExt: Stream {
11421140
&mut self,
11431141
cx: &mut Context<'_>,
11441142
) -> Poll<Option<Self::Item>>
1145-
where Self: Unpin + Sized
1143+
where Self: Unpin
11461144
{
11471145
Pin::new(self).poll_next(cx)
11481146
}
@@ -1196,7 +1194,7 @@ pub trait StreamExt: Stream {
11961194
/// assert_eq!(total, 6);
11971195
/// # });
11981196
/// ```
1199-
fn select_next_some(&mut self) -> SelectNextSome<'_, Self> where Self: Sized + Unpin + FusedStream {
1197+
fn select_next_some(&mut self) -> SelectNextSome<'_, Self> where Self: Unpin + FusedStream {
12001198
SelectNextSome::new(self)
12011199
}
12021200
}

futures-util/src/stream/next.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@ use futures_core::task::{Context, Poll};
77
/// Future for the [`next`](super::StreamExt::next) method.
88
#[derive(Debug)]
99
#[must_use = "futures do nothing unless you `.await` or poll them"]
10-
pub struct Next<'a, St> {
10+
pub struct Next<'a, St: ?Sized> {
1111
stream: &'a mut St,
1212
}
1313

14-
impl<St: Stream + Unpin> Unpin for Next<'_, St> {}
14+
impl<St: ?Sized + Stream + Unpin> Unpin for Next<'_, St> {}
1515

16-
impl<'a, St: Stream + Unpin> Next<'a, St> {
16+
impl<'a, St: ?Sized + Stream + Unpin> Next<'a, St> {
1717
pub(super) fn new(stream: &'a mut St) -> Self {
1818
Next { stream }
1919
}
2020
}
2121

22-
impl<St: FusedStream> FusedFuture for Next<'_, St> {
22+
impl<St: ?Sized + FusedStream> FusedFuture for Next<'_, St> {
2323
fn is_terminated(&self) -> bool {
2424
self.stream.is_terminated()
2525
}
2626
}
2727

28-
impl<St: Stream + Unpin> Future for Next<'_, St> {
28+
impl<St: ?Sized + Stream + Unpin> Future for Next<'_, St> {
2929
type Output = Option<St::Item>;
3030

3131
fn poll(

futures-util/src/stream/select_next_some.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ use crate::stream::StreamExt;
88
/// method.
99
#[derive(Debug)]
1010
#[must_use = "futures do nothing unless you `.await` or poll them"]
11-
pub struct SelectNextSome<'a, St> {
11+
pub struct SelectNextSome<'a, St: ?Sized> {
1212
stream: &'a mut St,
1313
}
1414

15-
impl<'a, St> SelectNextSome<'a, St> {
15+
impl<'a, St: ?Sized> SelectNextSome<'a, St> {
1616
pub(super) fn new(stream: &'a mut St) -> Self {
1717
SelectNextSome { stream }
1818
}
1919
}
2020

21-
impl<St: FusedStream> FusedFuture for SelectNextSome<'_, St> {
21+
impl<St: ?Sized + FusedStream> FusedFuture for SelectNextSome<'_, St> {
2222
fn is_terminated(&self) -> bool {
2323
self.stream.is_terminated()
2424
}
2525
}
2626

27-
impl<St: Stream + FusedStream + Unpin> Future for SelectNextSome<'_, St> {
27+
impl<St: ?Sized + Stream + FusedStream + Unpin> Future for SelectNextSome<'_, St> {
2828
type Output = St::Item;
2929

3030
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {

futures-util/src/try_stream/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ pub trait TryStreamExt: TryStream {
303303
/// # })
304304
/// ```
305305
fn try_next(&mut self) -> TryNext<'_, Self>
306-
where Self: Sized + Unpin,
306+
where Self: Unpin,
307307
{
308308
TryNext::new(self)
309309
}

futures-util/src/try_stream/try_next.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@ use futures_core::task::{Context, Poll};
77
/// Future for the [`try_next`](super::TryStreamExt::try_next) method.
88
#[derive(Debug)]
99
#[must_use = "futures do nothing unless you `.await` or poll them"]
10-
pub struct TryNext<'a, St: Unpin> {
10+
pub struct TryNext<'a, St: ?Sized + Unpin> {
1111
stream: &'a mut St,
1212
}
1313

14-
impl<St: Unpin> Unpin for TryNext<'_, St> {}
14+
impl<St: ?Sized + Unpin> Unpin for TryNext<'_, St> {}
1515

16-
impl<'a, St: TryStream + Unpin> TryNext<'a, St> {
16+
impl<'a, St: ?Sized + TryStream + Unpin> TryNext<'a, St> {
1717
pub(super) fn new(stream: &'a mut St) -> Self {
1818
TryNext { stream }
1919
}
2020
}
2121

22-
impl<St: Unpin + FusedStream> FusedFuture for TryNext<'_, St> {
22+
impl<St: ?Sized + Unpin + FusedStream> FusedFuture for TryNext<'_, St> {
2323
fn is_terminated(&self) -> bool {
2424
self.stream.is_terminated()
2525
}
2626
}
2727

28-
impl<St: TryStream + Unpin> Future for TryNext<'_, St> {
28+
impl<St: ?Sized + TryStream + Unpin> Future for TryNext<'_, St> {
2929
type Output = Result<Option<St::Ok>, St::Error>;
3030

3131
fn poll(

0 commit comments

Comments
 (0)