Struct CircleBuffer

Source
pub struct CircleBuffer<T>
where T: Clone,
{ /* private fields */ }
Expand description

A circular buffer.

Implementations§

Source§

impl<T> CircleBuffer<T>
where T: Clone,

Source

pub fn with_capacity(capacity: usize) -> CircleBuffer<T>

Creates a new empty CircleBuffer<T> with capacity.

§Examples
use circle_buffer::CircleBuffer;

let mut cbuf: CircleBuffer<i32> = CircleBuffer::with_capacity(3);
Source

pub fn capacity(&self) -> usize

Returns the capacity of the buffer.

§Examples
use circle_buffer::CircleBuffer;

let mut cbuf: CircleBuffer<i32> = CircleBuffer::with_capacity(3);
assert_eq!(3, cbuf.capacity());
Source

pub fn len(&self) -> usize

Returns the current number of elements in the buffer.

§Examples
use circle_buffer::CircleBuffer;

let mut cbuf: CircleBuffer<i32> = CircleBuffer::with_capacity(10);
cbuf.push(1);
cbuf.push(1);
cbuf.push(1);

assert_eq!(cbuf.len(), 3);
Source

pub fn is_empty(&self) -> bool

Returns true if the buffer contains no elements.

§Examples
use circle_buffer::CircleBuffer;

let mut cbuf: CircleBuffer<i32> = CircleBuffer::with_capacity(3);
assert!(cbuf.is_empty());

cbuf.push(1);
assert!(!cbuf.is_empty())
Source

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

Pushes a new element into the buffer. Once the capacity is reached, pushing new items will overwrite old ones.

§Examples
use circle_buffer::CircleBuffer;

let mut cbuf: CircleBuffer<i32> = CircleBuffer::with_capacity(3);
cbuf.push(1);
cbuf.push(2);
cbuf.push(3);
cbuf.push(4);

assert_eq!(cbuf.len(), 3);

assert_eq!(cbuf[0], 2);
assert_eq!(cbuf[1], 3);
assert_eq!(cbuf[2], 4);

let mut sum = 0;
for x in cbuf.as_slice() {
    sum += x;
}
assert_eq!(sum, 9);
Source

pub fn as_slice(&self) -> &[T]

Extracts a slice containing the entire buffer.

Source

pub fn as_mut_slice(&mut self) -> &mut [T]

Extracts a mutable slice of the entire buffer.

Source

pub fn iter(&self) -> Iter<'_, T>

Returns an iterator over the buffer’s contents. The iterator goes from the most recently pushed items to the oldest ones.

§Examples
use circle_buffer::CircleBuffer;

let mut buffer = CircleBuffer::with_capacity(3);
buffer.push(1);
buffer.push(2);
buffer.push(3);
let add1: Vec<i32> = buffer.iter().map(|x| x + 1).collect();
assert_eq!(add1, vec![2, 3, 4]);

buffer.push(4);
buffer.push(5);
let add2: Vec<i32> = buffer.iter().map(|x| x + 2).collect();
assert_eq!(add2, vec![5, 6, 7]);
Source

pub fn iter_mut(&mut self) -> IterMut<'_, T>

Returns a mutable iterator over the buffer’s contents. The iterator goes from the most recently pushed items to the oldest ones.

§Examples
use circle_buffer::CircleBuffer;

let mut buffer = CircleBuffer::with_capacity(3);
buffer.push(1);
buffer.push(2);
buffer.push(3);
for x in buffer.iter_mut() {
    *x += 1;
}
assert_eq!(buffer.as_slice(), &[2, 3, 4]);

buffer.push(4);
buffer.push(5);
for x in buffer.iter_mut() {
    *x += 2;
}
assert_eq!(buffer.as_slice(), &[6, 6, 7]);
Source§

impl<T> CircleBuffer<T>
where T: Clone + Zero,

Source

pub fn zero_clear(&mut self)

Fill all elements with zeros.

§Examples
use circle_buffer::CircleBuffer;

let mut cbuf: CircleBuffer<i32> = CircleBuffer::with_capacity(3);
cbuf.push(1);
cbuf.zero_clear();
assert!(cbuf.len() == 3);
assert_eq!(cbuf.as_slice(), &[0, 0, 0]);

cbuf.push(2);
assert_eq!(cbuf.as_slice(), &[0, 0, 2]);

Trait Implementations§

Source§

impl<T> Index<usize> for CircleBuffer<T>
where T: Clone,

Source§

type Output = T

The returned type after indexing.
Source§

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

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

impl<T> IndexMut<usize> for CircleBuffer<T>
where T: Clone,

Source§

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

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

Auto Trait Implementations§

§

impl<T> Freeze for CircleBuffer<T>

§

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

§

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

§

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

§

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

§

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