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
a Packet whose slices a and b concatenate to the full payload.
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 fn clear(&mut self)
pub fn clear(&mut self)
Empties the buffer, removing all packets and resetting indices. Does not modify the backing array, but all existing data is considered invalid and will be overwritten by future writes.
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 push_multipart(
&mut self,
) -> Result<MultipartPush<'_, N>, NotEnoughSpaceError>
pub fn push_multipart( &mut self, ) -> Result<MultipartPush<'_, N>, NotEnoughSpaceError>
Begins a multi-part push in normal mode.
Returns a MultipartPush guard whose MultipartPush::push method appends chunks of
payload. When the guard is dropped the completed packet is committed. Call
MultipartPush::cancel to discard the write.
§Errors
Returns NotEnoughSpaceError if there are fewer than 8 unused bytes (not enough for even
an empty packet).
Sourcepub fn push_multipart_force(&mut self) -> MultipartPush<'_, N>
pub fn push_multipart_force(&mut self) -> MultipartPush<'_, N>
Begins a multi-part push in force mode.
Like Self::push_multipart but drops the oldest packets as needed to make room. Dropped
packets are permanently lost even if the write is later cancelled.
Returns a MultipartPush guard. Calling MultipartPush::push will drop further old
packets on demand.
Sourcepub fn pop_front(&mut self) -> Option<Packet<'_>>
pub fn pop_front(&mut self) -> Option<Packet<'_>>
Removes and returns the oldest packet.
The payload may be split across the end of the backing array; use Packet::copy_into or
access Packet::a and Packet::b directly. If the payload is contiguous, b 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<Packet<'_>>
pub fn nth(&self, n: usize) -> Option<Packet<'_>>
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<Packet<'_>>
pub fn nth_reverse(&self, n: usize) -> Option<Packet<'_>>
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().