Expanding Slice Ring Buffer

Test Documentation Crates.io License

A self-expanding ring buffer optimized for working with slices of data. Copies/reads with slices are implemented with memcpy. This algorithm attempts to use as little memcpys and allocations as possible, and only potentially shuffles data around when the capacity of the buffer is increased.

This crate is especially useful when working with streams of data where the inputs and outputs use different sizes of buffers.

This buffer cannot be shared across threads, but it could be used as a building block for a ring buffer that does.

Installation

Add expanding_slice_rb as a dependency in your Cargo.toml: toml expanding_slice_rb = 0.1

Example

```rust use expandingslicerb::ExpSliceRB;

// Create a ring buffer with type u32. There is no data in the buffer to start. // // If possible, it is a good idea to set capacity to the largest you expect the // buffer to get to avoid future memory allocations. let mut buf = ExpSliceRB::::with_capacity(3);

let data = [0u32, 1, 2];

// Memcpy data from a slice into the ring buffer. The buffer will automatically // expand to fill new data. buf.write(&data); asserteq!(buf.len(), 3); asserteq!(buf.capacity(), 3);

buf.write(&data); asserteq!(buf.len(), 6); asserteq!(buf.capacity(), 6);

// Memcpy the next chunk of data into the read slice. If the length of existing // data in the buffer is less than the length of the slice, then only that amount // of data will be copied into the front of the slice. // // This is streaming, meaning the copied data will be cleared from the buffer for // reuse, and the next call to read_into() will start copying from where the // previous call left off. If you don't want this behavior, use the peek_into() // method instead. let mut readslice = [5u32; 4]; let mut amountwritten = buf.readinto(&mut readslice); asserteq!(amountwritten, 4); asserteq!(readslice, [0u32, 1, 2, 0]);

buf.write(&data); let mut largereadslice = [5u32; 8]; amountwritten = buf.readinto(&mut largereadslice); asserteq!(amountwritten, 5); asserteq!(largeread_slice, [1u32, 2, 0, 1, 2, 5, 5, 5]); ```