Struct RingBuffer

Source
pub struct RingBuffer<T: Copy + Clone, const N: usize> { /* private fields */ }
Expand description

An implementation of a ring buffer data structure that creates a “lossy” queue. This is a simplified version of the generic heapless Queue with a few constraints relaxed:

  • The Item type is at least Copy + Clone
  • We do not care about overwritten data in the buffer. Should be able to write indefinitely without error.

An iterator is also defined for RingBuffer which implements Iterator, ExactSizeIterator, and DoubleEndedIterator. The iterator begins at the head of the ring buffer and iterates until it reaches the tail.

Also uses an internal flag overwriting to allow iteration over the last value in the ring buffer, giving the structure a capacity of N rather than the typical ringbuffer capacity of N-1.

Implementations§

Source§

impl<T, const N: usize> RingBuffer<T, N>
where T: Copy + Clone,

Source

pub fn new(backing_store: [T; N]) -> Self

Source

pub fn is_empty(&self) -> bool

Source

pub fn is_full(&self) -> bool

Source

pub fn capacity(&self) -> usize

Source

pub fn add(&mut self, item: T)

Add a new item to the ring buffer.

Source

pub fn pop(&mut self) -> Option<T>

Pops the last item off of the ring buffer.

Trait Implementations§

Source§

impl<'a, T, const N: usize> IntoIterator for &'a RingBuffer<T, N>
where T: Copy + Clone,

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = RingBufferIterator<'a, T, N>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<T, const N: usize> Freeze for RingBuffer<T, N>
where T: Freeze,

§

impl<T, const N: usize> RefUnwindSafe for RingBuffer<T, N>
where T: RefUnwindSafe,

§

impl<T, const N: usize> Send for RingBuffer<T, N>
where T: Send,

§

impl<T, const N: usize> Sync for RingBuffer<T, N>
where T: Sync,

§

impl<T, const N: usize> Unpin for RingBuffer<T, N>
where T: Unpin,

§

impl<T, const N: usize> UnwindSafe for RingBuffer<T, N>
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> 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, 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.