embassy-sync: Add clear function to all channels

This commit is contained in:
Oliver Rockstedt 2024-05-22 14:54:09 +02:00
parent 1d4cd85f71
commit bbeba7f014
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 ## Unreleased
- Add `capacity`, `free_capacity`, `len`, `is_empty` and `is_full` functions to `Channel`. - Add `capacity`, `free_capacity`, `clear`, `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`, `clear`, `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 `PubSubChannel`.
- Made `PubSubBehavior` sealed - Made `PubSubBehavior` sealed
- If you called `.publish_immediate(...)` on the queue directly before, then now call `.immediate_publisher().publish_immediate(...)` - 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 { fn len(&self) -> usize {
self.queue.len() self.queue.len()
} }
@ -632,6 +636,11 @@ where
N - self.len() 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. /// Returns the number of elements currently in the channel.
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.lock(|c| c.len()) self.lock(|c| c.len())

View File

@ -315,6 +315,10 @@ where
} }
} }
fn clear(&mut self) {
self.queue.clear();
}
fn len(&self) -> usize { fn len(&self) -> usize {
self.queue.len() self.queue.len()
} }
@ -458,6 +462,11 @@ where
N - self.len() 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. /// Returns the number of elements currently in the channel.
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.lock(|c| c.len()) 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() 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. /// Returns the number of elements currently in the channel.
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.inner.lock(|inner| inner.borrow().len()) 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() self.free_capacity()
} }
fn clear(&self) {
self.clear();
}
fn len(&self) -> usize { fn len(&self) -> usize {
self.len() self.len()
} }
@ -407,6 +416,10 @@ impl<T: Clone, const CAP: usize, const SUBS: usize, const PUBS: usize> PubSubSta
self.publisher_count -= 1; self.publisher_count -= 1;
} }
fn clear(&mut self) {
self.queue.clear();
}
fn len(&self) -> usize { fn len(&self) -> usize {
self.queue.len() self.queue.len()
} }
@ -460,6 +473,9 @@ trait SealedPubSubBehavior<T> {
/// This is equivalent to `capacity() - len()` /// This is equivalent to `capacity() - len()`
fn free_capacity(&self) -> usize; fn free_capacity(&self) -> usize;
/// Clears all elements in the channel.
fn clear(&self);
/// Returns the number of elements currently in the channel. /// Returns the number of elements currently in the channel.
fn len(&self) -> usize; 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() 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***. /// Returns the number of elements currently in the ***channel***.
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.channel.len() self.channel.len()
@ -155,6 +160,11 @@ impl<'a, PSB: PubSubBehavior<T> + ?Sized, T: Clone> ImmediatePub<'a, PSB, T> {
self.channel.free_capacity() 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***. /// Returns the number of elements currently in the ***channel***.
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.channel.len() 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() 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***. /// Returns the number of elements currently in the ***channel***.
/// See [Self::available] for how many messages are available for this subscriber. /// See [Self::available] for how many messages are available for this subscriber.
pub fn len(&self) -> usize { pub fn len(&self) -> usize {