Trait RingBuffer

Source
pub unsafe trait RingBuffer<T>:
    Sized
    + IntoIterator<Item = T>
    + Extend<T>
    + Index<usize, Output = T>
    + IndexMut<usize> {
Show 31 methods // Required methods fn enqueue(&mut self, value: T) -> Option<T>; fn dequeue(&mut self) -> Option<T>; fn fill_with<F: FnMut() -> T>(&mut self, f: F); fn clear(&mut self); fn get_signed(&self, index: isize) -> Option<&T>; fn get(&self, index: usize) -> Option<&T>; unsafe fn ptr_copy_to_slice(rb: *const Self, offset: usize, dst: &mut [T]) where T: Copy; unsafe fn ptr_copy_from_slice(rb: *mut Self, offset: usize, src: &[T]) where T: Copy; // Provided methods fn len(&self) -> usize { ... } fn is_empty(&self) -> bool { ... } fn is_full(&self) -> bool { ... } fn capacity(&self) -> usize { ... } fn buffer_size(&self) -> usize { ... } fn push(&mut self, value: T) { ... } fn skip(&mut self) { ... } fn drain(&mut self) -> RingBufferDrainingIterator<'_, T, Self> { ... } fn fill_default(&mut self) where T: Default { ... } fn fill(&mut self, value: T) where T: Clone { ... } fn get_mut_signed(&mut self, index: isize) -> Option<&mut T> { ... } fn get_mut(&mut self, index: usize) -> Option<&mut T> { ... } fn peek(&self) -> Option<&T> { ... } fn front(&self) -> Option<&T> { ... } fn front_mut(&mut self) -> Option<&mut T> { ... } fn back(&self) -> Option<&T> { ... } fn back_mut(&mut self) -> Option<&mut T> { ... } fn iter_mut(&mut self) -> RingBufferMutIterator<'_, T, Self> { ... } fn iter(&self) -> RingBufferIterator<'_, T, Self> { ... } fn to_vec(&self) -> Vec<T> where T: Clone { ... } fn contains(&self, elem: &T) -> bool where T: PartialEq { ... } fn copy_to_slice(&self, offset: usize, dst: &mut [T]) where T: Copy { ... } fn copy_from_slice(&mut self, offset: usize, src: &[T]) where T: Copy { ... }
}
Expand description

RingBuffer is a trait defining the standard interface for all RingBuffer implementations (AllocRingBuffer, ConstGenericRingBuffer)

This trait is not object safe, so can’t be used dynamically. However it is possible to define a generic function over types implementing RingBuffer.

§Safety

Implementing this implies that the ringbuffer upholds some safety guarantees, such as returning a different value from get_mut any for every different index passed in. See the exact requirements in the safety comment on the next function of the mutable Iterator implementation, since these safety guarantees are necessary for iter_mut to work

Required Methods§

Source

fn enqueue(&mut self, value: T) -> Option<T>

Adds a value onto the buffer.

Cycles around if capacity is reached. Forms a more natural counterpart to dequeue. An alias is provided with push.

Source

fn dequeue(&mut self) -> Option<T>

dequeues the top item off the ringbuffer, and moves this item out.

Source

fn fill_with<F: FnMut() -> T>(&mut self, f: F)

Sets every element in the ringbuffer to the value returned by f.

Source

fn clear(&mut self)

Empties the buffer entirely. Sets the length to 0 but keeps the capacity allocated.

Source

fn get_signed(&self, index: isize) -> Option<&T>

Gets a value relative to the current index. 0 is the next index to be written to with push. -1 and down are the last elements pushed and 0 and up are the items that were pushed the longest ago.

Source

fn get(&self, index: usize) -> Option<&T>

Gets a value relative to the current index. 0 is the next index to be written to with push.

Source

unsafe fn ptr_copy_to_slice(rb: *const Self, offset: usize, dst: &mut [T])
where T: Copy,

Efficiently copy items from the ringbuffer to a target slice.

§Panics

Panics if the buffer length minus the offset is NOT equal to target.len().

§Safety

ONLY SAFE WHEN self is a *const to to an implementor of RingBuffer

Source

