Skip to main content

BytearrayRingbuffer

Struct BytearrayRingbuffer 

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

Fixed-capacity FIFO of variable-length byte slices, backed by [u8; N] with no heap allocation.

Each stored packet uses data.len() + 8 bytes: a leading u32 length (native endian), the payload, then the same length again. The queue is a ring: head is where the next push writes; tail is the oldest packet. Payloads may wrap across the end of the array; most accessors return two slices (a, b) that concatenate to the full packet.

The backing array is only modified by this crate’s own logic (the field is private). Methods maintain consistent framing; Self::pop_front and iterators rely on that.

Compile-time requirements: N > 8 and N < u32::MAX (see Self::new).

Implementations§

Source§

impl<const N: usize> BytearrayRingbuffer<N>

Source

pub const fn new() -> Self

Creates an empty ring buffer.

§Panics

Panics at compile time if N <= 8 or N >= u32::MAX.

Source

pub const fn free(&self) -> usize

Returns the largest payload length that can fit in the currently unused byte range, after accounting for the 8-byte packet framing (two u32 lengths).

Computed from the unused span between write and read positions, minus 8, saturated at zero.

Source

pub fn push(&mut self, data: &[u8]) -> Result<(), NotEnoughSpaceError>

Appends data as the newest packet.

§Errors

Returns NotEnoughSpaceError if fewer than data.len() + 8 bytes are unused.

§Panics

Panics if data.len() > u32::MAX (debug assertion).

Source

pub fn push_force(&mut self, data: &[u8]) -> Result<(), NotEnoughSpaceError>

Appends data as the newest packet, dropping the oldest packets until there is room.

Unlike Self::push, this never fails for lack of space as long as a single packet can fit in the backing array (data.len() <= N - 8).

§Errors

Returns NotEnoughSpaceError only when data.len() > N - 8 (one frame cannot fit at all).

Source

pub const fn empty(&self) -> bool

Returns true if there are no packets stored.

Source

pub fn pop_front(&mut self) -> Option<(&[u8], &[u8])>

Removes and returns the oldest packet.

The payload may be split across the end of the backing array; concatenate the two slices to reconstruct data. If the payload is contiguous, the second slice is empty.

Source

pub fn iter_backwards<'a>(&'a self) -> IterBackwards<'a, N>

Borrows the buffer and yields packets from newest to oldest.

Source

pub fn iter<'a>(&'a self) -> Iter<'a, N>

Borrows the buffer and yields packets from oldest to newest.

Source

pub const fn count(&self) -> usize

Returns how many packets are stored.

Source

pub fn nth(&self, n: usize) -> Option<(&[u8], &[u8])>

Returns the n-th packet in oldest-to-newest order (n == 0 is the oldest).

Same as Iterator::nth on Self::iter.

Source

pub fn nth_reverse(&self, n: usize) -> Option<(&[u8], &[u8])>

Returns the n-th packet in newest-to-oldest order (n == 0 is the newest).

Same as Iterator::nth on Self::iter_backwards.

Source

pub fn nth_contiguous(&mut self, n: usize) -> Option<&[u8]>

Returns the n-th packet in oldest-to-newest order as a single contiguous slice.

If the payload already lies in one contiguous range of the backing array, returns that subslice. If it wraps around the end of the ring, rotates the array in place so the payload is contiguous at the front, adjusts internal indices, and returns a prefix of the array.

n == 0 is the oldest packet. Returns None if the buffer is empty or if n >= count().

Trait Implementations§

Source§

impl<const N: usize> Default for BytearrayRingbuffer<N>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<const N: usize> Freeze for BytearrayRingbuffer<N>

§

impl<const N: usize> RefUnwindSafe for BytearrayRingbuffer<N>

§

impl<const N: usize> Send for BytearrayRingbuffer<N>

§

impl<const N: usize> Sync for BytearrayRingbuffer<N>

§

impl<const N: usize> Unpin for BytearrayRingbuffer<N>

§

impl<const N: usize> UnsafeUnpin for BytearrayRingbuffer<N>

§

impl<const N: usize> UnwindSafe for BytearrayRingbuffer<N>

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.