diff options
| author | Oliver Rockstedt <[email protected]> | 2024-05-18 13:48:40 +0200 |
|---|---|---|
| committer | Oliver Rockstedt <[email protected]> | 2024-05-18 13:48:40 +0200 |
| commit | f361c2e81cc2d595adc3896f8a8a198973392046 (patch) | |
| tree | e59ece88ad3a43fe896a492c1880e9d2233ad44b /embassy-sync/src | |
| parent | 3d9b502c7af1f1dab92ee4a3aa07dc667a3bf103 (diff) | |
embassy-sync: Add capacity, free_capacity, len, is_empty and is_full functions to PriorityChannel
Diffstat (limited to 'embassy-sync/src')
| -rw-r--r-- | embassy-sync/src/priority_channel.rs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/embassy-sync/src/priority_channel.rs b/embassy-sync/src/priority_channel.rs index e77678c24..4b9bd0515 100644 --- a/embassy-sync/src/priority_channel.rs +++ b/embassy-sync/src/priority_channel.rs | |||
| @@ -314,6 +314,18 @@ where | |||
| 314 | Poll::Pending | 314 | Poll::Pending |
| 315 | } | 315 | } |
| 316 | } | 316 | } |
| 317 | |||
| 318 | fn len(&self) -> usize { | ||
| 319 | self.queue.len() | ||
| 320 | } | ||
| 321 | |||
| 322 | fn is_empty(&self) -> bool { | ||
| 323 | self.queue.is_empty() | ||
| 324 | } | ||
| 325 | |||
| 326 | fn is_full(&self) -> bool { | ||
| 327 | self.queue.len() == self.queue.capacity() | ||
| 328 | } | ||
| 317 | } | 329 | } |
| 318 | 330 | ||
| 319 | /// A bounded channel for communicating between asynchronous tasks | 331 | /// A bounded channel for communicating between asynchronous tasks |
| @@ -433,6 +445,33 @@ where | |||
| 433 | pub fn try_receive(&self) -> Result<T, TryReceiveError> { | 445 | pub fn try_receive(&self) -> Result<T, TryReceiveError> { |
| 434 | self.lock(|c| c.try_receive()) | 446 | self.lock(|c| c.try_receive()) |
| 435 | } | 447 | } |
| 448 | |||
| 449 | /// Returns the maximum number of elements the channel can hold. | ||
| 450 | pub const fn capacity(&self) -> usize { | ||
| 451 | N | ||
| 452 | } | ||
| 453 | |||
| 454 | /// Returns the free capacity of the channel. | ||
| 455 | /// | ||
| 456 | /// This is equivalent to `capacity() - len()` | ||
| 457 | pub fn free_capacity(&self) -> usize { | ||
| 458 | N - self.len() | ||
| 459 | } | ||
| 460 | |||
| 461 | /// Returns the number of elements currently in the channel. | ||
| 462 | pub fn len(&self) -> usize { | ||
| 463 | self.lock(|c| c.len()) | ||
| 464 | } | ||
| 465 | |||
| 466 | /// Returns whether the channel is empty. | ||
| 467 | pub fn is_empty(&self) -> bool { | ||
| 468 | self.lock(|c| c.is_empty()) | ||
| 469 | } | ||
| 470 | |||
| 471 | /// Returns whether the channel is full. | ||
| 472 | pub fn is_full(&self) -> bool { | ||
| 473 | self.lock(|c| c.is_full()) | ||
| 474 | } | ||
| 436 | } | 475 | } |
| 437 | 476 | ||
| 438 | /// Implements the DynamicChannel to allow creating types that are unaware of the queue size with the | 477 | /// Implements the DynamicChannel to allow creating types that are unaware of the queue size with the |
