Use embassy::util::select3

This commit is contained in:
alexmoon 2022-04-12 18:55:57 -04:00
parent 2915e858ba
commit 1d875fab2d
2 changed files with 1 additions and 52 deletions

View File

@ -15,6 +15,7 @@ mod util;
use driver::Unsupported;
use embassy::blocking_mutex::raw::{NoopRawMutex, RawMutex};
use embassy::channel::Channel;
use embassy::util::{select3, Either3};
use heapless::Vec;
use self::control::*;

View File

@ -6,58 +6,6 @@ use core::task::{Context, Poll};
use embassy::blocking_mutex::raw::RawMutex;
use embassy::channel::Channel;
#[derive(Debug, Clone)]
pub enum Either3<A, B, C> {
First(A),
Second(B),
Third(C),
}
/// Same as [`select`], but with more futures.
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 }
}
/// Future for the [`select3`] function.
#[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
}
}
pub struct Pending<T> {
_phantom: PhantomData<T>,
}