1 use super::Chain;
2 use core::pin::Pin;
3 use futures_core::future::{FusedFuture, Future};
4 use futures_core::task::{Context, Poll};
5 use pin_utils::unsafe_pinned;
6 
7 /// Future for the [`then`](super::FutureExt::then) method.
8 #[derive(Debug)]
9 #[must_use = "futures do nothing unless you `.await` or poll them"]
10 pub struct Then<Fut1, Fut2, F> {
11     chain: Chain<Fut1, Fut2, F>,
12 }
13 
14 impl<Fut1, Fut2, F> Then<Fut1, Fut2, F>
15     where Fut1: Future,
16           Fut2: Future,
17 {
18     unsafe_pinned!(chain: Chain<Fut1, Fut2, F>);
19 
20     /// Creates a new `Then`.
new(future: Fut1, f: F) -> Then<Fut1, Fut2, F>21     pub(super) fn new(future: Fut1, f: F) -> Then<Fut1, Fut2, F> {
22         Then {
23             chain: Chain::new(future, f),
24         }
25     }
26 }
27 
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 {
is_terminated(&self) -> bool33     fn is_terminated(&self) -> bool { self.chain.is_terminated() }
34 }
35 
36 impl<Fut1, Fut2, F> Future for Then<Fut1, Fut2, F>
37     where Fut1: Future,
38           Fut2: Future,
39           F: FnOnce(Fut1::Output) -> Fut2,
40 {
41     type Output = Fut2::Output;
42 
poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Fut2::Output>43     fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Fut2::Output> {
44         self.as_mut().chain().poll(cx, |output, f| f(output))
45     }
46 }
47