Struct RingBuffer
pub struct RingBuffer<T> {
data: Box<[Option<T>]>,
head: usize,
}Expand description
A high performance ring buffer optimized for writing that can be used to store a fixed number of elements. This ring buffer is a contiguous memory array allocated in the heap and keeps track of the last index that was written. Pushing a new element will overwrite the oldest element in the buffer. Additionally, iteration will always be O(n) where n is the capacity of the buffer, regardless of the number of elements that are stored in the buffer.
The index 0 of the ring buffer will always be the oldest element in the buffer.
It might be justified to use unsafe code for peak performance.
Fields§
§data: Box<[Option<T>]>§head: usizeImplementations§
§impl<T> RingBuffer<T>
impl<T> RingBuffer<T>
pub fn new(buffer: Box<[Option<T>]>) -> Self
pub fn with_capacity(size: usize) -> Self
pub fn from_slice(slice: &[T]) -> Selfwhere
T: Clone,
pub fn from_vec(vec: impl Into<Vec<T>>) -> Self
fn get_real_idx(&self, index: usize) -> usize
pub fn push(&mut self, item: T)
pub fn last(&self) -> &Option<T>
fn next_index(&self) -> usize
pub fn iter(&self) -> RingBufferIter<'_, T> ⓘ
pub fn iter_range(&self, range: Range<usize>) -> RingBufferIter<'_, T> ⓘ
pub fn as_vec(&self) -> Vec<&T>
pub fn len(&self) -> usize
§impl<T: Clone> RingBuffer<T>
impl<T: Clone> RingBuffer<T>
pub fn set_all(&mut self, value: T)
Trait Implementations§
§impl<T: Clone> Clone for RingBuffer<T>
impl<T: Clone> Clone for RingBuffer<T>
§impl<T> From<RingBuffer<T>> for Vec<T>
impl<T> From<RingBuffer<T>> for Vec<T>
§fn from(buffer: RingBuffer<T>) -> Self
fn from(buffer: RingBuffer<T>) -> Self
Converts to this type from the input type.
§impl<T, U> From<U> for RingBuffer<T>where
U: Iterator<Item = T>,
impl<T, U> From<U> for RingBuffer<T>where
U: Iterator<Item = T>,
§impl<T> Index<usize> for RingBuffer<T>
impl<T> Index<usize> for RingBuffer<T>
§impl<T> IntoIterator for RingBuffer<T>
impl<T> IntoIterator for RingBuffer<T>
Auto Trait Implementations§
impl<T> Freeze for RingBuffer<T>
impl<T> RefUnwindSafe for RingBuffer<T>where
T: RefUnwindSafe,
impl<T> Send for RingBuffer<T>where
T: Send,
impl<T> Sync for RingBuffer<T>where
T: Sync,
impl<T> Unpin for RingBuffer<T>
impl<T> UnsafeUnpin for RingBuffer<T>
impl<T> UnwindSafe for RingBuffer<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more