Merge pull request #2960 from sourcebox/sync-additions

Consistent functions for Channel, PriorityChannel and PubSubChannel to return capacity and filling
This commit is contained in:
Dario Nieuwenhuis 2024-05-20 10:38:24 +02:00 committed by GitHub
commit c74729bddf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 93 additions and 1 deletions

View File

@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased
- Add `len`, `is_empty` and `is_full` functions to `Channel`.
- 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

@ -620,6 +620,18 @@ where
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())

View File

@ -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

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]