diff options
| author | huntc <[email protected]> | 2021-07-11 10:54:35 +1000 |
|---|---|---|
| committer | huntc <[email protected]> | 2021-07-15 12:31:52 +1000 |
| commit | dcd0c38109ed6711d91c4bdff42825f25e3ee402 (patch) | |
| tree | be45548f7646513d163f53dcd82d8a89fcebbc5b | |
| parent | f159beec1cbd1406f63ca7c3e84a1d598bbadaa1 (diff) | |
Return a new future each time recv is called
| -rw-r--r-- | embassy/src/util/mpsc.rs | 31 |
1 files changed, 12 insertions, 19 deletions
diff --git a/embassy/src/util/mpsc.rs b/embassy/src/util/mpsc.rs index e54c507c1..8f1bba764 100644 --- a/embassy/src/util/mpsc.rs +++ b/embassy/src/util/mpsc.rs | |||
| @@ -141,7 +141,18 @@ where | |||
| 141 | /// | 141 | /// |
| 142 | /// [`close`]: Self::close | 142 | /// [`close`]: Self::close |
| 143 | pub async fn recv(&mut self) -> Option<T> { | 143 | pub async fn recv(&mut self) -> Option<T> { |
| 144 | self.await | 144 | futures::future::poll_fn(|cx| self.recv_poll(cx)).await |
| 145 | } | ||
| 146 | |||
| 147 | fn recv_poll(self: &mut Self, cx: &mut Context<'_>) -> Poll<Option<T>> { | ||
| 148 | match self.try_recv() { | ||
| 149 | Ok(v) => Poll::Ready(Some(v)), | ||
| 150 | Err(TryRecvError::Closed) => Poll::Ready(None), | ||
| 151 | Err(TryRecvError::Empty) => { | ||
| 152 | self.channel.get().set_receiver_waker(cx.waker().clone()); | ||
| 153 | Poll::Pending | ||
| 154 | } | ||
| 155 | } | ||
| 145 | } | 156 | } |
| 146 | 157 | ||
| 147 | /// Attempts to immediately receive a message on this `Receiver` | 158 | /// Attempts to immediately receive a message on this `Receiver` |
| @@ -167,24 +178,6 @@ where | |||
| 167 | } | 178 | } |
| 168 | } | 179 | } |
| 169 | 180 | ||
| 170 | impl<'ch, M, T, const N: usize> Future for Receiver<'ch, M, T, N> | ||
| 171 | where | ||
| 172 | M: Mutex<Data = ()>, | ||
| 173 | { | ||
| 174 | type Output = Option<T>; | ||
| 175 | |||
| 176 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
| 177 | match self.try_recv() { | ||
| 178 | Ok(v) => Poll::Ready(Some(v)), | ||
| 179 | Err(TryRecvError::Closed) => Poll::Ready(None), | ||
| 180 | Err(TryRecvError::Empty) => { | ||
| 181 | self.channel.get().set_receiver_waker(cx.waker().clone()); | ||
| 182 | Poll::Pending | ||
| 183 | } | ||
| 184 | } | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | impl<'ch, M, T, const N: usize> Drop for Receiver<'ch, M, T, N> | 181 | impl<'ch, M, T, const N: usize> Drop for Receiver<'ch, M, T, N> |
| 189 | where | 182 | where |
| 190 | M: Mutex<Data = ()>, | 183 | M: Mutex<Data = ()>, |
