|
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, ()), |
25 |
| - } |
| 39 | + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 40 | + Poll::Ready(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) => { |
| 47 | + let output = ready!(f.poll(cx)); |
| 48 | + self.set(Self::Empty); |
| 49 | + break output; |
| 50 | + }, |
| 51 | + __InternalFlattenProjection::Empty => unreachable!() |
| 52 | + } |
| 53 | + }) |
26 | 54 | }
|
27 | 55 | }
|
28 | 56 |
|
29 |
| -impl<Fut> fmt::Debug for Flatten<Fut> |
30 |
| - where Fut: Future + fmt::Debug, |
31 |
| - Fut::Output: fmt::Debug, |
32 |
| -{ |
| 57 | +/// Future for the [`flatten`](super::FutureExt::flatten) method. |
| 58 | +#[must_use = "futures do nothing unless you `.await` or poll them"] |
| 59 | +#[pin_project] |
| 60 | +pub struct Flatten<Fut: Future>(#[pin] InternalFlatten<Fut>); |
| 61 | + |
| 62 | +impl<Fut: Debug + Future> Debug for Flatten<Fut> where Fut::Output: Debug { |
33 | 63 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
34 |
| - f.debug_struct("Flatten") |
35 |
| - .field("state", &self.state) |
36 |
| - .finish() |
| 64 | + self.0.fmt(f) |
37 | 65 | }
|
38 | 66 | }
|
39 | 67 |
|
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() } |
| 68 | +impl<Fut: Future> Flatten<Fut> { |
| 69 | + pub(super) fn new(future: Fut) -> Self { |
| 70 | + Self(InternalFlatten::new(future)) |
| 71 | + } |
45 | 72 | }
|
46 | 73 |
|
47 |
| -impl<Fut> Future for Flatten<Fut> |
48 |
| - where Fut: Future, |
49 |
| - Fut::Output: Future, |
50 |
| -{ |
| 74 | +impl<Fut: Future> FusedFuture for Flatten<Fut> where Fut::Output: Future { |
| 75 | + fn is_terminated(&self) -> bool { self.0.is_terminated() } |
| 76 | +} |
| 77 | + |
| 78 | +impl<Fut: Future> Future for Flatten<Fut> where Fut::Output: Future { |
51 | 79 | type Output = <Fut::Output as Future>::Output;
|
52 | 80 |
|
53 | 81 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
54 |
| - self.state().poll(cx, |a, ()| a) |
| 82 | + self.project().0.poll(cx) |
55 | 83 | }
|
56 | 84 | }
|
0 commit comments