aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOliver Rockstedt <[email protected]>2024-05-18 13:48:40 +0200
committerOliver Rockstedt <[email protected]>2024-05-18 13:48:40 +0200
commitf361c2e81cc2d595adc3896f8a8a198973392046 (patch)
treee59ece88ad3a43fe896a492c1880e9d2233ad44b
parent3d9b502c7af1f1dab92ee4a3aa07dc667a3bf103 (diff)
embassy-sync: Add capacity, free_capacity, len, is_empty and is_full functions to PriorityChannel
-rw-r--r--embassy-sync/CHANGELOG.md1
-rw-r--r--embassy-sync/src/priority_channel.rs39
2 files changed, 40 insertions, 0 deletions
diff --git a/embassy-sync/CHANGELOG.md b/embassy-sync/CHANGELOG.md
index 46466d610..28d97e68d 100644
--- a/embassy-sync/CHANGELOG.md
+++ b/embassy-sync/CHANGELOG.md
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
8## Unreleased 8## Unreleased
9 9
10- Add `capacity`, `free_capacity`, `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`.
11 12
12## 0.5.0 - 2023-12-04 13## 0.5.0 - 2023-12-04
13 14
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