hyper/common/
either.rs

1use pin_project_lite::pin_project;
2use std::{
3    future::Future,
4    pin::Pin,
5    task::{Context, Poll},
6};
7
8pin_project! {
9    /// One of two possible futures that have the same output type.
10    #[project = EitherProj]
11    pub(crate) enum Either<F1, F2> {
12        Left {
13            #[pin]
14            fut: F1
15        },
16        Right {
17            #[pin]
18            fut: F2,
19        },
20    }
21}
22
23impl<F1, F2> Either<F1, F2> {
24    pub(crate) fn left(fut: F1) -> Self {
25        Either::Left { fut }
26    }
27
28    pub(crate) fn right(fut: F2) -> Self {
29        Either::Right { fut }
30    }
31}
32
33impl<F1, F2> Future for Either<F1, F2>
34where
35    F1: Future,
36    F2: Future<Output = F1::Output>,
37{
38    type Output = F1::Output;
39
40    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
41        match self.project() {
42            EitherProj::Left { fut } => fut.poll(cx),
43            EitherProj::Right { fut } => fut.poll(cx),
44        }
45    }
46}