unsafe fn ptr_copy_from_slice(rb: *mut Self, offset: usize, src: &[T])
where T: Copy,

Efficiently copy items from a slice to the ringbuffer.

§Panics

Panics if the buffer length minus the offset is NOT equal to source.len().

§Safety

ONLY SAFE WHEN self is a *mut to to an implementor of RingBuffer

Provided Methods§

Source

fn len(&self) -> usize

Returns the length of the internal buffer. This length grows up to the capacity and then stops growing. This is because when the length is reached, new items are appended at the start.

Source

fn is_empty(&self) -> bool

Returns true if the buffer is entirely empty.

Source

fn is_full(&self) -> bool

Returns true when the length of the ringbuffer equals the capacity. This happens whenever more elements than capacity have been pushed to the buffer.

Source

fn capacity(&self) -> usize

Returns the capacity of the buffer.

Source

fn buffer_size(&self) -> usize

Returns the number of elements allocated for this ringbuffer (can be larger than capacity).

Source

fn push(&mut self, value: T)

👎Deprecated: use enqueue instead

Alias for [enqueue]

Source

fn skip(&mut self)

👎Deprecated: use dequeue instead

dequeues the top item off the queue, but does not return it. Instead it is dropped. If the ringbuffer is empty, this function is a nop.

Source

fn drain(&mut self) -> RingBufferDrainingIterator<'_, T, Self>

Returns an iterator over the elements in the ringbuffer, dequeueing elements as they are iterated over.

use ringbuffer::{AllocRingBuffer, RingBuffer};

let mut rb = AllocRingBuffer::new(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);
Source

fn fill_default(&mut self)
where T: Default,

Sets every element in the ringbuffer to it’s default value

Source

fn fill(&mut self, value: T)
where T: Clone,

Sets every element in the ringbuffer to value

Source

fn get_mut_signed(&mut self, index: isize) -> Option<&mut T>

Gets a value relative to the current index mutably. 0 is the next index to be written to with push. -1 and down are the last elements pushed and 0 and up are the items that were pushed the longest ago.

Source

fn get_mut(&mut self, index: usize) -> Option<&mut T>

Gets a value relative to the current index mutably. 0 is the next index to be written to with push.

Source

fn peek(&self) -> Option<&T>

Returns the value at the current index. This is the value that will be overwritten by the next push and also the value pushed the longest ago. (alias of Self::front)

Source

fn front(&self) -> Option<&T>

Returns the value at the front of the queue. This is the value that will be overwritten by the next push and also the value pushed the longest ago. (alias of peek)

Source

fn front_mut(&mut self) -> Option<&mut T>

Returns a mutable reference to the value at the back of the queue. This is the value that will be overwritten by the next push. (alias of peek)

Source

fn back(&self) -> Option<&T>

Returns the value at the back of the queue. This is the item that was pushed most recently.

Source

fn back_mut(&mut self) -> Option<&mut T>

Returns a mutable reference to the value at the back of the queue. This is the item that was pushed most recently.

Source

fn iter_mut(&mut self) -> RingBufferMutIterator<'_, T, Self>

Creates a mutable iterator over the buffer starting from the item pushed the longest ago, and ending at the element most recently pushed.

Source

fn iter(&self) -> RingBufferIterator<'_, T, Self>

Creates an iterator over the buffer starting from the item pushed the longest ago, and ending at the element most recently pushed.

Source

fn to_vec(&self) -> Vec<T>
where T: Clone,

Converts the buffer to a vector. This Copies all elements in the ringbuffer.

Source

fn contains(&self, elem: &T) -> bool
where T: PartialEq,

Returns true if elem is in the ringbuffer.

Source

fn copy_to_slice(&self, offset: usize, dst: &mut [T])
where T: Copy,

Efficiently copy items from the ringbuffer to a target slice.

§Panics

Panics if the buffer length minus the offset is NOT equal to target.len().

Source

fn copy_from_slice(&mut self, offset: usize, src: &[T])
where T: Copy,

Efficiently copy items from a slice to the ringbuffer.

§Panics

Panics if the buffer length minus the offset is NOT equal to source.len().

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§