Skip to content

Commit 22e2942

Browse files
taiki-ecramertj
authored andcommitted
Add Future/Stream bounds to FusedFuture/FusedStream
1 parent ab7743d commit 22e2942

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+181
-65
lines changed

futures-core/src/future/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@ pub type BoxFuture<'a, T> = Pin<alloc::boxed::Box<dyn Future<Output = T> + Send
1818
#[cfg(feature = "alloc")]
1919
pub type LocalBoxFuture<'a, T> = Pin<alloc::boxed::Box<dyn Future<Output = T> + 'a>>;
2020

21-
/// A `Future` or `TryFuture` which tracks whether or not the underlying future
21+
/// A future which tracks whether or not the underlying future
2222
/// should no longer be polled.
2323
///
2424
/// `is_terminated` will return `true` if a future should no longer be polled.
2525
/// Usually, this state occurs after `poll` (or `try_poll`) returned
2626
/// `Poll::Ready`. However, `is_terminated` may also return `true` if a future
2727
/// has become inactive and can no longer make progress and should be ignored
2828
/// or dropped rather than being `poll`ed again.
29-
pub trait FusedFuture {
29+
pub trait FusedFuture: Future {
3030
/// Returns `true` if the underlying future should no longer be polled.
3131
fn is_terminated(&self) -> bool;
3232
}
3333

34-
impl<F: FusedFuture + ?Sized> FusedFuture for &mut F {
34+
impl<F: FusedFuture + ?Sized + Unpin> FusedFuture for &mut F {
3535
fn is_terminated(&self) -> bool {
3636
<F as FusedFuture>::is_terminated(&**self)
3737
}
@@ -92,7 +92,7 @@ mod if_alloc {
9292
use alloc::boxed::Box;
9393
use super::*;
9494

95-
impl<F: FusedFuture + ?Sized> FusedFuture for Box<F> {
95+
impl<F: FusedFuture + ?Sized + Unpin> FusedFuture for Box<F> {
9696
fn is_terminated(&self) -> bool {
9797
<F as FusedFuture>::is_terminated(&**self)
9898
}

futures-core/src/stream.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,20 @@ where
8787
}
8888
}
8989

90-
/// A `Stream` or `TryStream` which tracks whether or not the underlying stream
90+
/// A stream which tracks whether or not the underlying stream
9191
/// should no longer be polled.
9292
///
9393
/// `is_terminated` will return `true` if a future should no longer be polled.
9494
/// Usually, this state occurs after `poll_next` (or `try_poll_next`) returned
9595
/// `Poll::Ready(None)`. However, `is_terminated` may also return `true` if a
9696
/// stream has become inactive and can no longer make progress and should be
9797
/// ignored or dropped rather than being polled again.
98-
pub trait FusedStream {
98+
pub trait FusedStream: Stream {
9999
/// Returns `true` if the stream should no longer be polled.
100100
fn is_terminated(&self) -> bool;
101101
}
102102

103-
impl<F: ?Sized + FusedStream> FusedStream for &mut F {
103+
impl<F: ?Sized + FusedStream + Unpin> FusedStream for &mut F {
104104
fn is_terminated(&self) -> bool {
105105
<F as FusedStream>::is_terminated(&**self)
106106
}
@@ -194,7 +194,7 @@ mod if_alloc {
194194
}
195195
}
196196

197-
impl<S: ?Sized + FusedStream> FusedStream for Box<S> {
197+
impl<S: ?Sized + FusedStream + Unpin> FusedStream for Box<S> {
198198
fn is_terminated(&self) -> bool {
199199
<S as FusedStream>::is_terminated(&**self)
200200
}

futures-util/src/future/either.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ where
6969
impl<A, B> FusedFuture for Either<A, B>
7070
where
7171
A: FusedFuture,
72-
B: FusedFuture,
72+
B: FusedFuture<Output = A::Output>,
7373
{
7474
fn is_terminated(&self) -> bool {
7575
match self {
@@ -99,7 +99,7 @@ where
9999
impl<A, B> FusedStream for Either<A, B>
100100
where
101101
A: FusedStream,
102-
B: FusedStream,
102+
B: FusedStream<Item = A::Item>,
103103
{
104104
fn is_terminated(&self) -> bool {
105105
match self {

futures-util/src/future/inspect.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ impl<Fut: Future, F: FnOnce(&Fut::Output)> Inspect<Fut, F> {
2525

2626
impl<Fut: Future + Unpin, F> Unpin for Inspect<Fut, F> {}
2727

28-
impl<Fut: Future + FusedFuture, F> FusedFuture for Inspect<Fut, F> {
28+
impl<Fut, F> FusedFuture for Inspect<Fut, F>
29+
where Fut: FusedFuture,
30+
F: FnOnce(&Fut::Output),
31+
{
2932
fn is_terminated(&self) -> bool { self.future.is_terminated() }
3033
}
3134

futures-util/src/future/lazy.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ pub fn lazy<F, R>(f: F) -> Lazy<F>
3838
Lazy { f: Some(f) }
3939
}
4040

41-
impl<F> FusedFuture for Lazy<F> {
41+
impl<F, R> FusedFuture for Lazy<F>
42+
where F: FnOnce(&mut Context<'_>) -> R,
43+
{
4244
fn is_terminated(&self) -> bool { self.f.is_none() }
4345
}
4446

45-
impl<R, F> Future for Lazy<F>
47+
impl<F, R> Future for Lazy<F>
4648
where F: FnOnce(&mut Context<'_>) -> R,
4749
{
4850
type Output = R;

futures-util/src/future/map.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ impl<Fut, F> Map<Fut, F> {
2323

2424
impl<Fut: Unpin, F> Unpin for Map<Fut, F> {}
2525

26-
impl<Fut, F> FusedFuture for Map<Fut, F> {
26+
impl<Fut, F, T> FusedFuture for Map<Fut, F>
27+
where Fut: Future,
28+
F: FnOnce(Fut::Output) -> T,
29+
{
2730
fn is_terminated(&self) -> bool { self.f.is_none() }
2831
}
2932

futures-util/src/future/shared.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,19 @@ where
173173
}
174174
}
175175

176-
impl<Fut: Future> FusedFuture for Shared<Fut> {
176+
impl<Fut> FusedFuture for Shared<Fut>
177+
where
178+
Fut: Future,
179+
Fut::Output: Clone,
180+
{
177181
fn is_terminated(&self) -> bool {
178182
self.inner.is_none()
179183
}
180184
}
181185

182-
impl<Fut: Future> Future for Shared<Fut>
186+
impl<Fut> Future for Shared<Fut>
183187
where
188+
Fut: Future,
184189
Fut::Output: Clone,
185190
{
186191
type Output = Fut::Output;

futures-util/src/future/then.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ impl<Fut1, Fut2, F> Then<Fut1, Fut2, F>
2525
}
2626
}
2727

28-
impl<Fut1, Fut2, F> FusedFuture for Then<Fut1, Fut2, F> {
28+
impl<Fut1, Fut2, F> FusedFuture for Then<Fut1, Fut2, F>
29+
where Fut1: Future,
30+
Fut2: Future,
31+
F: FnOnce(Fut1::Output) -> Fut2,
32+
{
2933
fn is_terminated(&self) -> bool { self.chain.is_terminated() }
3034
}
3135

futures-util/src/stream/chain.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ where St1: Stream,
2727
}
2828
}
2929

30-
impl<St1, St2: FusedStream> FusedStream for Chain<St1, St2> {
30+
impl<St1, St2> FusedStream for Chain<St1, St2>
31+
where St1: Stream,
32+
St2: FusedStream<Item=St1::Item>,
33+
{
3134
fn is_terminated(&self) -> bool {
3235
self.first.is_none() && self.second.is_terminated()
3336
}

futures-util/src/stream/collect.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ impl<St: Stream, C: Default> Collect<St, C> {
3131
}
3232
}
3333

34-
impl<St: FusedStream, C> FusedFuture for Collect<St, C> {
34+
impl<St, C> FusedFuture for Collect<St, C>
35+
where St: FusedStream,
36+
C: Default + Extend<St:: Item>
37+
{
3538
fn is_terminated(&self) -> bool {
3639
self.stream.is_terminated()
3740
}

0 commit comments

Comments
 (0)