aboutsummaryrefslogtreecommitdiff
path: root/embassy-sync/src/channel.rs
diff options
context:
space:
mode:
authornerwalt <[email protected]>2024-04-19 12:16:12 -0600
committernerwalt <[email protected]>2024-04-19 12:16:12 -0600
commit30dcc880933a6e37f4b4e3e297d0a4fab586befb (patch)
tree595d9695b6f2d2921f4b45416b50dbf67c07325f /embassy-sync/src/channel.rs
parentda86c086510490602ffdd688760fb59cc7a1e524 (diff)
Adding ready_to_receive to Channel and Receiver
Adding ReceiveReadyFuture
Diffstat (limited to 'embassy-sync/src/channel.rs')
-rw-r--r--embassy-sync/src/channel.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/embassy-sync/src/channel.rs b/embassy-sync/src/channel.rs
index 18be462cb..c4267064c 100644
--- a/embassy-sync/src/channel.rs
+++ b/embassy-sync/src/channel.rs
@@ -152,6 +152,13 @@ where
152 self.channel.receive() 152 self.channel.receive()
153 } 153 }
154 154
155 /// Is a value ready to be received in the channel
156 ///
157 /// See [`Channel::ready_to_receive()`].
158 pub fn ready_to_receive(&self) -> ReceiveReadyFuture<'_, M, T, N> {
159 self.channel.ready_to_receive()
160 }
161
155 /// Attempt to immediately receive the next value. 162 /// Attempt to immediately receive the next value.
156 /// 163 ///
157 /// See [`Channel::try_receive()`] 164 /// See [`Channel::try_receive()`]
@@ -246,6 +253,26 @@ where
246 } 253 }
247} 254}
248 255
256/// Future returned by [`Channel::ready_to_receive`] and [`Receiver::ready_to_receive`].
257#[must_use = "futures do nothing unless you `.await` or poll them"]
258pub struct ReceiveReadyFuture<'ch, M, T, const N: usize>
259where
260 M: RawMutex,
261{
262 channel: &'ch Channel<M, T, N>,
263}
264
265impl<'ch, M, T, const N: usize> Future for ReceiveReadyFuture<'ch, M, T, N>
266where
267 M: RawMutex,
268{
269 type Output = ();
270
271 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
272 self.channel.poll_ready_to_receive(cx)
273 }
274}
275
249/// Future returned by [`DynamicReceiver::receive`]. 276/// Future returned by [`DynamicReceiver::receive`].
250#[must_use = "futures do nothing unless you `.await` or poll them"] 277#[must_use = "futures do nothing unless you `.await` or poll them"]
251pub struct DynamicReceiveFuture<'ch, T> { 278pub struct DynamicReceiveFuture<'ch, T> {
@@ -577,6 +604,14 @@ where
577 ReceiveFuture { channel: self } 604 ReceiveFuture { channel: self }
578 } 605 }
579 606
607 /// Is a value ready to be received in the channel
608 ///
609 /// If there are no messages in the channel's buffer, this method will
610 /// wait until there is at least one
611 pub fn ready_to_receive(&self) -> ReceiveReadyFuture<'_, M, T, N> {
612 ReceiveReadyFuture { channel: self }
613 }
614
580 /// Attempt to immediately receive a message. 615 /// Attempt to immediately receive a message.
581 /// 616 ///
582 /// This method will either receive a message from the channel immediately or return an error 617 /// This method will either receive a message from the channel immediately or return an error