mirror of
https://github.com/rust-embedded/heapless.git
synced 2025-09-27 12:30:35 +00:00
Merge pull request #182 from almusil/expose_len_for_consumer_producer
Queue: Expose len and capacity for Consumer and Producer
This commit is contained in:
commit
70687d8ace
@ -134,6 +134,23 @@ macro_rules! impl_ {
|
||||
self._dequeue(head) // ▲
|
||||
}
|
||||
|
||||
/// Returns the maximum number of elements the queue can hold
|
||||
pub fn capacity(&self) -> $uxx {
|
||||
unsafe { self.rb.as_ref().capacity() }
|
||||
}
|
||||
|
||||
/// Returns the number of elements in the queue
|
||||
///
|
||||
/// # Note
|
||||
///
|
||||
/// This is a conservative estimate. Interrupt during this function
|
||||
/// might cause that the `Consumer` actually has more than N items available.
|
||||
pub fn len(&self) -> $uxx {
|
||||
let head = unsafe { self.rb.as_ref().0.head.load_relaxed() };
|
||||
let tail = unsafe { self.rb.as_ref().0.tail.load_acquire() };
|
||||
tail.wrapping_sub(head)
|
||||
}
|
||||
|
||||
unsafe fn _peek(&self, head: $uxx) -> &T {
|
||||
let rb = self.rb.as_ref();
|
||||
|
||||
@ -197,6 +214,23 @@ macro_rules! impl_ {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the maximum number of elements the queue can hold
|
||||
pub fn capacity(&self) -> $uxx {
|
||||
unsafe { self.rb.as_ref().capacity() }
|
||||
}
|
||||
|
||||
/// Returns the number of elements in the queue
|
||||
///
|
||||
/// # Note
|
||||
///
|
||||
/// This is a conservative estimate. Interrupt during this function
|
||||
/// might cause that the `Producer` actually has more than N items of available space.
|
||||
pub fn len(&self) -> $uxx {
|
||||
let head = unsafe { self.rb.as_ref().0.head.load_acquire() };
|
||||
let tail = unsafe { self.rb.as_ref().0.tail.load_relaxed() };
|
||||
tail.wrapping_sub(head)
|
||||
}
|
||||
|
||||
/// Adds an `item` to the end of the queue without checking if it's full
|
||||
///
|
||||
/// # Unsafety
|
||||
|
Loading…
x
Reference in New Issue
Block a user