diff options
| author | Dario Nieuwenhuis <[email protected]> | 2024-05-20 10:38:24 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-05-20 10:38:24 +0200 |
| commit | c74729bddf5d3eb06d201135cb7130cbf960dc9b (patch) | |
| tree | 49c3925bba0763fd71fb8dae1bb7f7fa157b1d0a /embassy-sync/src/pubsub/mod.rs | |
| parent | 729f143269a70da426740e141b50016951000059 (diff) | |
| parent | ab899934513d7b7b1eda1727bc145713856e4a9a (diff) | |
Merge pull request #2960 from sourcebox/sync-additions
Consistent functions for Channel, PriorityChannel and PubSubChannel to return capacity and filling
Diffstat (limited to 'embassy-sync/src/pubsub/mod.rs')
| -rw-r--r-- | embassy-sync/src/pubsub/mod.rs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/embassy-sync/src/pubsub/mod.rs b/embassy-sync/src/pubsub/mod.rs index 6afd54af5..754747ab8 100644 --- a/embassy-sync/src/pubsub/mod.rs +++ b/embassy-sync/src/pubsub/mod.rs | |||
| @@ -160,6 +160,33 @@ impl<M: RawMutex, T: Clone, const CAP: usize, const SUBS: usize, const PUBS: usi | |||
| 160 | pub fn dyn_immediate_publisher(&self) -> DynImmediatePublisher<T> { | 160 | pub fn dyn_immediate_publisher(&self) -> DynImmediatePublisher<T> { |
| 161 | DynImmediatePublisher(ImmediatePub::new(self)) | 161 | DynImmediatePublisher(ImmediatePub::new(self)) |
| 162 | } | 162 | } |
| 163 | |||
| 164 | /// Returns the maximum number of elements the channel can hold. | ||
| 165 | pub const fn capacity(&self) -> usize { | ||
| 166 | CAP | ||
| 167 | } | ||
| 168 | |||
| 169 | /// Returns the free capacity of the channel. | ||
| 170 | /// | ||
| 171 | /// This is equivalent to `capacity() - len()` | ||
| 172 | pub fn free_capacity(&self) -> usize { | ||
| 173 | CAP - self.len() | ||
| 174 | } | ||
| 175 | |||
| 176 | /// Returns the number of elements currently in the channel. | ||
| 177 | pub fn len(&self) -> usize { | ||
| 178 | self.inner.lock(|inner| inner.borrow().len()) | ||
| 179 | } | ||
| 180 | |||
| 181 | /// Returns whether the channel is empty. | ||
| 182 | pub fn is_empty(&self) -> bool { | ||
| 183 | self.inner.lock(|inner| inner.borrow().is_empty()) | ||
| 184 | } | ||
| 185 | |||
| 186 | /// Returns whether the channel is full. | ||
| 187 | pub fn is_full(&self) -> bool { | ||
| 188 | self.inner.lock(|inner| inner.borrow().is_full()) | ||
| 189 | } | ||
| 163 | } | 190 | } |
| 164 | 191 | ||
| 165 | impl<M: RawMutex, T: Clone, const CAP: usize, const SUBS: usize, const PUBS: usize> PubSubBehavior<T> | 192 | impl<M: RawMutex, T: Clone, const CAP: usize, const SUBS: usize, const PUBS: usize> PubSubBehavior<T> |
| @@ -366,6 +393,18 @@ impl<T: Clone, const CAP: usize, const SUBS: usize, const PUBS: usize> PubSubSta | |||
| 366 | fn unregister_publisher(&mut self) { | 393 | fn unregister_publisher(&mut self) { |
| 367 | self.publisher_count -= 1; | 394 | self.publisher_count -= 1; |
| 368 | } | 395 | } |
| 396 | |||
| 397 | fn len(&self) -> usize { | ||
| 398 | self.queue.len() | ||
| 399 | } | ||
| 400 | |||
| 401 | fn is_empty(&self) -> bool { | ||
| 402 | self.queue.is_empty() | ||
| 403 | } | ||
| 404 | |||
| 405 | fn is_full(&self) -> bool { | ||
| 406 | self.queue.is_full() | ||
| 407 | } | ||
| 369 | } | 408 | } |
| 370 | 409 | ||
| 371 | /// Error type for the [PubSubChannel] | 410 | /// Error type for the [PubSubChannel] |
