use std::{pin::Pin, task::{Context, Poll}};
pub struct Select<F1, F2> {
future1: Pin<Box<F1>>,
future2: Pin<Box<F2>>,
}
impl<F1, F2> Select<F1, F2>
where
F1: Future,
F2: Future,
{
pub fn new(future1: F1, future2: F2) -> Self {
Self {
future1: Box::pin(future1),
future2: Box::pin(future2),
}
}
}
#[derive(Debug)]
pub enum Either<A, B> {
Left(A),
Right(B),
}
impl<F1, F2> Future for Select<F1, F2>
where
F1: Future,
F2: Future,
{
type Output = Either<F1::Output, F2::Output>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if let Poll::Ready(output) = self.future1.as_mut().poll(cx) {
return Poll::Ready(Either::Left(output));
}
if let Poll::Ready(output) = self.future2.as_mut().poll(cx) {
return Poll::Ready(Either::Right(output));
}
Poll::Pending
}
}