From 99a97ec6c9c1212c16a5411e6743979de5a040e9 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Thu, 4 Aug 2022 23:18:56 +0200 Subject: [PATCH] util/pipe: add some capacity/len getters. --- embassy-util/src/pipe.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/embassy-util/src/pipe.rs b/embassy-util/src/pipe.rs index c0a5d2f6..9c20aeef 100644 --- a/embassy-util/src/pipe.rs +++ b/embassy-util/src/pipe.rs @@ -317,6 +317,35 @@ where pub fn clear(&self) { self.lock(|c| c.clear()) } + + /// Return whether the pipe is full (no free space in the buffer) + pub fn is_full(&self) -> bool { + self.len() == N + } + + /// Return whether the pipe is empty (no data buffered) + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + /// Total byte capacity. + /// + /// This is the same as the `N` generic param. + pub fn capacity(&self) -> usize { + N + } + + /// Used byte capacity. + pub fn len(&self) -> usize { + self.lock(|c| c.buffer.len()) + } + + /// Free byte capacity. + /// + /// This is equivalent to `capacity() - len()` + pub fn free_capacity(&self) -> usize { + N - self.len() + } } #[cfg(feature = "nightly")]