diff options
| author | Oliver Rockstedt <[email protected]> | 2024-05-18 14:01:23 +0200 |
|---|---|---|
| committer | Oliver Rockstedt <[email protected]> | 2024-05-18 14:01:23 +0200 |
| commit | ab899934513d7b7b1eda1727bc145713856e4a9a (patch) | |
| tree | 40d73b2fcd541005edb62f2d3352b77c3ae9103f /embassy-sync/src | |
| parent | f361c2e81cc2d595adc3896f8a8a198973392046 (diff) | |
embassy-sync: Add capacity, free_capacity, len, is_empty and is_full functions to PubSubChannel
Diffstat (limited to 'embassy-sync/src')
| -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] |
