/// General trait for ring buffers. pub trait RingBuffer { /// Get a borrow the current item. fn current(&self) -> &T; /// Get a mutable borrow to the current item. fn current_mut(&mut self) -> &mut T; /// Move to the next item in the ring buffer. fn next(&mut self); fn current_index(&self) -> usize; } impl RingBuffer for InlineRingBuffer { fn current(&self) -> &T { &self.items[self.index] } fn current_mut(&mut self) -> &mut T { &mut self.items[self.index] } fn next(&mut self) { self.index += 1; if self.index >= SIZE { self.index = 0 } } fn current_index(&self) -> usize { self.index } } /// A ring buffer that stores its contents inline. pub struct InlineRingBuffer { items: [T; SIZE], index: usize, } impl InlineRingBuffer where T: Copy, T: Default, { pub fn new() -> Self { Self { items: [T::default(); SIZE], index: 0, } } /// Get a borrow to all the items in this ring buffer. pub fn items(&self) -> &[T; SIZE] { &self.items } /// Get a mutable borrow to all the items in this ring buffer. pub fn items_mut(&mut self) -> &mut [T; SIZE] { &mut self.items } } /// A ring buffer that stores its contents in a box pub struct BoxRingBuffer { items: Box<[T]>, index: usize, } impl BoxRingBuffer where T: Copy, T: Default, { pub fn new(size: usize) -> Self { Self { items: vec![T::default(); size].into_boxed_slice(), index: 0, } } } impl BoxRingBuffer { pub fn from_vec(items: Vec) -> Self { Self { items: items.into_boxed_slice(), index: 0, } } /// Get a borrow to all the items in this ring buffer. pub fn items(&self) -> &[T] { &self.items } /// Get a mutable borrow to all the items in this ring buffer. pub fn items_mut(&mut self) -> &mut [T] { &mut self.items } } impl From> for BoxRingBuffer { fn from(value: Vec) -> Self { BoxRingBuffer::from_vec(value) } } impl RingBuffer for BoxRingBuffer { fn current(&self) -> &T { &self.items[self.index] } fn current_mut(&mut self) -> &mut T { &mut self.items[self.index] } fn next(&mut self) { self.index += 1; if self.index >= self.items.len() { self.index = 0 } } fn current_index(&self) -> usize { self.index } }