diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-08-04 23:18:56 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-08-04 23:18:56 +0200 |
| commit | 99a97ec6c9c1212c16a5411e6743979de5a040e9 (patch) | |
| tree | 445397e11fd4e26b99c351c0d83a48080ef1d81b | |
| parent | ac1a26b40f417e776de8ebd731f811134f97b3f9 (diff) | |
util/pipe: add some capacity/len getters.
| -rw-r--r-- | embassy-util/src/pipe.rs | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/embassy-util/src/pipe.rs b/embassy-util/src/pipe.rs index c0a5d2f6f..9c20aeeff 100644 --- a/embassy-util/src/pipe.rs +++ b/embassy-util/src/pipe.rs | |||
| @@ -317,6 +317,35 @@ where | |||
| 317 | pub fn clear(&self) { | 317 | pub fn clear(&self) { |
| 318 | self.lock(|c| c.clear()) | 318 | self.lock(|c| c.clear()) |
| 319 | } | 319 | } |
| 320 | |||
| 321 | /// Return whether the pipe is full (no free space in the buffer) | ||
| 322 | pub fn is_full(&self) -> bool { | ||
| 323 | self.len() == N | ||
| 324 | } | ||
| 325 | |||
| 326 | /// Return whether the pipe is empty (no data buffered) | ||
| 327 | pub fn is_empty(&self) -> bool { | ||
| 328 | self.len() == 0 | ||
| 329 | } | ||
| 330 | |||
| 331 | /// Total byte capacity. | ||
| 332 | /// | ||
| 333 | /// This is the same as the `N` generic param. | ||
| 334 | pub fn capacity(&self) -> usize { | ||
| 335 | N | ||
| 336 | } | ||
| 337 | |||
| 338 | /// Used byte capacity. | ||
| 339 | pub fn len(&self) -> usize { | ||
| 340 | self.lock(|c| c.buffer.len()) | ||
| 341 | } | ||
| 342 | |||
| 343 | /// Free byte capacity. | ||
| 344 | /// | ||
| 345 | /// This is equivalent to `capacity() - len()` | ||
| 346 | pub fn free_capacity(&self) -> usize { | ||
| 347 | N - self.len() | ||
| 348 | } | ||
| 320 | } | 349 | } |
| 321 | 350 | ||
| 322 | #[cfg(feature = "nightly")] | 351 | #[cfg(feature = "nightly")] |
