Merge pull request #3401 from sourcebox/sync-additions

Add `capacity`, `free_capacity`, `clear`, `len`, `is_empty` and `is_full` functions to `priority_channel::{Sender, Receiver}`
This commit is contained in:
Dario Nieuwenhuis 2024-10-07 15:42:48 +00:00 committed by GitHub
commit 42815e944a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 86 additions and 1 deletions

View File

@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add LazyLock sync primitive.
- Add `clear`, `len`, `is_empty` and `is_full` functions to `zerocopy_channel`.
- Add `capacity`, `free_capacity`, `clear`, `len`, `is_empty` and `is_full` functions to `channel::{Sender, Receiver}`.
- Add `capacity`, `free_capacity`, `clear`, `len`, `is_empty` and `is_full` functions to `priority_channel::{Sender, Receiver}`.
## 0.6.0 - 2024-05-29

View File

@ -5,7 +5,7 @@ An [Embassy](https://embassy.dev) project.
Synchronization primitives and data structures with async support:
- [`Channel`](channel::Channel) - A Multiple Producer Multiple Consumer (MPMC) channel. Each message is only received by a single consumer.
- [`PriorityChannel`](channel::priority_channel::PriorityChannel) - A Multiple Producer Multiple Consumer (MPMC) channel. Each message is only received by a single consumer. Higher priority items are shifted to the front of the channel.
- [`PriorityChannel`](priority_channel::PriorityChannel) - A Multiple Producer Multiple Consumer (MPMC) channel. Each message is only received by a single consumer. Higher priority items are shifted to the front of the channel.
- [`PubSubChannel`](pubsub::PubSubChannel) - A broadcast channel (publish-subscribe) channel. Each message is received by all consumers.
- [`Signal`](signal::Signal) - Signalling latest value to a single consumer.
- [`Watch`](watch::Watch) - Signalling latest value to multiple consumers.

View File

@ -71,6 +71,48 @@ where
pub fn poll_ready_to_send(&self, cx: &mut Context<'_>) -> Poll<()> {
self.channel.poll_ready_to_send(cx)
}
/// Returns the maximum number of elements the channel can hold.
///
/// See [`PriorityChannel::capacity()`]
pub const fn capacity(&self) -> usize {
self.channel.capacity()
}
/// Returns the free capacity of the channel.
///
/// See [`PriorityChannel::free_capacity()`]
pub fn free_capacity(&self) -> usize {
self.channel.free_capacity()
}
/// Clears all elements in the channel.
///
/// See [`PriorityChannel::clear()`]
pub fn clear(&self) {
self.channel.clear();
}
/// Returns the number of elements currently in the channel.
///
/// See [`PriorityChannel::len()`]
pub fn len(&self) -> usize {
self.channel.len()
}
/// Returns whether the channel is empty.
///
/// See [`PriorityChannel::is_empty()`]
pub fn is_empty(&self) -> bool {
self.channel.is_empty()
}
/// Returns whether the channel is full.
///
/// See [`PriorityChannel::is_full()`]
pub fn is_full(&self) -> bool {
self.channel.is_full()
}
}
impl<'ch, M, T, K, const N: usize> From<Sender<'ch, M, T, K, N>> for DynamicSender<'ch, T>
@ -146,6 +188,48 @@ where
pub fn poll_receive(&self, cx: &mut Context<'_>) -> Poll<T> {
self.channel.poll_receive(cx)
}
/// Returns the maximum number of elements the channel can hold.
///
/// See [`PriorityChannel::capacity()`]
pub const fn capacity(&self) -> usize {
self.channel.capacity()
}
/// Returns the free capacity of the channel.
///
/// See [`PriorityChannel::free_capacity()`]
pub fn free_capacity(&self) -> usize {
self.channel.free_capacity()
}
/// Clears all elements in the channel.
///
/// See [`PriorityChannel::clear()`]
pub fn clear(&self) {
self.channel.clear();
}
/// Returns the number of elements currently in the channel.
///
/// See [`PriorityChannel::len()`]
pub fn len(&self) -> usize {
self.channel.len()
}
/// Returns whether the channel is empty.
///
/// See [`PriorityChannel::is_empty()`]
pub fn is_empty(&self) -> bool {
self.channel.is_empty()
}
/// Returns whether the channel is full.
///
/// See [`PriorityChannel::is_full()`]
pub fn is_full(&self) -> bool {
self.channel.is_full()
}
}
impl<'ch, M, T, K, const N: usize> From<Receiver<'ch, M, T, K, N>> for DynamicReceiver<'ch, T>