diff options
| author | Oliver Rockstedt <[email protected]> | 2024-05-22 14:54:09 +0200 |
|---|---|---|
| committer | Oliver Rockstedt <[email protected]> | 2024-05-22 14:54:09 +0200 |
| commit | bbeba7f014b383568e999650f0d954a33955ce73 (patch) | |
| tree | df92d995036587cc8f26b5e3525796fc30edbf92 /embassy-sync | |
| parent | 1d4cd85f71058c3574a0a99f7a85572d1d87441c (diff) | |
embassy-sync: Add clear function to all channels
Diffstat (limited to 'embassy-sync')
| -rw-r--r-- | embassy-sync/CHANGELOG.md | 6 | ||||
| -rw-r--r-- | embassy-sync/src/channel.rs | 9 | ||||
| -rw-r--r-- | embassy-sync/src/priority_channel.rs | 9 | ||||
| -rw-r--r-- | embassy-sync/src/pubsub/mod.rs | 16 | ||||
| -rw-r--r-- | embassy-sync/src/pubsub/publisher.rs | 10 | ||||
| -rw-r--r-- | 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 | |||
| 7 | 7 | ||
| 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`, `clear`, `len`, `is_empty` and `is_full` functions to `Channel`. |
| 11 | - Add `capacity`, `free_capacity`, `len`, `is_empty` and `is_full` functions to `PriorityChannel`. | 11 | - Add `capacity`, `free_capacity`, `clear`, `len`, `is_empty` and `is_full` functions to `PriorityChannel`. |
| 12 | - Add `capacity`, `free_capacity`, `len`, `is_empty` and `is_full` functions to `PubSubChannel`. | 12 | - Add `capacity`, `free_capacity`, `clear`, `len`, `is_empty` and `is_full` functions to `PubSubChannel`. |
| 13 | - Made `PubSubBehavior` sealed | 13 | - Made `PubSubBehavior` sealed |
| 14 | - If you called `.publish_immediate(...)` on the queue directly before, then now call `.immediate_publisher().publish_immediate(...)` | 14 | - If you called `.publish_immediate(...)` on the queue directly before, then now call `.immediate_publisher().publish_immediate(...)` |
| 15 | 15 | ||
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<T, const N: usize> ChannelState<T, N> { | |||
| 477 | } | 477 | } |
| 478 | } | 478 | } |
| 479 | 479 | ||
| 480 | fn clear(&mut self) { | ||
| 481 | self.queue.clear(); | ||
| 482 | } | ||
| 483 | |||
| 480 | fn len(&self) -> usize { | 484 | fn len(&self) -> usize { |
| 481 | self.queue.len() | 485 | self.queue.len() |
| 482 | } | 486 | } |
| @@ -632,6 +636,11 @@ where | |||
| 632 | N - self.len() | 636 | N - self.len() |
| 633 | } | 637 | } |
| 634 | 638 | ||
| 639 | /// Clears all elements in the channel. | ||
| 640 | pub fn clear(&self) { | ||
| 641 | self.lock(|c| c.clear()); | ||
| 642 | } | ||
| 643 | |||
| 635 | /// Returns the number of elements currently in the channel. | 644 | /// Returns the number of elements currently in the channel. |
| 636 | pub fn len(&self) -> usize { | 645 | pub fn len(&self) -> usize { |
| 637 | self.lock(|c| c.len()) | 646 | 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 | |||
| 315 | } | 315 | } |
| 316 | } | 316 | } |
| 317 | 317 | ||
| 318 | fn clear(&mut self) { | ||
| 319 | self.queue.clear(); | ||
| 320 | } | ||
| 321 | |||
| 318 | fn len(&self) -> usize { | 322 | fn len(&self) -> usize { |
| 319 | self.queue.len() | 323 | self.queue.len() |
| 320 | } | 324 | } |
| @@ -458,6 +462,11 @@ where | |||
| 458 | N - self.len() | 462 | N - self.len() |
| 459 | } | 463 | } |
| 460 | 464 | ||
| 465 | /// Clears all elements in the channel. | ||
| 466 | pub fn clear(&self) { | ||
| 467 | self.lock(|c| c.clear()); | ||
| 468 | } | ||
| 469 | |||
| 461 | /// Returns the number of elements currently in the channel. | 470 | /// Returns the number of elements currently in the channel. |
| 462 | pub fn len(&self) -> usize { | 471 | pub fn len(&self) -> usize { |
| 463 | self.lock(|c| c.len()) | 472 | 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<M: RawMutex, T: Clone, const CAP: usize, const SUBS: usize, const PUBS: usi | |||
| 173 | CAP - self.len() | 173 | CAP - self.len() |
| 174 | } | 174 | } |
| 175 | 175 | ||
| 176 | /// Clears all elements in the channel. | ||
| 177 | pub fn clear(&self) { | ||
| 178 | self.inner.lock(|inner| inner.borrow_mut().clear()); | ||
| 179 | } | ||
| 180 | |||
| 176 | /// Returns the number of elements currently in the channel. | 181 | /// Returns the number of elements currently in the channel. |
| 177 | pub fn len(&self) -> usize { | 182 | pub fn len(&self) -> usize { |
| 178 | self.inner.lock(|inner| inner.borrow().len()) | 183 | 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 | |||
| 270 | self.free_capacity() | 275 | self.free_capacity() |
| 271 | } | 276 | } |
| 272 | 277 | ||
| 278 | fn clear(&self) { | ||
| 279 | self.clear(); | ||
| 280 | } | ||
| 281 | |||
| 273 | fn len(&self) -> usize { | 282 | fn len(&self) -> usize { |
| 274 | self.len() | 283 | self.len() |
| 275 | } | 284 | } |
| @@ -407,6 +416,10 @@ impl<T: Clone, const CAP: usize, const SUBS: usize, const PUBS: usize> PubSubSta | |||
| 407 | self.publisher_count -= 1; | 416 | self.publisher_count -= 1; |
| 408 | } | 417 | } |
| 409 | 418 | ||
| 419 | fn clear(&mut self) { | ||
| 420 | self.queue.clear(); | ||
| 421 | } | ||
| 422 | |||
| 410 | fn len(&self) -> usize { | 423 | fn len(&self) -> usize { |
| 411 | self.queue.len() | 424 | self.queue.len() |
| 412 | } | 425 | } |
| @@ -460,6 +473,9 @@ trait SealedPubSubBehavior<T> { | |||
| 460 | /// This is equivalent to `capacity() - len()` | 473 | /// This is equivalent to `capacity() - len()` |
| 461 | fn free_capacity(&self) -> usize; | 474 | fn free_capacity(&self) -> usize; |
| 462 | 475 | ||
| 476 | /// Clears all elements in the channel. | ||
| 477 | fn clear(&self); | ||
| 478 | |||
| 463 | /// Returns the number of elements currently in the channel. | 479 | /// Returns the number of elements currently in the channel. |
| 464 | fn len(&self) -> usize; | 480 | fn len(&self) -> usize; |
| 465 | 481 | ||
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<T> + ?Sized, T: Clone> Pub<'a, PSB, T> { | |||
| 55 | self.channel.free_capacity() | 55 | self.channel.free_capacity() |
| 56 | } | 56 | } |
| 57 | 57 | ||
| 58 | /// Clears all elements in the ***channel***. | ||
| 59 | pub fn clear(&self) { | ||
| 60 | self.channel.clear(); | ||
| 61 | } | ||
| 62 | |||
| 58 | /// Returns the number of elements currently in the ***channel***. | 63 | /// Returns the number of elements currently in the ***channel***. |
| 59 | pub fn len(&self) -> usize { | 64 | pub fn len(&self) -> usize { |
| 60 | self.channel.len() | 65 | self.channel.len() |
| @@ -155,6 +160,11 @@ impl<'a, PSB: PubSubBehavior<T> + ?Sized, T: Clone> ImmediatePub<'a, PSB, T> { | |||
| 155 | self.channel.free_capacity() | 160 | self.channel.free_capacity() |
| 156 | } | 161 | } |
| 157 | 162 | ||
| 163 | /// Clears all elements in the ***channel***. | ||
| 164 | pub fn clear(&self) { | ||
| 165 | self.channel.clear(); | ||
| 166 | } | ||
| 167 | |||
| 158 | /// Returns the number of elements currently in the ***channel***. | 168 | /// Returns the number of elements currently in the ***channel***. |
| 159 | pub fn len(&self) -> usize { | 169 | pub fn len(&self) -> usize { |
| 160 | self.channel.len() | 170 | 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<T> + ?Sized, T: Clone> Sub<'a, PSB, T> { | |||
| 83 | self.channel.free_capacity() | 83 | self.channel.free_capacity() |
| 84 | } | 84 | } |
| 85 | 85 | ||
| 86 | /// Clears all elements in the ***channel***. | ||
| 87 | pub fn clear(&self) { | ||
| 88 | self.channel.clear(); | ||
| 89 | } | ||
| 90 | |||
| 86 | /// Returns the number of elements currently in the ***channel***. | 91 | /// Returns the number of elements currently in the ***channel***. |
| 87 | /// See [Self::available] for how many messages are available for this subscriber. | 92 | /// See [Self::available] for how many messages are available for this subscriber. |
| 88 | pub fn len(&self) -> usize { | 93 | pub fn len(&self) -> usize { |
