From 3d9b502c7af1f1dab92ee4a3aa07dc667a3bf103 Mon Sep 17 00:00:00 2001
From: Oliver Rockstedt <info@sourcebox.de>
Date: Sat, 18 May 2024 13:37:51 +0200
Subject: [PATCH 1/3] embassy-sync: Add capacity and free_capacity functions to
 Channel

---
 embassy-sync/CHANGELOG.md   |  2 +-
 embassy-sync/src/channel.rs | 12 ++++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/embassy-sync/CHANGELOG.md b/embassy-sync/CHANGELOG.md
index 3f6b39d8b..46466d610 100644
--- a/embassy-sync/CHANGELOG.md
+++ b/embassy-sync/CHANGELOG.md
@@ -7,7 +7,7 @@ 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`.
 
 ## 0.5.0 - 2023-12-04
 
diff --git a/embassy-sync/src/channel.rs b/embassy-sync/src/channel.rs
index c4267064c..f9f71d026 100644
--- a/embassy-sync/src/channel.rs
+++ b/embassy-sync/src/channel.rs
@@ -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())

From f361c2e81cc2d595adc3896f8a8a198973392046 Mon Sep 17 00:00:00 2001
From: Oliver Rockstedt <info@sourcebox.de>
Date: Sat, 18 May 2024 13:48:40 +0200
Subject: [PATCH 2/3] embassy-sync: Add capacity, free_capacity, len, is_empty
 and is_full functions to PriorityChannel

---
 embassy-sync/CHANGELOG.md            |  1 +
 embassy-sync/src/priority_channel.rs | 39 ++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)

diff --git a/embassy-sync/CHANGELOG.md b/embassy-sync/CHANGELOG.md
index 46466d610..28d97e68d 100644
--- a/embassy-sync/CHANGELOG.md
+++ b/embassy-sync/CHANGELOG.md
@@ -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
 
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
             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

From ab899934513d7b7b1eda1727bc145713856e4a9a Mon Sep 17 00:00:00 2001
From: Oliver Rockstedt <info@sourcebox.de>
Date: Sat, 18 May 2024 14:01:23 +0200
Subject: [PATCH 3/3] embassy-sync: Add capacity, free_capacity, len, is_empty
 and is_full functions to PubSubChannel

---
 embassy-sync/CHANGELOG.md      |  1 +
 embassy-sync/src/pubsub/mod.rs | 39 ++++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)

diff --git a/embassy-sync/CHANGELOG.md b/embassy-sync/CHANGELOG.md
index 28d97e68d..7a830a853 100644
--- a/embassy-sync/CHANGELOG.md
+++ b/embassy-sync/CHANGELOG.md
@@ -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
 
diff --git a/embassy-sync/src/pubsub/mod.rs b/embassy-sync/src/pubsub/mod.rs
index 6afd54af5..754747ab8 100644
--- a/embassy-sync/src/pubsub/mod.rs
+++ b/embassy-sync/src/pubsub/mod.rs
@@ -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]