Bytearray-Ringbuffer
An embedded-friendly VecDeque<Vec<u8>>.
BytearrayRingbuffer stores byte slices in a fixed-size array, similar to heapless::Vec.
However, the elements in BytearrayRingbuffer are not required to have a fixed size.
Instead, information about each slice (ie. length) is stored alongside the payload.
Each slice data uses up data.len() + 8 bytes in the buffer.
This is useful for efficiently storing elements of very different lengths, as short elements do not have to be padded.
One downside is that elements may always wrap around at the end of the buffer.
When reading, therefore, always two slices (a, b) are returned.
If the element does not wrap around, a will contain all data and b will be empty.
Otherwise, a and b have to be concatenated to yield the full result.
Usage
use BytearrayRingbuffer;
// Create a buffer with a capacity of 64 bytes.
let mut buffer = new;
// store some packets
buffer.push.unwrap;
buffer.push.unwrap;
buffer.push.unwrap;
// number of packets
assert_eq!;
// retrieve
for in buffer.iter
Performance
The tradeoff here is that elements can't be accessed without iterating through the buffer.
| operation | time complexity |
|---|---|
push() |
O(1) |
pop() |
O(1) |
iter() |
O(1) |
count() |
O(1) |
nth(n) |
O(N) |