use std::future::Future;
use std::pin::Pin;
use std::task::Context;
use std::task::Poll;
#[derive(Debug, Clone)]
pub enum Either<A, B> {
Left(A),
Right(B),
}
#[must_use = "futures do nothing unless you `.await` or poll them"]
#[derive(Debug)]
#[pin_project::pin_project]
pub struct Select<A, B> {
#[pin]
a: A,
#[pin]
b: B,
}
impl<A, B> Future for Select<A, B>
where
A: Future,
B: Future,
{
type Output = Either<A::Output, B::Output>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
if let Poll::Ready(a) = this.a.poll(cx) {
return Poll::Ready(Either::Left(a));
}
if let Poll::Ready(b) = this.b.poll(cx) {
return Poll::Ready(Either::Right(b));
}
Poll::Pending
}
}
pub fn select<A, B>(a: A, b: B) -> Select<A, B>
where
A: Future,
B: Future,
{
Select { a, b }
}