bytearray-ringbuffer 0.3.0

a no_std and no-alloc ring buffer for variable-length byte slices in rust
Documentation
  • Coverage
  • 70.59%
    12 out of 17 items documented1 out of 17 items with examples
  • Size
  • Source code size: 35.31 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 3.08 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 13s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • martin2250/bytearray-ringbuffer
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • martin2250

crates.io crates.io

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 bytearray_ringbuffer::BytearrayRingbuffer;
// Create a buffer with a capacity of 64 bytes.
let mut buffer = BytearrayRingbuffer::<64>::new();
// store some packets
buffer.push(b"hello world").unwrap();
buffer.push(b"").unwrap();
buffer.push(b"testing").unwrap();
// number of packets
assert_eq!(buffer.count(), 3);
// retrieve 
for (a, b) in buffer.iter() {
    // a and b are &[u8]
    let mut output = Vec::new();
    output.extend_from_slice(a);
    output.extend_from_slice(b);
    // now output contains the original elements
}

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)