embassy-sync: Add capacity, free_capacity, len, is_empty and is_full functions to PubSubChannel

This commit is contained in:
Oliver Rockstedt 2024-05-18 14:01:23 +02:00
parent f361c2e81c
commit ab89993451
2 changed files with 40 additions and 0 deletions

View File

@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- 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`.
- Add `capacity`, `free_capacity`, `len`, `is_empty` and `is_full` functions to `PubSubChannel`.
## 0.5.0 - 2023-12-04

View File

@ -160,6 +160,33 @@ impl<M: RawMutex, T: Clone, const CAP: usize, const SUBS: usize, const PUBS: usi
pub fn dyn_immediate_publisher(&self) -> DynImmediatePublisher<T> {
DynImmediatePublisher(ImmediatePub::new(self))
}
/// Returns the maximum number of elements the channel can hold.
pub const fn capacity(&self) -> usize {
CAP
}
/// Returns the free capacity of the channel.
///
/// This is equivalent to `capacity() - len()`
pub fn free_capacity(&self) -> usize {
CAP - self.len()
}
/// Returns the number of elements currently in the channel.
pub fn len(&self) -> usize {
self.inner.lock(|inner| inner.borrow().len())
}
/// Returns whether the channel is empty.
pub fn is_empty(&self) -> bool {
self.inner.lock(|inner| inner.borrow().is_empty())
}
/// Returns whether the channel is full.
pub fn is_full(&self) -> bool {
self.inner.lock(|inner| inner.borrow().is_full())
}
}
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
fn unregister_publisher(&mut self) {
self.publisher_count -= 1;
}
fn len(&self) -> usize {
self.queue.len()
}
fn is_empty(&self) -> bool {
self.queue.is_empty()
}
fn is_full(&self) -> bool {
self.queue.is_full()
}
}
/// Error type for the [PubSubChannel]