Skip to main content

airio_core/
either.rs

1use std::{
2    pin::Pin,
3    task::{Context, Poll},
4};
5
6use either::Either;
7use futures::{TryFuture, future};
8use pin_project::pin_project;
9
10#[pin_project(project = EitherFutureProj)]
11#[derive(Debug, Copy, Clone)]
12#[must_use = "futures do nothing unless polled"]
13pub enum EitherFuture<A, B> {
14    Left(#[pin] A),
15    Right(#[pin] B),
16}
17
18impl<AFuture, BFuture, AInner, BInner> Future for EitherFuture<AFuture, BFuture>
19where
20    AFuture: TryFuture<Ok = AInner>,
21    BFuture: TryFuture<Ok = BInner>,
22{
23    type Output = Result<future::Either<AInner, BInner>, Either<AFuture::Error, BFuture::Error>>;
24
25    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
26        match self.project() {
27            EitherFutureProj::Left(a) => a
28                .try_poll(cx)
29                .map_ok(future::Either::Left)
30                .map_err(Either::Left),
31            EitherFutureProj::Right(a) => a
32                .try_poll(cx)
33                .map_ok(future::Either::Right)
34                .map_err(Either::Right),
35        }
36    }
37}