Skip to main content Skip to main content
Pigweed's logo Pigweed
code_xml
  1. Home
  2. Reference
  3. Rust
  4. pw_kernel
  5. circular_buffer
  6. CircularBuffer

CircularBuffer

Struct CircularBuffer 

Source
pub struct CircularBuffer<T, const N: usize> { /* private fields */ }
Expand description

A fixed-capacity circular buffer.

The buffer has the following properties:

  • No restrictions on a power of two on the value of N.
  • The full size of buffer is used (no wasted space).
  • When the buffer is full, writing will return an error until the buffer is read from.

§Invariants

For a buffer with N > 0, the following invariants are maintained:

  1. 0 <= self.len <= N: The number of elements is always between 0 and the capacity, inclusive.
  2. 0 <= self.head < N: The head index is always within the array bounds.
  3. 0 <= self.tail < N: The tail index is always within the array bounds.
  4. self.head == (self.tail + self.len) % N: The head is advanced from the tail by the length, modulo N.
  5. The self.len populated elements are stored in buffer at indices (self.tail) % N, (self.tail + 1) % N, …, (self.tail + self.len - 1) % N.

The case N = 0 is valid and handled; push_back will always return Error::ResourceExhausted and pop_front will always return None, preventing any out-of-bounds access or division by zero.

Implementations§

Source§

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

Source

pub const fn new() -> Self

Creates a new, empty circular buffer.

Source

pub const fn is_empty(&self) -> bool

Returns true if the buffer is empty.

Source

pub const fn is_full(&self) -> bool

Returns true if the buffer is full.

Source

pub const fn len(&self) -> usize

Returns the number of elements in the buffer.

Source

pub const fn capacity(&self) -> usize

Returns the capacity of the buffer.

Source

pub fn push_back(&mut self, item: T) -> Result<()>

Pushes an element to the back of the buffer.

If the buffer is full, Error::ResourceExhausted is returned.

Source

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

Removes an element from the front of the buffer (dequeue).

Returns None if the buffer is empty.

Trait Implementations§

Source§

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

Source§

fn default() -> Self

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

impl<T, const N: usize> Drop for CircularBuffer<T, N>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<T, const N: usize> Forget for CircularBuffer<T, N>

§

impl<T, const N: usize> Freeze for CircularBuffer<T, N>
where [MaybeUninit<T>; N]: Freeze,

§

impl<T, const N: usize> Move for CircularBuffer<T, N>

§

impl<T, const N: usize> RefUnwindSafe for CircularBuffer<T, N>
where [MaybeUninit<T>; N]: RefUnwindSafe,

§

impl<T, const N: usize> Send for CircularBuffer<T, N>
where [MaybeUninit<T>; N]: Send,

§

impl<T, const N: usize> Sync for CircularBuffer<T, N>
where [MaybeUninit<T>; N]: Sync,

§

impl<T, const N: usize> Unpin for CircularBuffer<T, N>
where [MaybeUninit<T>; N]: Unpin,

§

impl<T, const N: usize> UnsafeUnpin for CircularBuffer<T, N>
where [MaybeUninit<T>; N]: UnsafeUnpin,

§

impl<T, const N: usize> UnwindSafe for CircularBuffer<T, N>
where [MaybeUninit<T>; N]: UnwindSafe,

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = !

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
§

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.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.