Skip to content

Commit

Permalink
embassy-sync: Add capacity, free_capacity, len, is_empty and is_full …
Browse files Browse the repository at this point in the history
…functions to PriorityChannel
sourcebox committed May 18, 2024
1 parent 3d9b502 commit f361c2e
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions embassy-sync/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ 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`.

## 0.5.0 - 2023-12-04

39 changes: 39 additions & 0 deletions embassy-sync/src/priority_channel.rs
Original file line number Diff line number Diff line change
@@ -314,6 +314,18 @@ where
Poll::Pending
}
}

fn len(&self) -> usize {
self.queue.len()
}

fn is_empty(&self) -> bool {
self.queue.is_empty()
}

fn is_full(&self) -> bool {
self.queue.len() == self.queue.capacity()
}
}

/// A bounded channel for communicating between asynchronous tasks
@@ -433,6 +445,33 @@ where
pub fn try_receive(&self) -> Result<T, TryReceiveError> {
self.lock(|c| c.try_receive())
}

/// Returns the maximum number of elements the channel can hold.
pub const fn capacity(&self) -> usize {
N
}

/// Returns the free capacity of the channel.
///
/// This is equivalent to `capacity() - len()`
pub fn free_capacity(&self) -> usize {
N - self.len()
}

/// Returns the number of elements currently in the channel.
pub fn len(&self) -> usize {
self.lock(|c| c.len())
}

/// Returns whether the channel is empty.
pub fn is_empty(&self) -> bool {
self.lock(|c| c.is_empty())
}

/// Returns whether the channel is full.
pub fn is_full(&self) -> bool {
self.lock(|c| c.is_full())
}
}

/// Implements the DynamicChannel to allow creating types that are unaware of the queue size with the

0 comments on commit f361c2e

Please sign in to comment.