pub struct RingBuffer<T> {
    pub data: VecDeque<T>,
    /* private fields */
}
Expand description

A fixed-size ring buffer backed by a VecDeque.

If the ring buffer is not full, then calling the push_back method appends elements, as in a VecDeque. If the ring buffer is full, then calling push_back removes the element at the front of the buffer (in a first-in, first-out manner) before appending the new element to the back of the buffer.

Use from_iter to take the last size elements from an iterator.

Examples

After exceeding the size limit, the oldest elements are dropped in favor of the newest element:

let mut buffer: RingBuffer<u8> = RingBuffer::new(2);
buffer.push_back(0);
buffer.push_back(1);
buffer.push_back(2);
assert_eq!(vec![1, 2], buffer.data);

Take the last n elements from an iterator:

let iter = [0, 1, 2].iter();
let actual = RingBuffer::from_iter(iter, 2).data;
let expected = VecDeque::from_iter([1, 2].iter());
assert_eq!(expected, actual);

Fields§

§data: VecDeque<T>

Implementations§

source§

impl<T> RingBuffer<T>

source

pub fn new(size: usize) -> Self

source

pub fn from_iter(iter: impl Iterator<Item = T>, size: usize) -> Self

source

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

Append a value to the end of the ring buffer.

If the ring buffer is not full, this method return None. If the ring buffer is full, appending a new element will cause the oldest element to be evicted. In that case this method returns that element, or None.

In the special case where the size limit is zero, each call to this method with input value returns Some(value), because the input is immediately evicted.

Examples

Appending an element when the buffer is full returns the oldest element:

let mut buf = RingBuffer::new(3);
assert_eq!(None, buf.push_back(0));
assert_eq!(None, buf.push_back(1));
assert_eq!(None, buf.push_back(2));
assert_eq!(Some(0), buf.push_back(3));

If the size limit is zero, then this method always returns the input value:

let mut buf = RingBuffer::new(0);
assert_eq!(Some(0), buf.push_back(0));
assert_eq!(Some(1), buf.push_back(1));
assert_eq!(Some(2), buf.push_back(2));

Auto Trait Implementations§

§

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>where T: Unpin,

§

impl<T> UnwindSafe for RingBuffer<T>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for Twhere 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 Twhere 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.