aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOliver Rockstedt <[email protected]>2024-04-08 00:39:58 +0200
committerOliver Rockstedt <[email protected]>2024-04-08 00:40:42 +0200
commitfa05256f0563716ffa6e865b73679f4d545bcff9 (patch)
treeadd818437ac9764b2095686adab92e23bb854a1d
parenta4eebdcc6895d6b530e38fbf652bc1a4df00ab0c (diff)
embassy-sync: Add len, is_empty and is_full functions to Channel.
-rw-r--r--embassy-sync/CHANGELOG.md5
-rw-r--r--embassy-sync/src/channel.rs27
2 files changed, 31 insertions, 1 deletions
diff --git a/embassy-sync/CHANGELOG.md b/embassy-sync/CHANGELOG.md
index e7db97ef7..3f6b39d8b 100644
--- a/embassy-sync/CHANGELOG.md
+++ b/embassy-sync/CHANGELOG.md
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
5The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 5The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 6and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7 7
8## Unreleased
9
10- Add `len`, `is_empty` and `is_full` functions to `Channel`.
11
8## 0.5.0 - 2023-12-04 12## 0.5.0 - 2023-12-04
9 13
10- Add a PriorityChannel. 14- Add a PriorityChannel.
@@ -35,7 +39,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
35- Remove unnecessary uses of `atomic-polyfill` 39- Remove unnecessary uses of `atomic-polyfill`
36- Add `#[must_use]` to all futures. 40- Add `#[must_use]` to all futures.
37 41
38
39## 0.1.0 - 2022-08-26 42## 0.1.0 - 2022-08-26
40 43
41- First release 44- First release
diff --git a/embassy-sync/src/channel.rs b/embassy-sync/src/channel.rs
index 48f4dafd6..18be462cb 100644
--- a/embassy-sync/src/channel.rs
+++ b/embassy-sync/src/channel.rs
@@ -449,6 +449,18 @@ impl<T, const N: usize> ChannelState<T, N> {
449 Poll::Pending 449 Poll::Pending
450 } 450 }
451 } 451 }
452
453 fn len(&self) -> usize {
454 self.queue.len()
455 }
456
457 fn is_empty(&self) -> bool {
458 self.queue.is_empty()
459 }
460
461 fn is_full(&self) -> bool {
462 self.queue.is_full()
463 }
452} 464}
453 465
454/// A bounded channel for communicating between asynchronous tasks 466/// A bounded channel for communicating between asynchronous tasks
@@ -572,6 +584,21 @@ where
572 pub fn try_receive(&self) -> Result<T, TryReceiveError> { 584 pub fn try_receive(&self) -> Result<T, TryReceiveError> {
573 self.lock(|c| c.try_receive()) 585 self.lock(|c| c.try_receive())
574 } 586 }
587
588 /// Returns the number of elements currently in the channel.
589 pub fn len(&self) -> usize {
590 self.lock(|c| c.len())
591 }
592
593 /// Returns whether the channel is empty.
594 pub fn is_empty(&self) -> bool {
595 self.lock(|c| c.is_empty())
596 }
597
598 /// Returns whether the channel is full.
599 pub fn is_full(&self) -> bool {
600 self.lock(|c| c.is_full())
601 }
575} 602}
576 603
577/// Implements the DynamicChannel to allow creating types that are unaware of the queue size with the 604/// Implements the DynamicChannel to allow creating types that are unaware of the queue size with the