Struct circ_buffer::RingBuffer

source ·
pub struct RingBuffer<T, const N: usize>(/* private fields */);
Expand description

A ring Buffer with constant size. Makes use of a fixed-size array internally.

let mut circ_buffer = RingBuffer::<i64, 4>::default();

// These entries will be made into free space in the buffer.
circ_buffer.push(1_i64);
circ_buffer.push(2_i64);
circ_buffer.push(3_i64);
circ_buffer.push(4_i64);

// Now it will start truncating initial entries.
circ_buffer.push(5_i64);
circ_buffer.push(6_i64);
circ_buffer.push(7_i64);

let mut elements = circ_buffer.iter();
assert_eq!(elements.next(), Some(&4));
assert_eq!(elements.next(), Some(&5));
assert_eq!(elements.next(), Some(&6));
assert_eq!(elements.next(), Some(&7));

Implementations§

source§

impl<T, const N: usize> RingBuffer<T, N>

source

pub fn new() -> Self

Creates a new empty RingBuffer

source

pub fn get_size(&self) -> usize

Gets the current size of the RingBuffer

source§

impl<T, const N: usize> RingBuffer<T, N>

source

pub fn push(&mut self, new_item: T)

Append one element to the buffer.

This will not grow the buffer but instead replace existing entries when the maximum size is reached.

let mut circ_buffer = RingBuffer::<f64, 5>::default();
circ_buffer.push(1.0);
circ_buffer.push(2.0);
circ_buffer.push(3.0);
circ_buffer.push(4.0);
circ_buffer.push(5.0);
// Now we begin to drop the first entry when pushing more values.
circ_buffer.push(6.0);
let elements = circ_buffer.iter().collect::<Vec<_>>();
assert_eq!(elements, vec![&2.0, &3.0, &4.0, &5.0, &6.0])
source

pub fn iter<'a>(&'a self) -> RingBufferIterRef<'a, T, N>

Iterate over references to elements of the RingBuffer.

Trait Implementations§

source§

impl<T: Clone, const N: usize> Clone for RingBuffer<T, N>

source§

fn clone(&self) -> RingBuffer<T, N>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<T: Debug, const N: usize> Debug for RingBuffer<T, N>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T, const N: usize> Default for RingBuffer<T, N>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<T, const N: usize> Index<usize> for RingBuffer<T, N>

§

type Output = T

The returned type after indexing.
source§

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

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

impl<T, const N: usize> IntoIterator for RingBuffer<T, N>

§

type Item = T

The type of the elements being iterated over.
§

type IntoIter = RingBufferIter<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> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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>,

§

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

§

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.