From bbeba7f014b383568e999650f0d954a33955ce73 Mon Sep 17 00:00:00 2001 From: Oliver Rockstedt Date: Wed, 22 May 2024 14:54:09 +0200 Subject: [PATCH] embassy-sync: Add clear function to all channels --- embassy-sync/CHANGELOG.md | 6 +++--- embassy-sync/src/channel.rs | 9 +++++++++ embassy-sync/src/priority_channel.rs | 9 +++++++++ embassy-sync/src/pubsub/mod.rs | 16 ++++++++++++++++ embassy-sync/src/pubsub/publisher.rs | 10 ++++++++++ embassy-sync/src/pubsub/subscriber.rs | 5 +++++ 6 files changed, 52 insertions(+), 3 deletions(-) diff --git a/embassy-sync/CHANGELOG.md b/embassy-sync/CHANGELOG.md index bb919b28a..e5c453ce2 100644 --- a/embassy-sync/CHANGELOG.md +++ b/embassy-sync/CHANGELOG.md @@ -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(...)` diff --git a/embassy-sync/src/channel.rs b/embassy-sync/src/channel.rs index b124a885f..55ac5fb66 100644 --- a/embassy-sync/src/channel.rs +++ b/embassy-sync/src/channel.rs @@ -477,6 +477,10 @@ impl ChannelState { } } + 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()) diff --git a/embassy-sync/src/priority_channel.rs b/embassy-sync/src/priority_channel.rs index 2954d1b76..24c6c5a7f 100644 --- a/embassy-sync/src/priority_channel.rs +++ b/embassy-sync/src/priority_channel.rs @@ -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()) diff --git a/embassy-sync/src/pubsub/mod.rs b/embassy-sync/src/pubsub/mod.rs index 637336d9d..66c9b0017 100644 --- a/embassy-sync/src/pubsub/mod.rs +++ b/embassy-sync/src/pubsub/mod.rs @@ -173,6 +173,11 @@ impl usize { self.inner.lock(|inner| inner.borrow().len()) @@ -270,6 +275,10 @@ impl usize { self.len() } @@ -407,6 +416,10 @@ impl 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 { /// 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; diff --git a/embassy-sync/src/pubsub/publisher.rs b/embassy-sync/src/pubsub/publisher.rs index 26e2c63b7..e66b3b1db 100644 --- a/embassy-sync/src/pubsub/publisher.rs +++ b/embassy-sync/src/pubsub/publisher.rs @@ -55,6 +55,11 @@ impl<'a, PSB: PubSubBehavior + ?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 + ?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() diff --git a/embassy-sync/src/pubsub/subscriber.rs b/embassy-sync/src/pubsub/subscriber.rs index 2e2bd26a9..6ad660cb3 100644 --- a/embassy-sync/src/pubsub/subscriber.rs +++ b/embassy-sync/src/pubsub/subscriber.rs @@ -83,6 +83,11 @@ impl<'a, PSB: PubSubBehavior + ?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 {