aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Mabin <[email protected]>2023-11-18 15:01:12 +0000
committerScott Mabin <[email protected]>2023-11-18 15:01:12 +0000
commitf482a105b8491f3c21d41cb7e6f52fe6d778258f (patch)
treeafd4e5a46641f7111186eb578b2c56b3f1645ff7
parent7589b5e13e0e01922804e603918fea0aa4dac30a (diff)
more clean up, refactor channel into module to share code
-rw-r--r--embassy-sync/src/channel.rs8
-rw-r--r--embassy-sync/src/channel/priority.rs (renamed from embassy-sync/src/priority_channel.rs)45
-rw-r--r--embassy-sync/src/lib.rs1
3 files changed, 6 insertions, 48 deletions
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;
29use crate::blocking_mutex::Mutex; 29use crate::blocking_mutex::Mutex;
30use crate::waitqueue::WakerRegistration; 30use crate::waitqueue::WakerRegistration;
31 31
32pub mod priority;
33
32/// Send-only access to a [`Channel`]. 34/// Send-only access to a [`Channel`].
33pub struct Sender<'ch, M, T, const N: usize> 35pub struct Sender<'ch, M, T, const N: usize>
34where 36where
@@ -76,7 +78,7 @@ where
76 78
77/// Send-only access to a [`Channel`] without knowing channel size. 79/// Send-only access to a [`Channel`] without knowing channel size.
78pub struct DynamicSender<'ch, T> { 80pub struct DynamicSender<'ch, T> {
79 pub(crate) channel: &'ch dyn DynamicChannel<T>, 81 channel: &'ch dyn DynamicChannel<T>,
80} 82}
81 83
82impl<'ch, T> Clone for DynamicSender<'ch, T> { 84impl<'ch, T> Clone for DynamicSender<'ch, T> {
@@ -176,7 +178,7 @@ where
176 178
177/// Receive-only access to a [`Channel`] without knowing channel size. 179/// Receive-only access to a [`Channel`] without knowing channel size.
178pub struct DynamicReceiver<'ch, T> { 180pub struct DynamicReceiver<'ch, T> {
179 pub(crate) channel: &'ch dyn DynamicChannel<T>, 181 channel: &'ch dyn DynamicChannel<T>,
180} 182}
181 183
182impl<'ch, T> Clone for DynamicReceiver<'ch, T> { 184impl<'ch, T> Clone for DynamicReceiver<'ch, T> {
@@ -321,7 +323,7 @@ impl<'ch, T> Future for DynamicSendFuture<'ch, T> {
321 323
322impl<'ch, T> Unpin for DynamicSendFuture<'ch, T> {} 324impl<'ch, T> Unpin for DynamicSendFuture<'ch, T> {}
323 325
324pub(crate) trait DynamicChannel<T> { 326trait DynamicChannel<T> {
325 fn try_send_with_context(&self, message: T, cx: Option<&mut Context<'_>>) -> Result<(), TrySendError<T>>; 327 fn try_send_with_context(&self, message: T, cx: Option<&mut Context<'_>>) -> Result<(), TrySendError<T>>;
326 328
327 fn try_receive_with_context(&self, cx: Option<&mut Context<'_>>) -> Result<T, TryReceiveError>; 329 fn try_receive_with_context(&self, cx: Option<&mut Context<'_>>) -> Result<T, TryReceiveError>;
diff --git a/embassy-sync/src/priority_channel.rs b/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
183 } 183 }
184} 184}
185 185
186/// Future returned by [`DynamicReceiver::receive`].
187#[must_use = "futures do nothing unless you `.await` or poll them"]
188pub struct DynamicReceiveFuture<'ch, T> {
189 channel: &'ch dyn DynamicChannel<T>,
190}
191
192impl<'ch, T> Future for DynamicReceiveFuture<'ch, T> {
193 type Output = T;
194
195 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {
196 match self.channel.try_receive_with_context(Some(cx)) {
197 Ok(v) => Poll::Ready(v),
198 Err(TryReceiveError::Empty) => Poll::Pending,
199 }
200 }
201}
202
203/// Future returned by [`PriorityChannel::send`] and [`Sender::send`]. 186/// Future returned by [`PriorityChannel::send`] and [`Sender::send`].
204#[must_use = "futures do nothing unless you `.await` or poll them"] 187#[must_use = "futures do nothing unless you `.await` or poll them"]
205pub struct SendFuture<'ch, M, T, K, const N: usize> 188pub struct SendFuture<'ch, M, T, K, const N: usize>
@@ -242,32 +225,6 @@ where
242{ 225{
243} 226}
244 227
245/// Future returned by [`DynamicSender::send`].
246#[must_use = "futures do nothing unless you `.await` or poll them"]
247pub struct DynamicSendFuture<'ch, T> {
248 channel: &'ch dyn DynamicChannel<T>,
249 message: Option<T>,
250}
251
252impl<'ch, T> Future for DynamicSendFuture<'ch, T> {
253 type Output = ();
254
255 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
256 match self.message.take() {
257 Some(m) => match self.channel.try_send_with_context(m, Some(cx)) {
258 Ok(..) => Poll::Ready(()),
259 Err(TrySendError::Full(m)) => {
260 self.message = Some(m);
261 Poll::Pending
262 }
263 },
264 None => panic!("Message cannot be None"),
265 }
266 }
267}
268
269impl<'ch, T> Unpin for DynamicSendFuture<'ch, T> {}
270
271struct ChannelState<T, K, const N: usize> { 228struct ChannelState<T, K, const N: usize> {
272 queue: BinaryHeap<T, K, N>, 229 queue: BinaryHeap<T, K, N>,
273 receiver_waker: WakerRegistration, 230 receiver_waker: WakerRegistration,
@@ -386,7 +343,7 @@ where
386 /// 343 ///
387 /// ``` 344 /// ```
388 /// # use heapless::binary_heap::Max; 345 /// # use heapless::binary_heap::Max;
389 /// use embassy_sync::priority_channel::PriorityChannel; 346 /// use embassy_sync::channel::priority::PriorityChannel;
390 /// use embassy_sync::blocking_mutex::raw::NoopRawMutex; 347 /// use embassy_sync::blocking_mutex::raw::NoopRawMutex;
391 /// 348 ///
392 /// // Declare a bounded channel of 3 u32s. 349 /// // 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;
15pub mod channel; 15pub mod channel;
16pub mod mutex; 16pub mod mutex;
17pub mod pipe; 17pub mod pipe;
18pub mod priority_channel;
19pub mod pubsub; 18pub mod pubsub;
20pub mod signal; 19pub mod signal;
21pub mod waitqueue; 20pub mod waitqueue;