pub struct RollingBuffer<T>where
T: Clone,{ /* private fields */ }Expand description
RollingBuffer is a fixed size heap buffer that will override the beginning of the buffer when it is full RollingBuffer is a very simple Vec wrapper that only uses safe code.
[‘size’]: size is the maximum number of elements that the buffer can hold [‘vec’]: vec is the underlying Vec that stores the elements of the buffer [‘last_removed’]: last_removed is the last element that was removed from the buffer [‘count’]: count is the number of elements in the buffer as if the buffer was Vec
Trait Implementations§
Source§impl<T> Clone for RollingBuffer<T>
impl<T> Clone for RollingBuffer<T>
Source§fn clone(&self) -> RollingBuffer<T>
fn clone(&self) -> RollingBuffer<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T> Debug for RollingBuffer<T>
impl<T> Debug for RollingBuffer<T>
Source§impl<T> Default for RollingBuffer<T>
impl<T> Default for RollingBuffer<T>
Source§fn default() -> RollingBuffer<T>
fn default() -> RollingBuffer<T>
Source§impl<T> Rolling<T> for RollingBuffer<T>
impl<T> Rolling<T> for RollingBuffer<T>
Source§fn new(size: usize) -> Self
fn new(size: usize) -> Self
Creates a new RollingBuffer with the given size and initial value (aka none) If the size is 0, the buffer will behave as a normal Vec
Source§fn push(&mut self, value: T)
fn push(&mut self, value: T)
Adds an element to the buffer, overriding the beginning of the buffer when it is full Here using “safe code”, but it is essentially unsafe ptr::write()
Source§fn get(&self, i: usize) -> Option<&T>
fn get(&self, i: usize) -> Option<&T>
Get the element at the given index, as if the buffer was a Vec
buffer of size 3, adding 1,2,3,4 and asking for the element at index 3 will return 4. Asking for index 0 will return None since this element was overriden already. Example:
let mut buffer = RollingBuffer::<i32>::new(3, 0);
buffer.push(1);
buffer.push(2);
buffer.push(3);
buffer.push(4);
assert_eq!(buffer.get(3), Some(&4));
assert_eq!(buffer.get(0), None);Source§fn last(&self) -> Option<&T>
fn last(&self) -> Option<&T>
Returns an option containing a reference to the first element in the rolling data.
If no elements have been added (count is zero), it returns None.
Otherwise, it returns a reference to the last added element.
The index calculation considers the possibility of wrapping around when
the number of elements added exceeds the size of the vec.
Source§fn first(&self) -> Option<&T>
fn first(&self) -> Option<&T>
Returns the theoretical first element.
Example:
let mut buffer = RollingBuffer::<i32>::new(3);
buffer.push(1);
buffer.push(2);
buffer.push(3);
buffer.push(4);
assert_eq!(buffer.first(), Some(&2));Source§fn raw(&self) -> &Vec<T>
fn raw(&self) -> &Vec<T>
Returns the underlying vector as it is stored inside the RollingBuffer.
Source§fn last_removed(&self) -> &Option<T>
fn last_removed(&self) -> &Option<T>
Returns the last removed element. Can be very useful if needed for debugging or other purposes.