diff options
| author | Dario Nieuwenhuis <[email protected]> | 2024-05-20 10:38:24 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-05-20 10:38:24 +0200 |
| commit | c74729bddf5d3eb06d201135cb7130cbf960dc9b (patch) | |
| tree | 49c3925bba0763fd71fb8dae1bb7f7fa157b1d0a | |
| parent | 729f143269a70da426740e141b50016951000059 (diff) | |
| parent | ab899934513d7b7b1eda1727bc145713856e4a9a (diff) | |
Merge pull request #2960 from sourcebox/sync-additions
Consistent functions for Channel, PriorityChannel and PubSubChannel to return capacity and filling
| -rw-r--r-- | embassy-sync/CHANGELOG.md | 4 | ||||
| -rw-r--r-- | embassy-sync/src/channel.rs | 12 | ||||
| -rw-r--r-- | embassy-sync/src/priority_channel.rs | 39 | ||||
| -rw-r--r-- | embassy-sync/src/pubsub/mod.rs | 39 |
4 files changed, 93 insertions, 1 deletions
diff --git a/embassy-sync/CHANGELOG.md b/embassy-sync/CHANGELOG.md index 3f6b39d8b..7a830a853 100644 --- a/embassy-sync/CHANGELOG.md +++ b/embassy-sync/CHANGELOG.md | |||
| @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 | |||
| 7 | 7 | ||
| 8 | ## Unreleased | 8 | ## Unreleased |
| 9 | 9 | ||
| 10 | - Add `len`, `is_empty` and `is_full` functions to `Channel`. | 10 | - Add `capacity`, `free_capacity`, `len`, `is_empty` and `is_full` functions to `Channel`. |
| 11 | - Add `capacity`, `free_capacity`, `len`, `is_empty` and `is_full` functions to `PriorityChannel`. | ||
| 12 | - Add `capacity`, `free_capacity`, `len`, `is_empty` and `is_full` functions to `PubSubChannel`. | ||
| 11 | 13 | ||
| 12 | ## 0.5.0 - 2023-12-04 | 14 | ## 0.5.0 - 2023-12-04 |
| 13 | 15 | ||
diff --git a/embassy-sync/src/channel.rs b/embassy-sync/src/channel.rs index c4267064c..f9f71d026 100644 --- a/embassy-sync/src/channel.rs +++ b/embassy-sync/src/channel.rs | |||
| @@ -620,6 +620,18 @@ where | |||
| 620 | self.lock(|c| c.try_receive()) | 620 | self.lock(|c| c.try_receive()) |
| 621 | } | 621 | } |
| 622 | 622 | ||
| 623 | /// Returns the maximum number of elements the channel can hold. | ||
| 624 | pub const fn capacity(&self) -> usize { | ||
| 625 | N | ||
| 626 | } | ||
| 627 | |||
| 628 | /// Returns the free capacity of the channel. | ||
| 629 | /// | ||
| 630 | /// This is equivalent to `capacity() - len()` | ||
| 631 | pub fn free_capacity(&self) -> usize { | ||
| 632 | N - self.len() | ||
| 633 | } | ||
| 634 | |||
| 623 | /// Returns the number of elements currently in the channel. | 635 | /// Returns the number of elements currently in the channel. |
| 624 | pub fn len(&self) -> usize { | 636 | pub fn len(&self) -> usize { |
| 625 | self.lock(|c| c.len()) | 637 | self.lock(|c| c.len()) |
diff --git a/embassy-sync/src/priority_channel.rs b/embassy-sync/src/priority_channel.rs index e77678c24..4b9bd0515 100644 --- a/embassy-sync/src/priority_channel.rs +++ b/embassy-sync/src/priority_channel.rs | |||
| @@ -314,6 +314,18 @@ where | |||
| 314 | Poll::Pending | 314 | Poll::Pending |
| 315 | } | 315 | } |
| 316 | } | 316 | } |
| 317 | |||
| 318 | fn len(&self) -> usize { | ||
| 319 | self.queue.len() | ||
| 320 | } | ||
| 321 | |||
| 322 | fn is_empty(&self) -> bool { | ||
| 323 | self.queue.is_empty() | ||
| 324 | } | ||
| 325 | |||
| 326 | fn is_full(&self) -> bool { | ||
| 327 | self.queue.len() == self.queue.capacity() | ||
| 328 | } | ||
| 317 | } | 329 | } |
| 318 | 330 | ||
| 319 | /// A bounded channel for communicating between asynchronous tasks | 331 | /// A bounded channel for communicating between asynchronous tasks |
| @@ -433,6 +445,33 @@ where | |||
| 433 | pub fn try_receive(&self) -> Result<T, TryReceiveError> { | 445 | pub fn try_receive(&self) -> Result<T, TryReceiveError> { |
| 434 | self.lock(|c| c.try_receive()) | 446 | self.lock(|c| c.try_receive()) |
| 435 | } | 447 | } |
| 448 | |||
| 449 | /// Returns the maximum number of elements the channel can hold. | ||
| 450 | pub const fn capacity(&self) -> usize { | ||
| 451 | N | ||
| 452 | } | ||
| 453 | |||
| 454 | /// Returns the free capacity of the channel. | ||
| 455 | /// | ||
| 456 | /// This is equivalent to `capacity() - len()` | ||
| 457 | pub fn free_capacity(&self) -> usize { | ||
| 458 | N - self.len() | ||
| 459 | } | ||
| 460 | |||
| 461 | /// Returns the number of elements currently in the channel. | ||
| 462 | pub fn len(&self) -> usize { | ||
| 463 | self.lock(|c| c.len()) | ||
| 464 | } | ||
| 465 | |||
| 466 | /// Returns whether the channel is empty. | ||
| 467 | pub fn is_empty(&self) -> bool { | ||
| 468 | self.lock(|c| c.is_empty()) | ||
| 469 | } | ||
| 470 | |||
| 471 | /// Returns whether the channel is full. | ||
| 472 | pub fn is_full(&self) -> bool { | ||
| 473 | self.lock(|c| c.is_full()) | ||
| 474 | } | ||
| 436 | } | 475 | } |
| 437 | 476 | ||
| 438 | /// Implements the DynamicChannel to allow creating types that are unaware of the queue size with the | 477 | /// Implements the DynamicChannel to allow creating types that are unaware of the queue size with the |
diff --git a/embassy-sync/src/pubsub/mod.rs b/embassy-sync/src/pubsub/mod.rs index 6afd54af5..754747ab8 100644 --- a/embassy-sync/src/pubsub/mod.rs +++ b/embassy-sync/src/pubsub/mod.rs | |||
| @@ -160,6 +160,33 @@ impl<M: RawMutex, T: Clone, const CAP: usize, const SUBS: usize, const PUBS: usi | |||
| 160 | pub fn dyn_immediate_publisher(&self) -> DynImmediatePublisher<T> { | 160 | pub fn dyn_immediate_publisher(&self) -> DynImmediatePublisher<T> { |
| 161 | DynImmediatePublisher(ImmediatePub::new(self)) | 161 | DynImmediatePublisher(ImmediatePub::new(self)) |
| 162 | } | 162 | } |
| 163 | |||
| 164 | /// Returns the maximum number of elements the channel can hold. | ||
| 165 | pub const fn capacity(&self) -> usize { | ||
| 166 | CAP | ||
| 167 | } | ||
| 168 | |||
| 169 | /// Returns the free capacity of the channel. | ||
| 170 | /// | ||
| 171 | /// This is equivalent to `capacity() - len()` | ||
| 172 | pub fn free_capacity(&self) -> usize { | ||
| 173 | CAP - self.len() | ||
| 174 | } | ||
| 175 | |||
| 176 | /// Returns the number of elements currently in the channel. | ||
| 177 | pub fn len(&self) -> usize { | ||
| 178 | self.inner.lock(|inner| inner.borrow().len()) | ||
| 179 | } | ||
| 180 | |||
| 181 | /// Returns whether the channel is empty. | ||
| 182 | pub fn is_empty(&self) -> bool { | ||
| 183 | self.inner.lock(|inner| inner.borrow().is_empty()) | ||
| 184 | } | ||
| 185 | |||
| 186 | /// Returns whether the channel is full. | ||
| 187 | pub fn is_full(&self) -> bool { | ||
| 188 | self.inner.lock(|inner| inner.borrow().is_full()) | ||
| 189 | } | ||
| 163 | } | 190 | } |
| 164 | 191 | ||
| 165 | impl<M: RawMutex, T: Clone, const CAP: usize, const SUBS: usize, const PUBS: usize> PubSubBehavior<T> | 192 | impl<M: RawMutex, T: Clone, const CAP: usize, const SUBS: usize, const PUBS: usize> PubSubBehavior<T> |
| @@ -366,6 +393,18 @@ impl<T: Clone, const CAP: usize, const SUBS: usize, const PUBS: usize> PubSubSta | |||
| 366 | fn unregister_publisher(&mut self) { | 393 | fn unregister_publisher(&mut self) { |
| 367 | self.publisher_count -= 1; | 394 | self.publisher_count -= 1; |
| 368 | } | 395 | } |
| 396 | |||
| 397 | fn len(&self) -> usize { | ||
| 398 | self.queue.len() | ||
| 399 | } | ||
| 400 | |||
| 401 | fn is_empty(&self) -> bool { | ||
| 402 | self.queue.is_empty() | ||
| 403 | } | ||
| 404 | |||
| 405 | fn is_full(&self) -> bool { | ||
| 406 | self.queue.is_full() | ||
| 407 | } | ||
| 369 | } | 408 | } |
| 370 | 409 | ||
| 371 | /// Error type for the [PubSubChannel] | 410 | /// Error type for the [PubSubChannel] |
