diff options
| author | alexmoon <[email protected]> | 2022-04-12 18:55:57 -0400 |
|---|---|---|
| committer | alexmoon <[email protected]> | 2022-04-13 14:55:02 -0400 |
| commit | 1d875fab2dc9d765ebf9f4b37112581301f2ed7e (patch) | |
| tree | abc413b28227ef1cc003e410fe96ee6807e68f0a | |
| parent | 2915e858baac442e71bac4b565746401deed22bd (diff) | |
Use embassy::util::select3
| -rw-r--r-- | embassy-usb/src/lib.rs | 1 | ||||
| -rw-r--r-- | embassy-usb/src/util.rs | 52 |
2 files changed, 1 insertions, 52 deletions
diff --git a/embassy-usb/src/lib.rs b/embassy-usb/src/lib.rs index 11561430b..102ccbb3a 100644 --- a/embassy-usb/src/lib.rs +++ b/embassy-usb/src/lib.rs | |||
| @@ -15,6 +15,7 @@ mod util; | |||
| 15 | use driver::Unsupported; | 15 | use driver::Unsupported; |
| 16 | use embassy::blocking_mutex::raw::{NoopRawMutex, RawMutex}; | 16 | use embassy::blocking_mutex::raw::{NoopRawMutex, RawMutex}; |
| 17 | use embassy::channel::Channel; | 17 | use embassy::channel::Channel; |
| 18 | use embassy::util::{select3, Either3}; | ||
| 18 | use heapless::Vec; | 19 | use heapless::Vec; |
| 19 | 20 | ||
| 20 | use self::control::*; | 21 | use self::control::*; |
diff --git a/embassy-usb/src/util.rs b/embassy-usb/src/util.rs index 4fc55461f..3f20262c6 100644 --- a/embassy-usb/src/util.rs +++ b/embassy-usb/src/util.rs | |||
| @@ -6,58 +6,6 @@ use core::task::{Context, Poll}; | |||
| 6 | use embassy::blocking_mutex::raw::RawMutex; | 6 | use embassy::blocking_mutex::raw::RawMutex; |
| 7 | use embassy::channel::Channel; | 7 | use embassy::channel::Channel; |
| 8 | 8 | ||
| 9 | #[derive(Debug, Clone)] | ||
| 10 | pub enum Either3<A, B, C> { | ||
| 11 | First(A), | ||
| 12 | Second(B), | ||
| 13 | Third(C), | ||
| 14 | } | ||
| 15 | |||
| 16 | /// Same as [`select`], but with more futures. | ||
| 17 | pub fn select3<A, B, C>(a: A, b: B, c: C) -> Select3<A, B, C> | ||
| 18 | where | ||
| 19 | A: Future, | ||
| 20 | B: Future, | ||
| 21 | C: Future, | ||
| 22 | { | ||
| 23 | Select3 { a, b, c } | ||
| 24 | } | ||
| 25 | |||
| 26 | /// Future for the [`select3`] function. | ||
| 27 | #[derive(Debug)] | ||
| 28 | #[must_use = "futures do nothing unless you `.await` or poll them"] | ||
| 29 | pub struct Select3<A, B, C> { | ||
| 30 | a: A, | ||
| 31 | b: B, | ||
| 32 | c: C, | ||
| 33 | } | ||
| 34 | |||
| 35 | impl<A, B, C> Future for Select3<A, B, C> | ||
| 36 | where | ||
| 37 | A: Future, | ||
| 38 | B: Future, | ||
| 39 | C: Future, | ||
| 40 | { | ||
| 41 | type Output = Either3<A::Output, B::Output, C::Output>; | ||
| 42 | |||
| 43 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
| 44 | let this = unsafe { self.get_unchecked_mut() }; | ||
| 45 | let a = unsafe { Pin::new_unchecked(&mut this.a) }; | ||
| 46 | let b = unsafe { Pin::new_unchecked(&mut this.b) }; | ||
| 47 | let c = unsafe { Pin::new_unchecked(&mut this.c) }; | ||
| 48 | if let Poll::Ready(x) = a.poll(cx) { | ||
| 49 | return Poll::Ready(Either3::First(x)); | ||
| 50 | } | ||
| 51 | if let Poll::Ready(x) = b.poll(cx) { | ||
| 52 | return Poll::Ready(Either3::Second(x)); | ||
| 53 | } | ||
| 54 | if let Poll::Ready(x) = c.poll(cx) { | ||
| 55 | return Poll::Ready(Either3::Third(x)); | ||
| 56 | } | ||
| 57 | Poll::Pending | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | pub struct Pending<T> { | 9 | pub struct Pending<T> { |
| 62 | _phantom: PhantomData<T>, | 10 | _phantom: PhantomData<T>, |
| 63 | } | 11 | } |
