use core::future::Future;
use core::pin::Pin;
use core::task::{Context, Poll};
use futures_util::FutureExt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Either<A, B> {
A(A),
B(B),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Which3<A, B, C> {
A(A),
B(B),
C(C),
}
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct Select2<A: Future, B: Future> {
a: A,
b: B,
}
pub fn select2<A: Future, B: Future>(a: A, b: B) -> Select2<A, B> {
Select2 { a, b }
}
impl<A: Future + Unpin, B: Future + Unpin> Future for Select2<A, B> {
type Output = Either<A::Output, B::Output>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
if let Poll::Ready(v) = this.a.poll_unpin(cx) {
return Poll::Ready(Either::A(v));
}
if let Poll::Ready(v) = this.b.poll_unpin(cx) {
return Poll::Ready(Either::B(v));
}
Poll::Pending
}
}
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct Select3<A: Future, B: Future, C: Future> {
a: A,
b: B,
c: C,
}
pub fn select3<A: Future, B: Future, C: Future>(a: A, b: B, c: C) -> Select3<A, B, C> {
Select3 { a, b, c }
}
impl<A: Future + Unpin, B: Future + Unpin, C: Future + Unpin> Future for Select3<A, B, C> {
type Output = Which3<A::Output, B::Output, C::Output>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
if let Poll::Ready(v) = this.a.poll_unpin(cx) {
return Poll::Ready(Which3::A(v));
}
if let Poll::Ready(v) = this.b.poll_unpin(cx) {
return Poll::Ready(Which3::B(v));
}
if let Poll::Ready(v) = this.c.poll_unpin(cx) {
return Poll::Ready(Which3::C(v));
}
Poll::Pending
}
}