From b658f10db9a963d85b8465759692b0aa7973a1d1 Mon Sep 17 00:00:00 2001 From: Ruben De Smet Date: Wed, 9 Aug 2023 11:50:26 +0200 Subject: [PATCH] Expose poll_ready_to_{send,receive} in Sender/Receiver --- embassy-sync/src/channel.rs | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/embassy-sync/src/channel.rs b/embassy-sync/src/channel.rs index d6f36f53d..3896f70ac 100644 --- a/embassy-sync/src/channel.rs +++ b/embassy-sync/src/channel.rs @@ -65,6 +65,13 @@ where pub fn try_send(&self, message: T) -> Result<(), TrySendError> { self.channel.try_send(message) } + + /// Allows a poll_fn to poll until the channel is ready to send + /// + /// See [`Channel::poll_ready_to_send()`] + pub fn poll_ready_to_send(&self, cx: &mut Context<'_>) -> bool { + self.channel.poll_ready_to_send(cx) + } } /// Send-only access to a [`Channel`] without knowing channel size. @@ -106,6 +113,13 @@ impl<'ch, T> DynamicSender<'ch, T> { pub fn try_send(&self, message: T) -> Result<(), TrySendError> { self.channel.try_send_with_context(message, None) } + + /// Allows a poll_fn to poll until the channel is ready to send + /// + /// See [`Channel::poll_ready_to_send()`] + pub fn poll_ready_to_send(&self, cx: &mut Context<'_>) -> bool { + self.channel.poll_ready_to_send(cx) + } } /// Receive-only access to a [`Channel`]. @@ -144,6 +158,13 @@ where pub fn try_recv(&self) -> Result { self.channel.try_recv() } + + /// Allows a poll_fn to poll until the channel is ready to receive + /// + /// See [`Channel::poll_ready_to_receive()`] + pub fn poll_ready_to_receive(&self, cx: &mut Context<'_>) -> bool { + self.channel.poll_ready_to_receive(cx) + } } /// Receive-only access to a [`Channel`] without knowing channel size. @@ -173,6 +194,13 @@ impl<'ch, T> DynamicReceiver<'ch, T> { pub fn try_recv(&self) -> Result { self.channel.try_recv_with_context(None) } + + /// Allows a poll_fn to poll until the channel is ready to receive + /// + /// See [`Channel::poll_ready_to_receive()`] + pub fn poll_ready_to_receive(&self, cx: &mut Context<'_>) -> bool { + self.channel.poll_ready_to_receive(cx) + } } impl<'ch, M, T, const N: usize> From> for DynamicReceiver<'ch, T> @@ -286,6 +314,9 @@ trait DynamicChannel { fn try_send_with_context(&self, message: T, cx: Option<&mut Context<'_>>) -> Result<(), TrySendError>; fn try_recv_with_context(&self, cx: Option<&mut Context<'_>>) -> Result; + + fn poll_ready_to_send(&self, cx: &mut Context<'_>) -> bool; + fn poll_ready_to_receive(&self, cx: &mut Context<'_>) -> bool; } /// Error returned by [`try_recv`](Channel::try_recv). @@ -492,6 +523,14 @@ where fn try_recv_with_context(&self, cx: Option<&mut Context<'_>>) -> Result { Channel::try_recv_with_context(self, cx) } + + fn poll_ready_to_send(&self, cx: &mut Context<'_>) -> bool { + Channel::poll_ready_to_send(self, cx) + } + + fn poll_ready_to_receive(&self, cx: &mut Context<'_>) -> bool { + Channel::poll_ready_to_receive(self, cx) + } } #[cfg(test)]