Skip to main content

async_std/future/future/
try_join.rs

1use std::pin::Pin;
2
3use crate::future::MaybeDone;
4use pin_project_lite::pin_project;
5
6use crate::task::{Context, Poll};
7use std::future::Future;
8
9pin_project! {
10    #[allow(missing_docs)]
11    #[allow(missing_debug_implementations)]
12    pub struct TryJoin<L, R>
13    where
14        L: Future,
15        R: Future,
16    {
17        #[pin] left: MaybeDone<L>,
18        #[pin] right: MaybeDone<R>,
19    }
20}
21
22impl<L, R> TryJoin<L, R>
23where
24    L: Future,
25    R: Future,
26{
27    pub(crate) fn new(left: L, right: R) -> Self {
28        Self {
29            left: MaybeDone::new(left),
30            right: MaybeDone::new(right),
31        }
32    }
33}
34
35impl<L, R, A, B, E> Future for TryJoin<L, R>
36where
37    L: Future<Output = Result<A, E>>,
38    R: Future<Output = Result<B, E>>,
39{
40    type Output = Result<(A, B), E>;
41
42    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
43        let this = self.project();
44
45        let mut left = this.left;
46        let mut right = this.right;
47
48        if Future::poll(Pin::new(&mut left), cx).is_ready() {
49            if left.as_ref().output().unwrap().is_err() {
50                return Poll::Ready(Err(left.take().unwrap().err().unwrap()));
51            } else if right.as_ref().output().is_some() {
52                return Poll::Ready(Ok((
53                    left.take().unwrap().ok().unwrap(),
54                    right.take().unwrap().ok().unwrap(),
55                )));
56            }
57        }
58
59        if Future::poll(Pin::new(&mut right), cx).is_ready() {
60            if right.as_ref().output().unwrap().is_err() {
61                return Poll::Ready(Err(right.take().unwrap().err().unwrap()));
62            } else if left.as_ref().output().is_some() {
63                return Poll::Ready(Ok((
64                    left.take().unwrap().ok().unwrap(),
65                    right.take().unwrap().ok().unwrap(),
66                )));
67            }
68        }
69
70        Poll::Pending
71    }
72}