Skip to content

Commit 70687d8

Browse files
authored
Merge pull request #182 from almusil/expose_len_for_consumer_producer
Queue: Expose len and capacity for Consumer and Producer
2 parents 9ff3a5f + 5823e47 commit 70687d8

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/spsc/split.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,23 @@ macro_rules! impl_ {
134134
self._dequeue(head) // ▲
135135
}
136136

137+
/// Returns the maximum number of elements the queue can hold
138+
pub fn capacity(&self) -> $uxx {
139+
unsafe { self.rb.as_ref().capacity() }
140+
}
141+
142+
/// Returns the number of elements in the queue
143+
///
144+
/// # Note
145+
///
146+
/// This is a conservative estimate. Interrupt during this function
147+
/// might cause that the `Consumer` actually has more than N items available.
148+
pub fn len(&self) -> $uxx {
149+
let head = unsafe { self.rb.as_ref().0.head.load_relaxed() };
150+
let tail = unsafe { self.rb.as_ref().0.tail.load_acquire() };
151+
tail.wrapping_sub(head)
152+
}
153+
137154
unsafe fn _peek(&self, head: $uxx) -> &T {
138155
let rb = self.rb.as_ref();
139156

@@ -197,6 +214,23 @@ macro_rules! impl_ {
197214
}
198215
}
199216

217+
/// Returns the maximum number of elements the queue can hold
218+
pub fn capacity(&self) -> $uxx {
219+
unsafe { self.rb.as_ref().capacity() }
220+
}
221+
222+
/// Returns the number of elements in the queue
223+
///
224+
/// # Note
225+
///
226+
/// This is a conservative estimate. Interrupt during this function
227+
/// might cause that the `Producer` actually has more than N items of available space.
228+
pub fn len(&self) -> $uxx {
229+
let head = unsafe { self.rb.as_ref().0.head.load_acquire() };
230+
let tail = unsafe { self.rb.as_ref().0.tail.load_relaxed() };
231+
tail.wrapping_sub(head)
232+
}
233+
200234
/// Adds an `item` to the end of the queue without checking if it's full
201235
///
202236
/// # Unsafety

0 commit comments

Comments
 (0)