Files
ebu-plugs/dsp/src/ring_buffer.rs

39 lines
821 B
Rust

use std::ops::{Index, IndexMut};
pub struct RingBuffer<T, const N: usize> {
buffer: [T; N],
index: usize,
}
impl<T, const N: usize> RingBuffer<T, N>
where
T: Copy,
{
pub const fn new(value: T) -> Self {
Self {
buffer: [value; N],
index: 0,
}
}
pub const fn len(&self) -> usize {
N
}
pub fn shift(&mut self) {
self.index = (self.index + 1) % N;
}
}
impl<T, const N: usize> Index<usize> for RingBuffer<T, N> {
type Output = T;
fn index(&self, index: usize) -> &Self::Output {
&self.buffer[(self.index + index) % N]
}
}
impl<T, const N: usize> IndexMut<usize> for RingBuffer<T, N> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.buffer[(self.index + index) % N]
}
}