Skip to main content

CircularBuffer

Struct CircularBuffer 

Source
pub struct CircularBuffer { /* private fields */ }
Expand description

A fixed-capacity circular (ring) buffer backed by a flat array.

Supports O(1) push-front, push-back, pop-front, and pop-back. Useful for implementing deques and sliding-window algorithms.

Implementations§

Source§

impl CircularBuffer

Source

pub fn new(cap: usize) -> Self

Create a new circular buffer with the given capacity.

Source

pub fn len(&self) -> usize

Return the number of elements currently stored.

Source

pub fn is_empty(&self) -> bool

Return true if the buffer contains no elements.

Source

pub fn is_full(&self) -> bool

Return true if the buffer is at full capacity.

Source

pub fn push_back(&mut self, val: i64) -> bool

Push an element to the back. Returns false if the buffer is full.

Source

pub fn pop_front(&mut self) -> Option<i64>

Pop an element from the front. Returns None if empty.

Source

pub fn front(&self) -> Option<i64>

Peek at the front element without removing it.

Auto Trait Implementations§

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.