aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoralexmoon <[email protected]>2022-04-12 18:55:57 -0400
committeralexmoon <[email protected]>2022-04-13 14:55:02 -0400
commit1d875fab2dc9d765ebf9f4b37112581301f2ed7e (patch)
treeabc413b28227ef1cc003e410fe96ee6807e68f0a
parent2915e858baac442e71bac4b565746401deed22bd (diff)
Use embassy::util::select3
-rw-r--r--embassy-usb/src/lib.rs1
-rw-r--r--embassy-usb/src/util.rs52
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;
15use driver::Unsupported; 15use driver::Unsupported;
16use embassy::blocking_mutex::raw::{NoopRawMutex, RawMutex}; 16use embassy::blocking_mutex::raw::{NoopRawMutex, RawMutex};
17use embassy::channel::Channel; 17use embassy::channel::Channel;
18use embassy::util::{select3, Either3};
18use heapless::Vec; 19use heapless::Vec;
19 20
20use self::control::*; 21use 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};
6use embassy::blocking_mutex::raw::RawMutex; 6use embassy::blocking_mutex::raw::RawMutex;
7use embassy::channel::Channel; 7use embassy::channel::Channel;
8 8
9#[derive(Debug, Clone)]
10pub enum Either3<A, B, C> {
11 First(A),
12 Second(B),
13 Third(C),
14}
15
16/// Same as [`select`], but with more futures.
17pub fn select3<A, B, C>(a: A, b: B, c: C) -> Select3<A, B, C>
18where
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"]
29pub struct Select3<A, B, C> {
30 a: A,
31 b: B,
32 c: C,
33}
34
35impl<A, B, C> Future for Select3<A, B, C>
36where
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
61pub struct Pending<T> { 9pub struct Pending<T> {
62 _phantom: PhantomData<T>, 10 _phantom: PhantomData<T>,
63} 11}