Skip to main content

RingBuf

Struct RingBuf 

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

A ring buffer of N elements stored entirely on the stack.

Once full, new pushes overwrite the oldest entry. Iteration with iter() yields elements from oldest to newest.

§Safety note

Slots are stored as MaybeUninit<T> so that T: Default is not required. Exactly one invariant makes every read sound: the len entries ending at head have all been written by push. Every read goes through the private index helper, which addresses only that range, and every public accessor checks len before calling it. A change that lets len outrun the number of writes is undefined behaviour, not a logic bug.

Implementations§

Source§

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

Source

pub const fn new() -> Self

Create a new, empty ring buffer.

This is a const fn, so the buffer can be built in a const or static initialiser. Note that push and clear take &mut self, so a bare static RingBuf is read-only and of little use, and static mut is a hard error to reference under edition 2024. The pattern this actually enables is const-initialising the buffer inside an interior-mutability wrapper, which is how a single-owner buffer is reached from an interrupt context:

// with critical-section, cortex-m, or similar:
static LOG: Mutex<RefCell<RingBuf<u32, 64>>> =
    Mutex::new(RefCell::new(RingBuf::new()));

Without a const new that initialiser is impossible and you need a StaticCell or a OnceCell and a runtime init step.

§Capacity 0 is a build failure

The N > 0 check is a const assertion, so a zero-capacity ring cannot be constructed at all – there is no runtime panic left to catch, and therefore no way to write the negative case as a #[test] (zero_capacity_panics was deleted for exactly this reason). This compile_fail doctest is that coverage, and pinning the error code keeps it honest: a bare compile_fail would also pass on a typo.

let _ = ph_eventing::RingBuf::<u32, 0>::new();
§Panics

Does not panic.

Source

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

Append a value, overwriting the oldest entry once the ring is full.

This never fails and never blocks; if losing the oldest entry is not acceptable, use crate::EventBuf, whose push reports when full.

Source

pub fn len(&self) -> usize

Number of elements currently stored, always in 0..=N.

Source

pub fn is_empty(&self) -> bool

Returns true if the ring holds no elements.

Source

pub fn is_full(&self) -> bool

Returns true if the ring is at capacity, so the next push will overwrite the oldest entry.

Source

pub const fn capacity(&self) -> usize

Number of elements the ring can hold.

Source

pub fn clear(&mut self)

Drop every element, resetting the ring to empty.

The backing array is left as-is; only the cursors are reset, so this is O(1) and does not touch the stored values.

Source

pub fn get(&self, i: usize) -> Option<T>

Read the i-th element (0 = oldest).

Source

pub fn latest(&self) -> Option<T>

Most recently pushed element.

Source

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

Iterate over elements oldest→newest.

Trait Implementations§

Source§

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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

Source§

fn default() -> Self

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

impl<'a, T: Copy, const N: usize> IntoIterator for &'a RingBuf<T, N>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = RingIter<'a, T, N>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> RingIter<'a, T, N>

Creates an iterator from a value. Read more
Source§

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

Source§

type Error = Infallible

The error returned when the sink cannot accept the value.
Source§

fn try_push(&mut self, val: T) -> Result<(), Infallible>

Push a value into the sink. Read more

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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

§

impl<T, const N: usize> UnwindSafe for RingBuf<T, N>
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.