bit_mask_ring_buf
A fast ring buffer implementation with cheap and safe indexing written in Rust. It works by bit-masking an integer index to get the corresponding index in an array/vec whose length is a power of 2. This is best used when indexing the buffer with an isize
value. Copies/reads with slices are implemented with memcpy. This is most useful for high performance algorithms such as audio DSP.
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.
Note, this crate has not been tested in a production environment yet. If you find any bugs, especially ones pertaining to memory safety, please let me know!
Example
use BitMaskRingBuf;
// Create a ring buffer with type u32. The data will be initialized with the default
// value (0 in this case). The actual capacity will be set to the next highest power
// of 2 if the given capacity is not already a power of 2.
let mut rb = from_capacity;
assert_eq!;
// read/write to buffer by indexing
rb = 0;
rb = 1;
rb = 2;
rb = 3;
// cheaply wrap when reading/writing outside of bounds
assert_eq!;
assert_eq!;
// memcpy into slices at arbitrary points and length
let mut read_buffer = ;
rb.read_into;
assert_eq!;
// memcpy data from a slice into the ring buffer. Only
// the latest data will be copied.
rb.write_latest;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
// read/write by retreiving slices directly
let = rb.as_slices_len;
assert_eq!;
assert_eq!;