pub fn next_pow_of_2(n: usize) -> usize {
if n == 0 {
return 2;
}
let mut n = n - 1;
let mut res: usize = 2;
n >>= 1;
while n != 0 {
res <<= 1;
n >>= 1;
}
res
}
static MS_TO_SEC_RATIO: f64 = 1.0 / 1000.0;
#[allow(missing_debug_implementations)]
pub struct BitMaskRingBuf<T: Copy + Clone + Default> {
vec: Vec<T>,
mask: isize,
}
impl<T: Copy + Clone + Default> BitMaskRingBuf<T> {
pub fn from_capacity(capacity: usize) -> Self {
let mut new_self = Self {
vec: Vec::new(),
mask: 0,
};
new_self.set_capacity(capacity);
new_self
}
pub fn from_ms(milliseconds: f64, sample_rate: f64) -> Self {
let mut new_self = Self {
vec: Vec::new(),
mask: 0,
};
new_self.set_capacity_from_ms(milliseconds, sample_rate);
new_self
}
pub fn set_capacity(&mut self, capacity: usize) {
self.vec.clear();
self.vec.resize(next_pow_of_2(capacity), Default::default());
self.mask = (self.vec.len() as isize) - 1;
}
pub fn set_capacity_from_ms(&mut self, milliseconds: f64, sample_rate: f64) {
assert!(milliseconds >= 0.0);
assert!(sample_rate >= 0.0);
self.set_capacity((milliseconds * MS_TO_SEC_RATIO * sample_rate).ceil() as usize);
}
pub fn clear(&mut self) {
let len = self.vec.len();
self.vec.clear();
self.vec.resize(len, Default::default());
}
pub fn as_slices(&self, start: isize) -> (&[T], &[T]) {
let start = (start & self.mask) as usize;
unsafe {
let self_vec_ptr = self.vec.as_ptr();
(
&*std::ptr::slice_from_raw_parts(self_vec_ptr.add(start), self.vec.len() - start),
&*std::ptr::slice_from_raw_parts(self_vec_ptr, start),
)
}
}
pub fn as_slices_len(&self, start: isize, len: usize) -> (&[T], &[T]) {
let start = (start & self.mask) as usize;
unsafe {
let self_vec_ptr = self.vec.as_ptr();
let first_portion_len = self.vec.len() - start;
if len > first_portion_len {
let second_portion_len = std::cmp::min(len - first_portion_len, start);
(
&*std::ptr::slice_from_raw_parts(self_vec_ptr.add(start), first_portion_len),
&*std::ptr::slice_from_raw_parts(self_vec_ptr, second_portion_len),
)
} else {
(
&*std::ptr::slice_from_raw_parts(self_vec_ptr.add(start), len),
&[],
)
}
}
}
pub fn as_mut_slices(&mut self, start: isize) -> (&mut [T], &mut [T]) {
let start = (start & self.mask) as usize;
unsafe {
let self_vec_ptr = self.vec.as_mut_ptr();
(
&mut *std::ptr::slice_from_raw_parts_mut(self_vec_ptr.add(start), self.vec.len() - start),
&mut *std::ptr::slice_from_raw_parts_mut(self_vec_ptr, start),
)
}
}
pub fn as_mut_slices_len(&mut self, start: isize, len: usize) -> (&mut [T], &mut [T]) {
let start = (start & self.mask) as usize;
unsafe {
let self_vec_ptr = self.vec.as_mut_ptr();
let first_portion_len = self.vec.len() - start;
if len > first_portion_len {
let second_portion_len = std::cmp::min(len - first_portion_len, start);
(
&mut *std::ptr::slice_from_raw_parts_mut(self_vec_ptr.add(start), first_portion_len),
&mut *std::ptr::slice_from_raw_parts_mut(self_vec_ptr, second_portion_len),
)
} else {
(
&mut *std::ptr::slice_from_raw_parts_mut(self_vec_ptr.add(start), len),
&mut [],
)
}
}
}
pub fn read_into(&self, slice: &mut [T], start: isize) {
let start = self.constrain(start) as usize;
unsafe {
let self_vec_ptr = self.vec.as_ptr();
let mut slice_ptr = slice.as_mut_ptr();
let mut slice_len = slice.len();
let first_portion_len = self.vec.len() - start;
while slice_len > first_portion_len {
std::ptr::copy_nonoverlapping(self_vec_ptr.add(start), slice_ptr, first_portion_len);
slice_ptr = slice_ptr.add(first_portion_len);
slice_len -= first_portion_len;
let second_portion_len = std::cmp::min(slice_len, start);
std::ptr::copy_nonoverlapping(self_vec_ptr, slice_ptr, second_portion_len);
slice_ptr = slice_ptr.add(second_portion_len);
slice_len -= second_portion_len;
}
std::ptr::copy_nonoverlapping(self_vec_ptr.add(start), slice_ptr, slice_len);
}
}
pub fn write_latest(&mut self, slice: &[T], start: isize) {
let end_i = start + slice.len() as isize;
let slice = if slice.len() > self.vec.len() {
&slice[slice.len() - self.vec.len()..]
} else {
&slice[..]
};
let start_i = self.constrain(end_i - slice.len() as isize) as usize;
unsafe {
let slice_ptr = slice.as_ptr();
let self_vec_ptr = self.vec.as_mut_ptr();
if start_i + slice.len() > self.vec.len() {
let first_portion_len = self.vec.len() - start_i;
std::ptr::copy_nonoverlapping(slice_ptr, self_vec_ptr.add(start_i), first_portion_len);
let second_portion_len = slice.len() - first_portion_len;
std::ptr::copy_nonoverlapping(slice_ptr.add(first_portion_len), self_vec_ptr, second_portion_len);
} else {
std::ptr::copy_nonoverlapping(slice_ptr, self_vec_ptr.add(start_i), slice.len());
}
}
}
pub fn capacity(&self) -> usize {
self.vec.len()
}
pub fn constrain(&self, i: isize) -> isize {
i & self.mask
}
}
impl<T: Copy + Clone + Default> std::ops::Index<isize> for BitMaskRingBuf<T> {
type Output = T;
fn index(&self, i: isize) -> &T {
unsafe { &*self.vec.as_ptr().offset(i & self.mask) }
}
}
impl<T: Copy + Clone + Default> std::ops::IndexMut<isize> for BitMaskRingBuf<T> {
fn index_mut(&mut self, i: isize) -> &mut T {
unsafe { &mut *self.vec.as_mut_ptr().offset(i & self.mask) }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn next_pow_of_2_test() {
assert_eq!(next_pow_of_2(0), 2);
assert_eq!(next_pow_of_2(1), 2);
assert_eq!(next_pow_of_2(2), 2);
assert_eq!(next_pow_of_2(30), 32);
assert_eq!(next_pow_of_2(127), 128);
assert_eq!(next_pow_of_2(128), 128);
assert_eq!(next_pow_of_2(129), 256);
assert_eq!(next_pow_of_2(4000), 4096);
assert_eq!(next_pow_of_2(5000), 8192);
}
#[test]
fn bit_mask_ring_buf_initialize() {
let ring_buf = BitMaskRingBuf::<f32>::from_capacity(4);
assert_eq!(&ring_buf.vec[..], &[0.0, 0.0, 0.0, 0.0]);
}
#[test]
fn bit_mask_ring_buf_constrain() {
let ring_buf = BitMaskRingBuf::<f32>::from_capacity(4);
assert_eq!(ring_buf.constrain(-8), 0);
assert_eq!(ring_buf.constrain(-7), 1);
assert_eq!(ring_buf.constrain(-6), 2);
assert_eq!(ring_buf.constrain(-5), 3);
assert_eq!(ring_buf.constrain(-4), 0);
assert_eq!(ring_buf.constrain(-3), 1);
assert_eq!(ring_buf.constrain(-2), 2);
assert_eq!(ring_buf.constrain(-1), 3);
assert_eq!(ring_buf.constrain(0), 0);
assert_eq!(ring_buf.constrain(1), 1);
assert_eq!(ring_buf.constrain(2), 2);
assert_eq!(ring_buf.constrain(3), 3);
assert_eq!(ring_buf.constrain(4), 0);
assert_eq!(ring_buf.constrain(5), 1);
assert_eq!(ring_buf.constrain(6), 2);
assert_eq!(ring_buf.constrain(7), 3);
assert_eq!(ring_buf.constrain(8), 0);
}
#[test]
fn bit_mask_ring_buf_clear() {
let mut ring_buf = BitMaskRingBuf::<f32>::from_capacity(4);
ring_buf.write_latest(&[1.0f32, 2.0, 3.0, 4.0], 0);
assert_eq!(ring_buf.vec.as_slice(), &[1.0, 2.0, 3.0, 4.0]);
ring_buf.clear();
assert_eq!(ring_buf.vec.as_slice(), &[0.0, 0.0, 0.0, 0.0]);
}
#[test]
fn bit_mask_ring_buf_index() {
let mut ring_buf = BitMaskRingBuf::<f32>::from_capacity(4);
ring_buf.write_latest(&[0.0f32, 1.0, 2.0, 3.0], 0);
let ring_buf = &ring_buf;
assert_eq!(ring_buf[-8], 0.0);
assert_eq!(ring_buf[-7], 1.0);
assert_eq!(ring_buf[-6], 2.0);
assert_eq!(ring_buf[-5], 3.0);
assert_eq!(ring_buf[-4], 0.0);
assert_eq!(ring_buf[-3], 1.0);
assert_eq!(ring_buf[-2], 2.0);
assert_eq!(ring_buf[-1], 3.0);
assert_eq!(ring_buf[0], 0.0);
assert_eq!(ring_buf[1], 1.0);
assert_eq!(ring_buf[2], 2.0);
assert_eq!(ring_buf[3], 3.0);
assert_eq!(ring_buf[4], 0.0);
assert_eq!(ring_buf[5], 1.0);
assert_eq!(ring_buf[6], 2.0);
assert_eq!(ring_buf[7], 3.0);
assert_eq!(ring_buf[8], 0.0);
}
#[test]
fn bit_mask_ring_buf_index_mut() {
let mut ring_buf = BitMaskRingBuf::<f32>::from_capacity(4);
ring_buf.write_latest(&[0.0f32, 1.0, 2.0, 3.0], 0);
assert_eq!(&mut ring_buf[-8], &mut 0.0);
assert_eq!(&mut ring_buf[-7], &mut 1.0);
assert_eq!(&mut ring_buf[-6], &mut 2.0);
assert_eq!(&mut ring_buf[-5], &mut 3.0);
assert_eq!(&mut ring_buf[-4], &mut 0.0);
assert_eq!(&mut ring_buf[-3], &mut 1.0);
assert_eq!(&mut ring_buf[-2], &mut 2.0);
assert_eq!(&mut ring_buf[-1], &mut 3.0);
assert_eq!(&mut ring_buf[0], &mut 0.0);
assert_eq!(&mut ring_buf[1], &mut 1.0);
assert_eq!(&mut ring_buf[2], &mut 2.0);
assert_eq!(&mut ring_buf[3], &mut 3.0);
assert_eq!(&mut ring_buf[4], &mut 0.0);
assert_eq!(&mut ring_buf[5], &mut 1.0);
assert_eq!(&mut ring_buf[6], &mut 2.0);
assert_eq!(&mut ring_buf[7], &mut 3.0);
assert_eq!(&mut ring_buf[8], &mut 0.0);
}
#[test]
fn bit_mask_ring_buf_as_slices() {
let mut ring_buf = BitMaskRingBuf::<f32>::from_capacity(4);
ring_buf.write_latest(&[1.0f32, 2.0, 3.0, 4.0], 0);
let (s1, s2) = ring_buf.as_slices(0);
assert_eq!(s1, &[1.0, 2.0, 3.0, 4.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_slices(1);
assert_eq!(s1, &[2.0, 3.0, 4.0]);
assert_eq!(s2, &[1.0]);
let (s1, s2) = ring_buf.as_slices(2);
assert_eq!(s1, &[3.0, 4.0]);
assert_eq!(s2, &[1.0, 2.0]);
let (s1, s2) = ring_buf.as_slices(3);
assert_eq!(s1, &[4.0]);
assert_eq!(s2, &[1.0, 2.0, 3.0]);
let (s1, s2) = ring_buf.as_slices(4);
assert_eq!(s1, &[1.0, 2.0, 3.0, 4.0]);
assert_eq!(s2, &[]);
}
#[test]
fn bit_mask_ring_buf_as_mut_slices() {
let mut ring_buf = BitMaskRingBuf::<f32>::from_capacity(4);
ring_buf.write_latest(&[1.0f32, 2.0, 3.0, 4.0], 0);
let (s1, s2) = ring_buf.as_mut_slices(0);
assert_eq!(s1, &[1.0, 2.0, 3.0, 4.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_mut_slices(1);
assert_eq!(s1, &[2.0, 3.0, 4.0]);
assert_eq!(s2, &[1.0]);
let (s1, s2) = ring_buf.as_mut_slices(2);
assert_eq!(s1, &[3.0, 4.0]);
assert_eq!(s2, &[1.0, 2.0]);
let (s1, s2) = ring_buf.as_mut_slices(3);
assert_eq!(s1, &[4.0]);
assert_eq!(s2, &[1.0, 2.0, 3.0]);
let (s1, s2) = ring_buf.as_mut_slices(4);
assert_eq!(s1, &[1.0, 2.0, 3.0, 4.0]);
assert_eq!(s2, &[]);
}
#[repr(C, align(1))]
struct Aligned1([f32; 8]);
#[repr(C, align(2))]
struct Aligned2([f32; 8]);
#[repr(C, align(4))]
struct Aligned4([f32; 8]);
#[repr(C, align(8))]
struct Aligned8([f32; 8]);
#[repr(C, align(16))]
struct Aligned16([f32; 8]);
#[repr(C, align(32))]
struct Aligned32([f32; 8]);
#[repr(C, align(64))]
struct Aligned64([f32; 8]);
#[test]
fn bit_mask_ring_buf_write_latest() {
let mut ring_buf = BitMaskRingBuf::<f32>::from_capacity(4);
let input = [0.0f32, 1.0, 2.0, 3.0];
ring_buf.write_latest(&input, 0);
assert_eq!(ring_buf.vec.as_slice(), &[0.0, 1.0, 2.0, 3.0]);
ring_buf.write_latest(&input, 1);
assert_eq!(ring_buf.vec.as_slice(), &[3.0, 0.0, 1.0, 2.0]);
ring_buf.write_latest(&input, 2);
assert_eq!(ring_buf.vec.as_slice(), &[2.0, 3.0, 0.0, 1.0]);
ring_buf.write_latest(&input, 3);
assert_eq!(ring_buf.vec.as_slice(), &[1.0, 2.0, 3.0, 0.0]);
ring_buf.write_latest(&input, 4);
assert_eq!(ring_buf.vec.as_slice(), &[0.0, 1.0, 2.0, 3.0]);
let input = [0.0f32, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0];
ring_buf.write_latest(&input, 0);
assert_eq!(ring_buf.vec.as_slice(), &[4.0, 5.0, 6.0, 7.0]);
ring_buf.write_latest(&input, 1);
assert_eq!(ring_buf.vec.as_slice(), &[7.0, 4.0, 5.0, 6.0]);
ring_buf.write_latest(&input, 2);
assert_eq!(ring_buf.vec.as_slice(), &[6.0, 7.0, 4.0, 5.0]);
ring_buf.write_latest(&input, 3);
assert_eq!(ring_buf.vec.as_slice(), &[5.0, 6.0, 7.0, 4.0]);
ring_buf.write_latest(&input, 4);
assert_eq!(ring_buf.vec.as_slice(), &[4.0, 5.0, 6.0, 7.0]);
let input = [0.0f32, 1.0];
ring_buf.write_latest(&input, 0);
assert_eq!(ring_buf.vec.as_slice(), &[0.0, 1.0, 6.0, 7.0]);
ring_buf.write_latest(&input, 1);
assert_eq!(ring_buf.vec.as_slice(), &[0.0, 0.0, 1.0, 7.0]);
ring_buf.write_latest(&input, 2);
assert_eq!(ring_buf.vec.as_slice(), &[0.0, 0.0, 0.0, 1.0]);
ring_buf.write_latest(&input, 3);
assert_eq!(ring_buf.vec.as_slice(), &[1.0, 0.0, 0.0, 0.0]);
ring_buf.write_latest(&input, 4);
assert_eq!(ring_buf.vec.as_slice(), &[0.0, 1.0, 0.0, 0.0]);
let aligned_input = Aligned1([8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0]);
ring_buf.write_latest(&aligned_input.0, 0);
assert_eq!(ring_buf.vec.as_slice(), &[12.0, 13.0, 14.0, 15.0]);
let aligned_input = Aligned2([8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0]);
ring_buf.write_latest(&aligned_input.0, 0);
assert_eq!(ring_buf.vec.as_slice(), &[12.0, 13.0, 14.0, 15.0]);
let aligned_input = Aligned4([8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0]);
ring_buf.write_latest(&aligned_input.0, 0);
assert_eq!(ring_buf.vec.as_slice(), &[12.0, 13.0, 14.0, 15.0]);
let aligned_input = Aligned8([8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0]);
ring_buf.write_latest(&aligned_input.0, 0);
assert_eq!(ring_buf.vec.as_slice(), &[12.0, 13.0, 14.0, 15.0]);
let aligned_input = Aligned16([8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0]);
ring_buf.write_latest(&aligned_input.0, 0);
assert_eq!(ring_buf.vec.as_slice(), &[12.0, 13.0, 14.0, 15.0]);
let aligned_input = Aligned32([8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0]);
ring_buf.write_latest(&aligned_input.0, 0);
assert_eq!(ring_buf.vec.as_slice(), &[12.0, 13.0, 14.0, 15.0]);
let aligned_input = Aligned64([8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0]);
ring_buf.write_latest(&aligned_input.0, 0);
assert_eq!(ring_buf.vec.as_slice(), &[12.0, 13.0, 14.0, 15.0]);
}
#[test]
fn bit_mask_ring_buf_as_slices_len() {
let mut ring_buf = BitMaskRingBuf::<f32>::from_capacity(4);
ring_buf.write_latest(&[0.0, 1.0, 2.0, 3.0], 0);
let (s1, s2) = ring_buf.as_slices_len(0, 0);
assert_eq!(s1, &[]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_slices_len(0, 1);
assert_eq!(s1, &[0.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_slices_len(0, 2);
assert_eq!(s1, &[0.0, 1.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_slices_len(0, 3);
assert_eq!(s1, &[0.0, 1.0, 2.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_slices_len(0, 4);
assert_eq!(s1, &[0.0, 1.0, 2.0, 3.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_slices_len(0, 5);
assert_eq!(s1, &[0.0, 1.0, 2.0, 3.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_slices_len(1, 0);
assert_eq!(s1, &[]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_slices_len(1, 1);
assert_eq!(s1, &[1.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_slices_len(1, 2);
assert_eq!(s1, &[1.0, 2.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_slices_len(1, 3);
assert_eq!(s1, &[1.0, 2.0, 3.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_slices_len(1, 4);
assert_eq!(s1, &[1.0, 2.0, 3.0]);
assert_eq!(s2, &[0.0]);
let (s1, s2) = ring_buf.as_slices_len(1, 5);
assert_eq!(s1, &[1.0, 2.0, 3.0]);
assert_eq!(s2, &[0.0]);
let (s1, s2) = ring_buf.as_slices_len(2, 0);
assert_eq!(s1, &[]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_slices_len(2, 1);
assert_eq!(s1, &[2.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_slices_len(2, 2);
assert_eq!(s1, &[2.0, 3.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_slices_len(2, 3);
assert_eq!(s1, &[2.0, 3.0]);
assert_eq!(s2, &[0.0]);
let (s1, s2) = ring_buf.as_slices_len(2, 4);
assert_eq!(s1, &[2.0, 3.0]);
assert_eq!(s2, &[0.0, 1.0]);
let (s1, s2) = ring_buf.as_slices_len(2, 5);
assert_eq!(s1, &[2.0, 3.0]);
assert_eq!(s2, &[0.0, 1.0]);
let (s1, s2) = ring_buf.as_slices_len(3, 0);
assert_eq!(s1, &[]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_slices_len(3, 1);
assert_eq!(s1, &[3.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_slices_len(3, 2);
assert_eq!(s1, &[3.0]);
assert_eq!(s2, &[0.0]);
let (s1, s2) = ring_buf.as_slices_len(3, 3);
assert_eq!(s1, &[3.0]);
assert_eq!(s2, &[0.0, 1.0]);
let (s1, s2) = ring_buf.as_slices_len(3, 4);
assert_eq!(s1, &[3.0]);
assert_eq!(s2, &[0.0, 1.0, 2.0]);
let (s1, s2) = ring_buf.as_slices_len(3, 5);
assert_eq!(s1, &[3.0]);
assert_eq!(s2, &[0.0, 1.0, 2.0]);
let (s1, s2) = ring_buf.as_slices_len(4, 0);
assert_eq!(s1, &[]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_slices_len(4, 1);
assert_eq!(s1, &[0.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_slices_len(4, 2);
assert_eq!(s1, &[0.0, 1.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_slices_len(4, 3);
assert_eq!(s1, &[0.0, 1.0, 2.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_slices_len(4, 4);
assert_eq!(s1, &[0.0, 1.0, 2.0, 3.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_slices_len(4, 5);
assert_eq!(s1, &[0.0, 1.0, 2.0, 3.0]);
assert_eq!(s2, &[]);
}
#[test]
fn bit_mask_ring_buf_as_mut_slices_len() {
let mut ring_buf = BitMaskRingBuf::<f32>::from_capacity(4);
ring_buf.write_latest(&[0.0, 1.0, 2.0, 3.0], 0);
let (s1, s2) = ring_buf.as_mut_slices_len(0, 0);
assert_eq!(s1, &[]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_mut_slices_len(0, 1);
assert_eq!(s1, &[0.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_mut_slices_len(0, 2);
assert_eq!(s1, &[0.0, 1.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_mut_slices_len(0, 3);
assert_eq!(s1, &[0.0, 1.0, 2.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_mut_slices_len(0, 4);
assert_eq!(s1, &[0.0, 1.0, 2.0, 3.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_mut_slices_len(0, 5);
assert_eq!(s1, &[0.0, 1.0, 2.0, 3.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_mut_slices_len(1, 0);
assert_eq!(s1, &[]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_mut_slices_len(1, 1);
assert_eq!(s1, &[1.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_mut_slices_len(1, 2);
assert_eq!(s1, &[1.0, 2.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_mut_slices_len(1, 3);
assert_eq!(s1, &[1.0, 2.0, 3.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_mut_slices_len(1, 4);
assert_eq!(s1, &[1.0, 2.0, 3.0]);
assert_eq!(s2, &[0.0]);
let (s1, s2) = ring_buf.as_mut_slices_len(1, 5);
assert_eq!(s1, &[1.0, 2.0, 3.0]);
assert_eq!(s2, &[0.0]);
let (s1, s2) = ring_buf.as_mut_slices_len(2, 0);
assert_eq!(s1, &[]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_mut_slices_len(2, 1);
assert_eq!(s1, &[2.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_mut_slices_len(2, 2);
assert_eq!(s1, &[2.0, 3.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_mut_slices_len(2, 3);
assert_eq!(s1, &[2.0, 3.0]);
assert_eq!(s2, &[0.0]);
let (s1, s2) = ring_buf.as_mut_slices_len(2, 4);
assert_eq!(s1, &[2.0, 3.0]);
assert_eq!(s2, &[0.0, 1.0]);
let (s1, s2) = ring_buf.as_mut_slices_len(2, 5);
assert_eq!(s1, &[2.0, 3.0]);
assert_eq!(s2, &[0.0, 1.0]);
let (s1, s2) = ring_buf.as_mut_slices_len(3, 0);
assert_eq!(s1, &[]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_mut_slices_len(3, 1);
assert_eq!(s1, &[3.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_mut_slices_len(3, 2);
assert_eq!(s1, &[3.0]);
assert_eq!(s2, &[0.0]);
let (s1, s2) = ring_buf.as_mut_slices_len(3, 3);
assert_eq!(s1, &[3.0]);
assert_eq!(s2, &[0.0, 1.0]);
let (s1, s2) = ring_buf.as_mut_slices_len(3, 4);
assert_eq!(s1, &[3.0]);
assert_eq!(s2, &[0.0, 1.0, 2.0]);
let (s1, s2) = ring_buf.as_mut_slices_len(3, 5);
assert_eq!(s1, &[3.0]);
assert_eq!(s2, &[0.0, 1.0, 2.0]);
let (s1, s2) = ring_buf.as_mut_slices_len(4, 0);
assert_eq!(s1, &[]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_mut_slices_len(4, 1);
assert_eq!(s1, &[0.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_mut_slices_len(4, 2);
assert_eq!(s1, &[0.0, 1.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_mut_slices_len(4, 3);
assert_eq!(s1, &[0.0, 1.0, 2.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_mut_slices_len(4, 4);
assert_eq!(s1, &[0.0, 1.0, 2.0, 3.0]);
assert_eq!(s2, &[]);
let (s1, s2) = ring_buf.as_mut_slices_len(4, 5);
assert_eq!(s1, &[0.0, 1.0, 2.0, 3.0]);
assert_eq!(s2, &[]);
}
#[test]
fn bit_mask_ring_buf_read_into() {
let mut ring_buf = BitMaskRingBuf::<f32>::from_capacity(4);
ring_buf.write_latest(&[0.0, 1.0, 2.0, 3.0], 0);
let mut output = [0.0f32; 4];
ring_buf.read_into(&mut output, 0);
assert_eq!(output, [0.0, 1.0, 2.0, 3.0]);
ring_buf.read_into(&mut output, 1);
assert_eq!(output, [1.0, 2.0, 3.0, 0.0]);
ring_buf.read_into(&mut output, 2);
assert_eq!(output, [2.0, 3.0, 0.0, 1.0]);
ring_buf.read_into(&mut output, 3);
assert_eq!(output, [3.0, 0.0, 1.0, 2.0]);
ring_buf.read_into(&mut output, 4);
assert_eq!(output, [0.0, 1.0, 2.0, 3.0]);
let mut output = [0.0f32; 3];
ring_buf.read_into(&mut output, 0);
assert_eq!(output, [0.0, 1.0, 2.0]);
ring_buf.read_into(&mut output, 1);
assert_eq!(output, [1.0, 2.0, 3.0]);
ring_buf.read_into(&mut output, 2);
assert_eq!(output, [2.0, 3.0, 0.0]);
ring_buf.read_into(&mut output, 3);
assert_eq!(output, [3.0, 0.0, 1.0]);
ring_buf.read_into(&mut output, 4);
assert_eq!(output, [0.0, 1.0, 2.0]);
let mut output = [0.0f32; 5];
ring_buf.read_into(&mut output, 0);
assert_eq!(output, [0.0, 1.0, 2.0, 3.0, 0.0]);
ring_buf.read_into(&mut output, 1);
assert_eq!(output, [1.0, 2.0, 3.0, 0.0, 1.0]);
ring_buf.read_into(&mut output, 2);
assert_eq!(output, [2.0, 3.0, 0.0, 1.0, 2.0]);
ring_buf.read_into(&mut output, 3);
assert_eq!(output, [3.0, 0.0, 1.0, 2.0, 3.0]);
ring_buf.read_into(&mut output, 4);
assert_eq!(output, [0.0, 1.0, 2.0, 3.0, 0.0]);
let mut output = [0.0f32; 10];
ring_buf.read_into(&mut output, 0);
assert_eq!(output, [0.0, 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0, 0.0, 1.0]);
ring_buf.read_into(&mut output, 3);
assert_eq!(output, [3.0, 0.0, 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0, 0.0]);
let mut aligned_output = Aligned1([0.0; 8]);
ring_buf.read_into(&mut aligned_output.0, 0);
assert_eq!(aligned_output.0, [0.0, 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0]);
let mut aligned_output = Aligned2([0.0; 8]);
ring_buf.read_into(&mut aligned_output.0, 0);
assert_eq!(aligned_output.0, [0.0, 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0]);
let mut aligned_output = Aligned4([0.0; 8]);
ring_buf.read_into(&mut aligned_output.0, 0);
assert_eq!(aligned_output.0, [0.0, 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0]);
let mut aligned_output = Aligned8([0.0; 8]);
ring_buf.read_into(&mut aligned_output.0, 0);
assert_eq!(aligned_output.0, [0.0, 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0]);
let mut aligned_output = Aligned16([0.0; 8]);
ring_buf.read_into(&mut aligned_output.0, 0);
assert_eq!(aligned_output.0, [0.0, 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0]);
let mut aligned_output = Aligned32([0.0; 8]);
ring_buf.read_into(&mut aligned_output.0, 0);
assert_eq!(aligned_output.0, [0.0, 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0]);
let mut aligned_output = Aligned64([0.0; 8]);
ring_buf.read_into(&mut aligned_output.0, 0);
assert_eq!(aligned_output.0, [0.0, 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0]);
}
}