From f482a105b8491f3c21d41cb7e6f52fe6d778258f Mon Sep 17 00:00:00 2001 From: Scott Mabin Date: Sat, 18 Nov 2023 15:01:12 +0000 Subject: [PATCH] more clean up, refactor channel into module to share code --- embassy-sync/src/channel.rs | 8 ++-- .../priority.rs} | 45 +------------------ embassy-sync/src/lib.rs | 1 - 3 files changed, 6 insertions(+), 48 deletions(-) rename embassy-sync/src/{priority_channel.rs => channel/priority.rs} (92%) diff --git a/embassy-sync/src/channel.rs b/embassy-sync/src/channel.rs index ff7129303..1843bbae0 100644 --- a/embassy-sync/src/channel.rs +++ b/embassy-sync/src/channel.rs @@ -29,6 +29,8 @@ use crate::blocking_mutex::raw::RawMutex; use crate::blocking_mutex::Mutex; use crate::waitqueue::WakerRegistration; +pub mod priority; + /// Send-only access to a [`Channel`]. pub struct Sender<'ch, M, T, const N: usize> where @@ -76,7 +78,7 @@ where /// Send-only access to a [`Channel`] without knowing channel size. pub struct DynamicSender<'ch, T> { - pub(crate) channel: &'ch dyn DynamicChannel, + channel: &'ch dyn DynamicChannel, } impl<'ch, T> Clone for DynamicSender<'ch, T> { @@ -176,7 +178,7 @@ where /// Receive-only access to a [`Channel`] without knowing channel size. pub struct DynamicReceiver<'ch, T> { - pub(crate) channel: &'ch dyn DynamicChannel, + channel: &'ch dyn DynamicChannel, } impl<'ch, T> Clone for DynamicReceiver<'ch, T> { @@ -321,7 +323,7 @@ impl<'ch, T> Future for DynamicSendFuture<'ch, T> { impl<'ch, T> Unpin for DynamicSendFuture<'ch, T> {} -pub(crate) trait DynamicChannel { +trait DynamicChannel { fn try_send_with_context(&self, message: T, cx: Option<&mut Context<'_>>) -> Result<(), TrySendError>; fn try_receive_with_context(&self, cx: Option<&mut Context<'_>>) -> Result; diff --git a/embassy-sync/src/priority_channel.rs b/embassy-sync/src/channel/priority.rs similarity index 92% rename from embassy-sync/src/priority_channel.rs rename to embassy-sync/src/channel/priority.rs index 143662814..1fd137db5 100644 --- a/embassy-sync/src/priority_channel.rs +++ b/embassy-sync/src/channel/priority.rs @@ -183,23 +183,6 @@ where } } -/// Future returned by [`DynamicReceiver::receive`]. -#[must_use = "futures do nothing unless you `.await` or poll them"] -pub struct DynamicReceiveFuture<'ch, T> { - channel: &'ch dyn DynamicChannel, -} - -impl<'ch, T> Future for DynamicReceiveFuture<'ch, T> { - type Output = T; - - fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - match self.channel.try_receive_with_context(Some(cx)) { - Ok(v) => Poll::Ready(v), - Err(TryReceiveError::Empty) => Poll::Pending, - } - } -} - /// Future returned by [`PriorityChannel::send`] and [`Sender::send`]. #[must_use = "futures do nothing unless you `.await` or poll them"] pub struct SendFuture<'ch, M, T, K, const N: usize> @@ -242,32 +225,6 @@ where { } -/// Future returned by [`DynamicSender::send`]. -#[must_use = "futures do nothing unless you `.await` or poll them"] -pub struct DynamicSendFuture<'ch, T> { - channel: &'ch dyn DynamicChannel, - message: Option, -} - -impl<'ch, T> Future for DynamicSendFuture<'ch, T> { - type Output = (); - - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - match self.message.take() { - Some(m) => match self.channel.try_send_with_context(m, Some(cx)) { - Ok(..) => Poll::Ready(()), - Err(TrySendError::Full(m)) => { - self.message = Some(m); - Poll::Pending - } - }, - None => panic!("Message cannot be None"), - } - } -} - -impl<'ch, T> Unpin for DynamicSendFuture<'ch, T> {} - struct ChannelState { queue: BinaryHeap, receiver_waker: WakerRegistration, @@ -386,7 +343,7 @@ where /// /// ``` /// # use heapless::binary_heap::Max; - /// use embassy_sync::priority_channel::PriorityChannel; + /// use embassy_sync::channel::priority::PriorityChannel; /// use embassy_sync::blocking_mutex::raw::NoopRawMutex; /// /// // Declare a bounded channel of 3 u32s. diff --git a/embassy-sync/src/lib.rs b/embassy-sync/src/lib.rs index 3ffcb9135..c40fa3b6a 100644 --- a/embassy-sync/src/lib.rs +++ b/embassy-sync/src/lib.rs @@ -15,7 +15,6 @@ pub mod blocking_mutex; pub mod channel; pub mod mutex; pub mod pipe; -pub mod priority_channel; pub mod pubsub; pub mod signal; pub mod waitqueue;