Merge pull request #2841 from nerwalt/channel-ready-to-receive

Adding ready_to_receive to Channel and Receiver
This commit is contained in:
Ulf Lilleengen 2024-04-22 10:25:02 +00:00 committed by GitHub
commit a9878a243d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -152,6 +152,13 @@ where
self.channel.receive()
}
/// Is a value ready to be received in the channel
///
/// See [`Channel::ready_to_receive()`].
pub fn ready_to_receive(&self) -> ReceiveReadyFuture<'_, M, T, N> {
self.channel.ready_to_receive()
}
/// Attempt to immediately receive the next value.
///
/// See [`Channel::try_receive()`]
@ -246,6 +253,26 @@ where
}
}
/// Future returned by [`Channel::ready_to_receive`] and [`Receiver::ready_to_receive`].
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct ReceiveReadyFuture<'ch, M, T, const N: usize>
where
M: RawMutex,
{
channel: &'ch Channel<M, T, N>,
}
impl<'ch, M, T, const N: usize> Future for ReceiveReadyFuture<'ch, M, T, N>
where
M: RawMutex,
{
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
self.channel.poll_ready_to_receive(cx)
}
}
/// Future returned by [`DynamicReceiver::receive`].
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct DynamicReceiveFuture<'ch, T> {
@ -577,6 +604,14 @@ where
ReceiveFuture { channel: self }
}
/// Is a value ready to be received in the channel
///
/// If there are no messages in the channel's buffer, this method will
/// wait until there is at least one
pub fn ready_to_receive(&self) -> ReceiveReadyFuture<'_, M, T, N> {
ReceiveReadyFuture { channel: self }
}
/// Attempt to immediately receive a message.
///
/// This method will either receive a message from the channel immediately or return an error