Skip to main content

RingBuffer

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: usize

Implementations§

§

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]) -> Self
where 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>

pub fn set_all(&mut self, value: T)

Trait Implementations§

§

impl<T: Clone> Clone for RingBuffer<T>

§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl<T> From<RingBuffer<T>> for Vec<T>

§

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>,

§

fn from(iter: U) -> Self

Converts to this type from the input type.
§

impl<T> Index<usize> for RingBuffer<T>

§

type Output = T

The returned type after indexing.
§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
§

impl<T> IntoIterator for RingBuffer<T>

§

type Item = T

The type of the elements being iterated over.
§

type IntoIter = Flatten<IntoIter<Option<<RingBuffer<T> as IntoIterator>::Item>>>

Which kind of iterator are we turning this into?
§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.