Trait ringbuffer::RingBufferRead
source · pub trait RingBufferRead<T>: RingBuffer<T> {
// Required methods
fn dequeue_ref(&mut self) -> Option<&T>;
fn skip(&mut self);
// Provided methods
fn dequeue(&mut self) -> Option<T>
where T: Clone { ... }
fn drain(&mut self) -> RingBufferDrainingIterator<'_, T, Self>
where T: Clone { ... }
}Expand description
Defines behaviour for ringbuffers which allow for reading from the start of them (as a queue).
For arbitrary buffer access however, RingBufferExt is necessary.
Required Methods§
sourcefn dequeue_ref(&mut self) -> Option<&T>
fn dequeue_ref(&mut self) -> Option<&T>
dequeues the top item off the ringbuffer. Returns a reference to the item. This means that lifetimes will be problematic because as long as this reference exists, you can not push to the queue. To solve this, use the dequeue method. This requires the item to be clone. Easily moving out of the ringbuffer is sadly impossible.
Returns None when the ringbuffer is empty.
Provided Methods§
sourcefn dequeue(&mut self) -> Option<T>where
T: Clone,
fn dequeue(&mut self) -> Option<T>where
T: Clone,
dequeues the top item off the ringbuffer and returns a cloned version. See the dequeue_ref docs
sourcefn drain(&mut self) -> RingBufferDrainingIterator<'_, T, Self>where
T: Clone,
fn drain(&mut self) -> RingBufferDrainingIterator<'_, T, Self>where
T: Clone,
Returns an iterator over the elements in the ringbuffer, dequeueing elements as they are iterated over.
use ringbuffer::{AllocRingBuffer, RingBufferWrite, RingBufferRead, RingBuffer};
let mut rb = AllocRingBuffer::with_capacity(16);
for i in 0..8 {
rb.push(i);
}
assert_eq!(rb.len(), 8);
for i in rb.drain() {
// prints the numbers 0 through 8
println!("{}", i);
}
// No elements remain
assert_eq!(rb.len(), 0);
Object Safety§
This trait is not object safe.