pub struct CircularBuffer<T> { /* private fields */ }
Expand description

A circular buffer.

A circular buffer is an ordered buffer with a fixed capacity and the property that if it is full and a new element is pushed to the front, the oldest element is deleted.

The buffer is created using the new method, elements are pushed to the front using push, and retrieved using std::ops::Index::index trait method.

let mut buf = CircularBuffer::new(3);
buf.push(0);
buf.push(1);
assert_eq![buf.index(0), &1];
assert_eq![buf.index(1), &0];
buf.push(2);
buf.push(3);
assert_eq![buf.index(0), &3];
assert_eq![buf.index(1), &2];
assert_eq![buf.index(2), &1];

Implementations§

source§

impl<T> CircularBuffer<T>

source

pub fn new(capacity: usize) -> CircularBuffer<T>

Create a new circular buffer with the provided capacity.

source

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

Push a new element to the front of the buffer.

source

pub fn capacity(&self) -> usize

Return the buffer’s capacity.

source§

impl<T: Clone> CircularBuffer<T>

source

pub fn clone_to_front(&mut self, i: usize) -> &mut T

Clone the element at the provided index to the front of the buffer.

The buffer contains the following optimization: if the buffer is full and the referenced element is the tail element, this method is a no-op. If the method’s clone function has side effects, these will not be seen, as in the following example.

/// A struct whose clone function has a side effect; i.e., is not pure.
/// The side effect is that a shared counter owned by all clones is incremented.
/// The shared counter thus records the number of times a clone has occured.
struct ImpureClone {
    counter: Rc<RefCell<i64>>,
}

impl Clone for ImpureClone {
    fn clone(&self) -> Self {
        let old_count = *self.counter.borrow();
        *self.counter.borrow_mut() = old_count + 1;
        ImpureClone {
            counter: self.counter.clone()
        }
    }
}

let mut buf = CircularBuffer::new(2);
let counter = Rc::new(RefCell::new(0));
buf.push(ImpureClone{counter: counter.clone()});
assert_eq![*counter.borrow(), 0];

buf.clone_to_front(0);
assert_eq![*counter.borrow(), 1];

// Clone from the tail - no clone occurs!
buf.clone_to_front(1);
assert_eq![*counter.borrow(), 1];

Trait Implementations§

source§

impl<T> Index<usize> for CircularBuffer<T>

source§

fn index(&self, i: usize) -> &T

Get the element at the provided index.

The first element in the buffer has index 0, the next element index 1, etc.

This method may panic if the index is valid.

§

type Output = T

The returned type after indexing.

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for CircularBuffer<T>where T: RefUnwindSafe,

§

impl<T> Send for CircularBuffer<T>where T: Send,

§

impl<T> Sync for CircularBuffer<T>where T: Sync,

§

impl<T> Unpin for CircularBuffer<T>where T: Unpin,

§

impl<T> UnwindSafe for CircularBuffer<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, 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.