1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//!Future related utilities
use core::task;
use core::future::Future;
use core::pin::Pin;
use core::marker::Unpin;

///Either variant.
pub enum Either<A, B> {
    ///Left
    Left(A),
    ///Right
    Right(B),
}

impl<A: Unpin, B: Unpin> Unpin for Either<A, B> {}

impl<A: Unpin, B: Unpin> Future for Either<A, B> where A: Future, B: Future<Output = A::Output> {
    type Output = A::Output;

    fn poll(mut self: Pin<&mut Self>, ctx: &mut task::Context<'_>) -> task::Poll<Self::Output> {
        match *self {
            Either::Left(ref mut left) => Future::poll(Pin::new(left), ctx),
            Either::Right(ref mut right) => Future::poll(Pin::new(right), ctx),
        }
    }
}

///Create pair of futures that being processed together.
///
///First goes left, then  right.
pub struct Pair<A: Unpin, B: Unpin> {
    inner: Option<(A, B)>,
}

impl<A: Unpin, B: Unpin> Pair<A, B> {
    ///Creates new instance
    pub fn new(left: A, right: B) -> Self {
        Self {
            inner: Some((left, right)),
        }
    }
}

impl<A: Unpin, B: Unpin> Future for Pair<A, B> where A: Future, B: Future {
    type Output = Either<(A::Output, B), (B::Output, A)>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> task::Poll<Self::Output> {
        let (ref mut left, ref mut right) = match self.inner.as_mut() {
            Some(value) => value,
            None => unreach!()
        };

        match Pin::new(left).poll(cx) {
            task::Poll::Ready(res) => {
                let (_, right) = self.inner.take().unwrap();
                task::Poll::Ready(Either::Left((res, right)))
            },
            task::Poll::Pending => match Pin::new(right).poll(cx) {
                task::Poll::Ready(res) => {
                    let (left, _) = self.inner.take().unwrap();
                    task::Poll::Ready(Either::Right((res, left)))
                },
                task::Poll::Pending => task::Poll::Pending,
            }
        }
    }
}