Slice Ring Buffer
A ring buffer implementation that is optimized for working with slices. Note this pretty much does the same thing as VecDeque
, but with the added ability to index using negative values, as well as working with buffers allocated on the stack.
This crate has no consumer/producer logic, and is meant to be used as a raw data structure or a base for other data structures.
This is optimized for manipulating data in chunks with slices. If your algorithm instead indexes elements one at a time and only uses buffers that have a size that is a power of two, then consider my crate bit_mask_ring_buf
.
Installation
Add slice_ring_buf
as a dependency in your Cargo.toml
:
= 0.2
Example
use ;
// Create a ring buffer with type u32. The data will be
// initialized with the default value (0 in this case).
let mut rb = from_len;
// Memcpy data from a slice into the ring buffer at
// arbitrary `isize` indexes. Earlier data will not be
// copied if it will be overwritten by newer data,
// avoiding unecessary memcpy's. The correct placement
// of the newer data will still be preserved.
rb.write_latest;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
// Memcpy into slices at arbitrary `isize` indexes
// and length.
let mut read_buffer = ;
rb.read_into;
assert_eq!;
// Read/write by retrieving slices directly.
let = rb.as_slices_len;
assert_eq!;
assert_eq!;
// Read/write to buffer by indexing. Performance will be
// limited by the modulo (remainder) operation on an
// `isize` value.
rb = 0;
rb = 1;
rb = 2;
rb = 3;
// Wrap when reading/writing outside of bounds.
// Performance will be limited by the modulo (remainder)
// operation on an `isize` value.
assert_eq!;
assert_eq!;
// Aligned/stack data may also be used.
let mut stack_data = ;
let mut rb_ref = new;
rb_ref = 5;
let = rb_ref.as_slices_len;
assert_eq!;
assert_eq!;