|
1 |
| -use super::chain::Chain; |
2 |
| -use core::fmt; |
| 1 | +use core::fmt::{self, Debug}; |
3 | 2 | use core::pin::Pin;
|
4 | 3 | use futures_core::future::{FusedFuture, Future};
|
5 | 4 | use futures_core::task::{Context, Poll};
|
6 |
| -use pin_utils::unsafe_pinned; |
| 5 | +use pin_project::pin_project; |
7 | 6 |
|
8 |
| -/// Future for the [`flatten`](super::FutureExt::flatten) method. |
9 |
| -#[must_use = "futures do nothing unless you `.await` or poll them"] |
10 |
| -pub struct Flatten<Fut> |
| 7 | +#[pin_project] |
| 8 | +#[derive(Debug)] |
| 9 | +enum InternalFlatten<Fut: Future> { |
| 10 | + First(#[pin] Fut), |
| 11 | + Second(#[pin] Fut::Output), |
| 12 | + Empty, |
| 13 | +} |
| 14 | + |
| 15 | +impl<Fut: Future> InternalFlatten<Fut> { |
| 16 | + fn new(future: Fut) -> Self { |
| 17 | + Self::First(future) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +impl<Fut> FusedFuture for InternalFlatten<Fut> |
11 | 22 | where Fut: Future,
|
| 23 | + Fut::Output: Future, |
12 | 24 | {
|
13 |
| - state: Chain<Fut, Fut::Output, ()>, |
| 25 | + fn is_terminated(&self) -> bool { |
| 26 | + match self { |
| 27 | + Self::Empty => true, |
| 28 | + _ => false, |
| 29 | + } |
| 30 | + } |
14 | 31 | }
|
15 | 32 |
|
16 |
| -impl<Fut> Flatten<Fut> |
| 33 | +impl<Fut> Future for InternalFlatten<Fut> |
17 | 34 | where Fut: Future,
|
18 | 35 | Fut::Output: Future,
|
19 | 36 | {
|
20 |
| - unsafe_pinned!(state: Chain<Fut, Fut::Output, ()>); |
| 37 | + type Output = <Fut::Output as Future>::Output; |
21 | 38 |
|
22 |
| - pub(super) fn new(future: Fut) -> Flatten<Fut> { |
23 |
| - Flatten { |
24 |
| - state: Chain::new(future, ()), |
| 39 | + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 40 | + loop { |
| 41 | + match self.as_mut().project() { |
| 42 | + __InternalFlattenProjection::First(f) => { |
| 43 | + let f = ready!(f.poll(cx)); |
| 44 | + self.set(Self::Second(f)); |
| 45 | + }, |
| 46 | + __InternalFlattenProjection::Second(f) => break f.poll(cx), |
| 47 | + __InternalFlattenProjection::Empty => unreachable!() |
| 48 | + } |
25 | 49 | }
|
26 | 50 | }
|
27 | 51 | }
|
28 | 52 |
|
29 |
| -impl<Fut> fmt::Debug for Flatten<Fut> |
30 |
| - where Fut: Future + fmt::Debug, |
31 |
| - Fut::Output: fmt::Debug, |
32 |
| -{ |
| 53 | +/// Future for the [`flatten`](super::FutureExt::flatten) method. |
| 54 | +#[must_use = "futures do nothing unless you `.await` or poll them"] |
| 55 | +#[pin_project] |
| 56 | +pub struct Flatten<Fut: Future>(#[pin] InternalFlatten<Fut>); |
| 57 | + |
| 58 | +impl<Fut: Debug + Future> Debug for Flatten<Fut> where Fut::Output: Debug { |
33 | 59 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
34 |
| - f.debug_struct("Flatten") |
35 |
| - .field("state", &self.state) |
36 |
| - .finish() |
| 60 | + self.0.fmt(f) |
37 | 61 | }
|
38 | 62 | }
|
39 | 63 |
|
40 |
| -impl<Fut> FusedFuture for Flatten<Fut> |
41 |
| - where Fut: Future, |
42 |
| - Fut::Output: Future, |
43 |
| -{ |
44 |
| - fn is_terminated(&self) -> bool { self.state.is_terminated() } |
| 64 | +impl<Fut: Future> Flatten<Fut> { |
| 65 | + pub(super) fn new(future: Fut) -> Self { |
| 66 | + Self(InternalFlatten::new(future)) |
| 67 | + } |
45 | 68 | }
|
46 | 69 |
|
47 |
| -impl<Fut> Future for Flatten<Fut> |
48 |
| - where Fut: Future, |
49 |
| - Fut::Output: Future, |
50 |
| -{ |
| 70 | +impl<Fut: Future> FusedFuture for Flatten<Fut> where Fut::Output: Future { |
| 71 | + fn is_terminated(&self) -> bool { self.0.is_terminated() } |
| 72 | +} |
| 73 | + |
| 74 | +impl<Fut: Future> Future for Flatten<Fut> where Fut::Output: Future { |
51 | 75 | type Output = <Fut::Output as Future>::Output;
|
52 | 76 |
|
53 | 77 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
54 |
| - self.state().poll(cx, |a, ()| a) |
| 78 | + self.project().0.poll(cx) |
55 | 79 | }
|
56 | 80 | }
|
0 commit comments