use core::{
future::{Future, poll_fn},
pin::pin,
task::Poll,
};
#[derive(Debug, PartialEq, Eq)]
pub enum Either2<L, R> {
Left(L),
Right(R),
}
pub async fn select2<L: Future, R: Future>(left: L, right: R) -> Either2<L::Output, R::Output> {
let mut left = pin!(left);
let mut right = pin!(right);
poll_fn(|cx| {
if let Poll::Ready(output) = left.as_mut().poll(cx) {
return Poll::Ready(Either2::Left(output));
}
if let Poll::Ready(output) = right.as_mut().poll(cx) {
return Poll::Ready(Either2::Right(output));
}
Poll::Pending
})
.await
}
#[derive(Debug, PartialEq, Eq)]
pub enum Either3<A, B, C> {
First(A),
Second(B),
Third(C),
}
pub async fn select3<A: Future, B: Future, C: Future>(
first: A,
second: B,
third: C,
) -> Either3<A::Output, B::Output, C::Output> {
let mut first = pin!(first);
let mut second = pin!(second);
let mut third = pin!(third);
poll_fn(|cx| {
if let Poll::Ready(output) = first.as_mut().poll(cx) {
return Poll::Ready(Either3::First(output));
}
if let Poll::Ready(output) = second.as_mut().poll(cx) {
return Poll::Ready(Either3::Second(output));
}
if let Poll::Ready(output) = third.as_mut().poll(cx) {
return Poll::Ready(Either3::Third(output));
}
Poll::Pending
})
.await
}
#[cfg(test)]
mod tests {
use crate::{Either2, Either3, select2, select3};
use core::{
future::{Future, pending, ready},
pin::pin,
task::{Context, Poll, Waker},
};
fn poll_once<F: Future>(future: F) -> Poll<F::Output> {
pin!(future).poll(&mut Context::from_waker(Waker::noop()))
}
#[test]
fn test_left() {
assert_eq!(
poll_once(select2(ready(1), pending::<()>())),
Poll::Ready(Either2::Left(1))
);
}
#[test]
fn test_right() {
assert_eq!(
poll_once(select2(pending::<()>(), ready(2))),
Poll::Ready(Either2::Right(2))
);
}
#[test]
fn test_left_wins_when_both_ready() {
assert_eq!(
poll_once(select2(ready(1), ready(2))),
Poll::Ready(Either2::Left(1))
);
}
#[test]
fn test_pending() {
assert_eq!(
poll_once(select2(pending::<()>(), pending::<()>())),
Poll::Pending
);
}
#[test]
fn test3_first() {
assert_eq!(
poll_once(select3(ready(1), pending::<()>(), pending::<()>())),
Poll::Ready(Either3::First(1))
);
}
#[test]
fn test3_second() {
assert_eq!(
poll_once(select3(pending::<()>(), ready(2), pending::<()>())),
Poll::Ready(Either3::Second(2))
);
}
#[test]
fn test3_third() {
assert_eq!(
poll_once(select3(pending::<()>(), pending::<()>(), ready(3))),
Poll::Ready(Either3::Third(3))
);
}
#[test]
fn test3_first_wins_when_all_ready() {
assert_eq!(
poll_once(select3(ready(1), ready(2), ready(3))),
Poll::Ready(Either3::First(1))
);
}
#[test]
fn test3_pending() {
assert_eq!(
poll_once(select3(pending::<()>(), pending::<()>(), pending::<()>())),
Poll::Pending
);
}
}