use core::future::Future;
use core::pin::Pin;
use core::task::{Context, Poll};
#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Either<A, B> {
First(A),
Second(B),
}
pub fn select<A, B>(a: A, b: B) -> Select<A, B>
where
A: Future,
B: Future,
{
Select { a, b }
}
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct Select<A, B> {
a: A,
b: B,
}
impl<A: Unpin, B: Unpin> Unpin for Select<A, 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 = unsafe { self.get_unchecked_mut() };
let a = unsafe { Pin::new_unchecked(&mut this.a) };
let b = unsafe { Pin::new_unchecked(&mut this.b) };
if let Poll::Ready(x) = a.poll(cx) {
return Poll::Ready(Either::First(x));
}
if let Poll::Ready(x) = b.poll(cx) {
return Poll::Ready(Either::Second(x));
}
Poll::Pending
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Either3<A, B, C> {
First(A),
Second(B),
Third(C),
}
pub fn select3<A, B, C>(a: A, b: B, c: C) -> Select3<A, B, C>
where
A: Future,
B: Future,
C: Future,
{
Select3 { a, b, c }
}
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct Select3<A, B, C> {
a: A,
b: B,
c: C,
}
impl<A, B, C> Future for Select3<A, B, C>
where
A: Future,
B: Future,
C: Future,
{
type Output = Either3<A::Output, B::Output, C::Output>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = unsafe { self.get_unchecked_mut() };
let a = unsafe { Pin::new_unchecked(&mut this.a) };
let b = unsafe { Pin::new_unchecked(&mut this.b) };
let c = unsafe { Pin::new_unchecked(&mut this.c) };
if let Poll::Ready(x) = a.poll(cx) {
return Poll::Ready(Either3::First(x));
}
if let Poll::Ready(x) = b.poll(cx) {
return Poll::Ready(Either3::Second(x));
}
if let Poll::Ready(x) = c.poll(cx) {
return Poll::Ready(Either3::Third(x));
}
Poll::Pending
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Either4<A, B, C, D> {
First(A),
Second(B),
Third(C),
Fourth(D),
}
pub fn select4<A, B, C, D>(a: A, b: B, c: C, d: D) -> Select4<A, B, C, D>
where
A: Future,
B: Future,
C: Future,
D: Future,
{
Select4 { a, b, c, d }
}
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct Select4<A, B, C, D> {
a: A,
b: B,
c: C,
d: D,
}
impl<A, B, C, D> Future for Select4<A, B, C, D>
where
A: Future,
B: Future,
C: Future,
D: Future,
{
type Output = Either4<A::Output, B::Output, C::Output, D::Output>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = unsafe { self.get_unchecked_mut() };
let a = unsafe { Pin::new_unchecked(&mut this.a) };
let b = unsafe { Pin::new_unchecked(&mut this.b) };
let c = unsafe { Pin::new_unchecked(&mut this.c) };
let d = unsafe { Pin::new_unchecked(&mut this.d) };
if let Poll::Ready(x) = a.poll(cx) {
return Poll::Ready(Either4::First(x));
}
if let Poll::Ready(x) = b.poll(cx) {
return Poll::Ready(Either4::Second(x));
}
if let Poll::Ready(x) = c.poll(cx) {
return Poll::Ready(Either4::Third(x));
}
if let Poll::Ready(x) = d.poll(cx) {
return Poll::Ready(Either4::Fourth(x));
}
Poll::Pending
}
}
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct SelectArray<Fut, const N: usize> {
inner: [Fut; N],
}
pub fn select_array<Fut: Future, const N: usize>(arr: [Fut; N]) -> SelectArray<Fut, N> {
SelectArray { inner: arr }
}
impl<Fut: Future, const N: usize> Future for SelectArray<Fut, N> {
type Output = (Fut::Output, usize);
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let item = unsafe {
self.get_unchecked_mut()
.inner
.iter_mut()
.enumerate()
.find_map(|(i, f)| match Pin::new_unchecked(f).poll(cx) {
Poll::Pending => None,
Poll::Ready(e) => Some((i, e)),
})
};
match item {
Some((idx, res)) => Poll::Ready((res, idx)),
None => Poll::Pending,
}
}
}
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct SelectSlice<'a, Fut> {
inner: &'a mut [Fut],
}
pub fn select_slice<'a, Fut: Future>(slice: &'a mut [Fut]) -> SelectSlice<'a, Fut> {
SelectSlice { inner: slice }
}
impl<'a, Fut: Future> Future for SelectSlice<'a, Fut> {
type Output = (Fut::Output, usize);
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let item = unsafe {
self.get_unchecked_mut()
.inner
.iter_mut()
.enumerate()
.find_map(|(i, f)| match Pin::new_unchecked(f).poll(cx) {
Poll::Pending => None,
Poll::Ready(e) => Some((i, e)),
})
};
match item {
Some((idx, res)) => Poll::Ready((res, idx)),
None => Poll::Pending,
}
}
}