Merge pull request #2987 from sourcebox/sync-additions

embassy-sync: Add clear function to all channels
This commit is contained in:
Ulf Lilleengen 2024-05-22 13:01:16 +00:00 committed by GitHub
commit 4e2296e344
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 52 additions and 3 deletions

View File

@ -7,9 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased
- Add `capacity`, `free_capacity`, `len`, `is_empty` and `is_full` functions to `Channel`.
- Add `capacity`, `free_capacity`, `len`, `is_empty` and `is_full` functions to `PriorityChannel`.
- Add `capacity`, `free_capacity`, `len`, `is_empty` and `is_full` functions to `PubSubChannel`.
- Add `capacity`, `free_capacity`, `clear`, `len`, `is_empty` and `is_full` functions to `Channel`.
- Add `capacity`, `free_capacity`, `clear`, `len`, `is_empty` and `is_full` functions to `PriorityChannel`.
- Add `capacity`, `free_capacity`, `clear`, `len`, `is_empty` and `is_full` functions to `PubSubChannel`.
- Made `PubSubBehavior` sealed
- If you called `.publish_immediate(...)` on the queue directly before, then now call `.immediate_publisher().publish_immediate(...)`

View File

@ -477,6 +477,10 @@ impl<T, const N: usize> ChannelState<T, N> {
}
}
fn clear(&mut self) {
self.queue.clear();
}
fn len(&self) -> usize {
self.queue.len()
}
@ -632,6 +636,11 @@ where
N - self.len()
}
/// Clears all elements in the channel.
pub fn clear(&self) {
self.lock(|c| c.clear());
}
/// Returns the number of elements currently in the channel.
pub fn len(&self) -> usize {
self.lock(|c| c.len())

View File

@ -315,6 +315,10 @@ where
}
}
fn clear(&mut self) {
self.queue.clear();
}
fn len(&self) -> usize {
self.queue.len()
}
@ -458,6 +462,11 @@ where
N - self.len()
}
/// Clears all elements in the channel.
pub fn clear(&self) {
self.lock(|c| c.clear());
}
/// Returns the number of elements currently in the channel.
pub fn len(&self) -> usize {
self.lock(|c| c.len())

View File

@ -173,6 +173,11 @@ impl<M: RawMutex, T: Clone, const CAP: usize, const SUBS: usize, const PUBS: usi
CAP - self.len()
}
/// Clears all elements in the channel.
pub fn clear(&self) {
self.inner.lock(|inner| inner.borrow_mut().clear());
}
/// Returns the number of elements currently in the channel.
pub fn len(&self) -> usize {
self.inner.lock(|inner| inner.borrow().len())
@ -270,6 +275,10 @@ impl<M: RawMutex, T: Clone, const CAP: usize, const SUBS: usize, const PUBS: usi
self.free_capacity()
}
fn clear(&self) {
self.clear();
}
fn len(&self) -> usize {
self.len()
}
@ -407,6 +416,10 @@ impl<T: Clone, const CAP: usize, const SUBS: usize, const PUBS: usize> PubSubSta
self.publisher_count -= 1;
}
fn clear(&mut self) {
self.queue.clear();
}
fn len(&self) -> usize {
self.queue.len()
}
@ -460,6 +473,9 @@ trait SealedPubSubBehavior<T> {
/// This is equivalent to `capacity() - len()`
fn free_capacity(&self) -> usize;
/// Clears all elements in the channel.
fn clear(&self);
/// Returns the number of elements currently in the channel.
fn len(&self) -> usize;

View File

@ -55,6 +55,11 @@ impl<'a, PSB: PubSubBehavior<T> + ?Sized, T: Clone> Pub<'a, PSB, T> {
self.channel.free_capacity()
}
/// Clears all elements in the ***channel***.
pub fn clear(&self) {
self.channel.clear();
}
/// Returns the number of elements currently in the ***channel***.
pub fn len(&self) -> usize {
self.channel.len()
@ -155,6 +160,11 @@ impl<'a, PSB: PubSubBehavior<T> + ?Sized, T: Clone> ImmediatePub<'a, PSB, T> {
self.channel.free_capacity()
}
/// Clears all elements in the ***channel***.
pub fn clear(&self) {
self.channel.clear();
}
/// Returns the number of elements currently in the ***channel***.
pub fn len(&self) -> usize {
self.channel.len()

View File

@ -83,6 +83,11 @@ impl<'a, PSB: PubSubBehavior<T> + ?Sized, T: Clone> Sub<'a, PSB, T> {
self.channel.free_capacity()
}
/// Clears all elements in the ***channel***.
pub fn clear(&self) {
self.channel.clear();
}
/// Returns the number of elements currently in the ***channel***.
/// See [Self::available] for how many messages are available for this subscriber.
pub fn len(&self) -> usize {