diff options
| author | Scott Mabin <[email protected]> | 2023-11-18 15:01:12 +0000 |
|---|---|---|
| committer | Scott Mabin <[email protected]> | 2023-11-18 15:01:12 +0000 |
| commit | f482a105b8491f3c21d41cb7e6f52fe6d778258f (patch) | |
| tree | afd4e5a46641f7111186eb578b2c56b3f1645ff7 /embassy-sync | |
| parent | 7589b5e13e0e01922804e603918fea0aa4dac30a (diff) | |
more clean up, refactor channel into module to share code
Diffstat (limited to 'embassy-sync')
| -rw-r--r-- | embassy-sync/src/channel.rs | 8 | ||||
| -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.rs | 1 |
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; | |||
| 29 | use crate::blocking_mutex::Mutex; | 29 | use crate::blocking_mutex::Mutex; |
| 30 | use crate::waitqueue::WakerRegistration; | 30 | use crate::waitqueue::WakerRegistration; |
| 31 | 31 | ||
| 32 | pub mod priority; | ||
| 33 | |||
| 32 | /// Send-only access to a [`Channel`]. | 34 | /// Send-only access to a [`Channel`]. |
| 33 | pub struct Sender<'ch, M, T, const N: usize> | 35 | pub struct Sender<'ch, M, T, const N: usize> |
| 34 | where | 36 | where |
| @@ -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. |
| 78 | pub struct DynamicSender<'ch, T> { | 80 | pub struct DynamicSender<'ch, T> { |
| 79 | pub(crate) channel: &'ch dyn DynamicChannel<T>, | 81 | channel: &'ch dyn DynamicChannel<T>, |
| 80 | } | 82 | } |
| 81 | 83 | ||
| 82 | impl<'ch, T> Clone for DynamicSender<'ch, T> { | 84 | impl<'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. |
| 178 | pub struct DynamicReceiver<'ch, T> { | 180 | pub struct DynamicReceiver<'ch, T> { |
| 179 | pub(crate) channel: &'ch dyn DynamicChannel<T>, | 181 | channel: &'ch dyn DynamicChannel<T>, |
| 180 | } | 182 | } |
| 181 | 183 | ||
| 182 | impl<'ch, T> Clone for DynamicReceiver<'ch, T> { | 184 | impl<'ch, T> Clone for DynamicReceiver<'ch, T> { |
| @@ -321,7 +323,7 @@ impl<'ch, T> Future for DynamicSendFuture<'ch, T> { | |||
| 321 | 323 | ||
| 322 | impl<'ch, T> Unpin for DynamicSendFuture<'ch, T> {} | 324 | impl<'ch, T> Unpin for DynamicSendFuture<'ch, T> {} |
| 323 | 325 | ||
| 324 | pub(crate) trait DynamicChannel<T> { | 326 | trait 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"] | ||
| 188 | pub struct DynamicReceiveFuture<'ch, T> { | ||
| 189 | channel: &'ch dyn DynamicChannel<T>, | ||
| 190 | } | ||
| 191 | |||
| 192 | impl<'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"] |
| 205 | pub struct SendFuture<'ch, M, T, K, const N: usize> | 188 | pub 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"] | ||
| 247 | pub struct DynamicSendFuture<'ch, T> { | ||
| 248 | channel: &'ch dyn DynamicChannel<T>, | ||
| 249 | message: Option<T>, | ||
| 250 | } | ||
| 251 | |||
| 252 | impl<'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 | |||
| 269 | impl<'ch, T> Unpin for DynamicSendFuture<'ch, T> {} | ||
| 270 | |||
| 271 | struct ChannelState<T, K, const N: usize> { | 228 | struct 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; | |||
| 15 | pub mod channel; | 15 | pub mod channel; |
| 16 | pub mod mutex; | 16 | pub mod mutex; |
| 17 | pub mod pipe; | 17 | pub mod pipe; |
| 18 | pub mod priority_channel; | ||
| 19 | pub mod pubsub; | 18 | pub mod pubsub; |
| 20 | pub mod signal; | 19 | pub mod signal; |
| 21 | pub mod waitqueue; | 20 | pub mod waitqueue; |
