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>
impl<const N: usize> BytearrayRingbuffer<N>
Sourcepub const fn free(&self) -> usize
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.
Sourcepub fn push(&mut self, data: &[u8]) -> Result<(), NotEnoughSpaceError>
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).
Sourcepub fn push_force(&mut self, data: &[u8]) -> Result<(), NotEnoughSpaceError>
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).
Sourcepub fn pop_front(&mut self) -> Option<(&[u8], &[u8])>
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.
Sourcepub fn iter_backwards<'a>(&'a self) -> IterBackwards<'a, N> ⓘ
pub fn iter_backwards<'a>(&'a self) -> IterBackwards<'a, N> ⓘ
Borrows the buffer and yields packets from newest to oldest.
Sourcepub fn iter<'a>(&'a self) -> Iter<'a, N> ⓘ
pub fn iter<'a>(&'a self) -> Iter<'a, N> ⓘ
Borrows the buffer and yields packets from oldest to newest.
Sourcepub fn nth(&self, n: usize) -> Option<(&[u8], &[u8])>
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.
Sourcepub fn nth_reverse(&self, n: usize) -> Option<(&[u8], &[u8])>
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.
Sourcepub fn nth_contiguous(&mut self, n: usize) -> Option<&[u8]>
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().