Skip to main content

RingBuf

Struct RingBuf 

Source
pub struct RingBuf<T: Copy, const SIZE: usize> { /* private fields */ }
Expand description

A Circular / Ring buffer.

A fixed-capacity FIFO (First-In-First-Out) data structure that wraps around when it reaches the end of its backing array. Elements are added at the head and removed from the tail.

§Capacity

The buffer is considered ‘full’ when it contains SIZE - 1 elements. This design choice simplifies the empty/full detection logic.

§Type Parameters

  • T: The type of elements stored (must be Copy)
  • SIZE: The size of the backing array (usable capacity is SIZE - 1)

§Examples

use planck_noalloc::ringbuf::RingBuf;

let mut buf = RingBuf::<i32, 16>::new();

buf.push(10);
buf.push(20);
buf.push(30);

assert_eq!(buf.pop(), Some(10));
assert_eq!(buf.pop(), Some(20));
assert_eq!(buf.len(), 1);

Implementations§

Source§

impl<T, const N: usize> RingBuf<T, N>
where T: Copy,

Source

pub const SIZE: usize = N

The total size of the backing array. The usable capacity is SIZE - 1.

Source

pub const fn new() -> Self

Create a new ringbuf with no data inside

This method does not allocate memory.

§Example
use planck_noalloc::ringbuf::RingBuf;

// Create an empty ringbuf
let ringbuf = RingBuf::<u8, 8>::new();
assert!(ringbuf.is_empty());
Source

pub const fn is_empty(&self) -> bool

Returns true if the ring buffer is empty

§Example
use planck_noalloc::ringbuf::RingBuf;

// Create an empty ringbuf
let mut ringbuf = RingBuf::<u8, 8>::new();
assert!(ringbuf.is_empty());
ringbuf.push(42);
assert!(!ringbuf.is_empty());
Source

pub const fn len(&self) -> usize

Returns the length of the used buffer

§Example
use planck_noalloc::ringbuf::RingBuf;

// Create an empty ringbuf
let mut ringbuf = RingBuf::<u8, 8>::new();
assert_eq!(ringbuf.len(), 0);
ringbuf.push(1);
ringbuf.push(2);
ringbuf.push(3);
assert_eq!(ringbuf.len(), 3);
ringbuf.pop();
assert_eq!(ringbuf.len(), 2);
Source

pub const fn is_full(&self) -> bool

Returns true if the buffer is full

§Example
use planck_noalloc::ringbuf::RingBuf;

// Create an empty ringbuf
let mut ringbuf = RingBuf::<u8, 8>::new();
for i in 0..6 {
    ringbuf.push(i);
}
assert!(!ringbuf.is_full());
ringbuf.push(6);
assert!(ringbuf.is_full());
Source

pub const fn max_capacity(&self) -> usize

Returns the maximum number of elements that can be stored in the ringbuf This is calculated as the size subtracted 1

Source

pub fn try_push(&mut self, x: T) -> Result<(), T>

Pushes (or enqueues) an element on the ring buffer

§Errors

Returns an error with the pushed value if the ringbuf is full

Source

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

Pushes (or enqueues) an element on the ring buffer

§Panics

Panics if the ringbuf is full. If this is not what you want, see RingBuf::try_push or RingBuf::push_unchecked.

Source

pub unsafe fn push_unchecked(&mut self, x: T)

Pushes (or enqueues) an element on the ring buffer

§Safety

This does not check if it is out of bounds, which may cause data to be overwritten

Source

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

Pops (or dequeues) an element off the ring buffer

Returns none if the ringbuf is empty

Trait Implementations§

Source§

impl<T: Clone + Copy, const SIZE: usize> Clone for RingBuf<T, SIZE>

Source§

fn clone(&self) -> RingBuf<T, SIZE>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T, const N: usize> Default for RingBuf<T, N>
where T: Copy,

Source§

fn default() -> Self

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

impl<T: Copy + Copy, const SIZE: usize> Copy for RingBuf<T, SIZE>

Auto Trait Implementations§

§

impl<T, const SIZE: usize> Freeze for RingBuf<T, SIZE>
where T: Freeze,

§

impl<T, const SIZE: usize> RefUnwindSafe for RingBuf<T, SIZE>
where T: RefUnwindSafe,

§

impl<T, const SIZE: usize> Send for RingBuf<T, SIZE>
where T: Send,

§

impl<T, const SIZE: usize> Sync for RingBuf<T, SIZE>
where T: Sync,

§

impl<T, const SIZE: usize> Unpin for RingBuf<T, SIZE>
where T: Unpin,

§

impl<T, const SIZE: usize> UnsafeUnpin for RingBuf<T, SIZE>
where T: UnsafeUnpin,

§

impl<T, const SIZE: usize> UnwindSafe for RingBuf<T, SIZE>
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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.