RollingBuffer

Struct RollingBuffer 

Source
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>
where T: Clone + Clone,

Source§

fn clone(&self) -> RollingBuffer<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for RollingBuffer<T>
where T: Clone + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for RollingBuffer<T>
where T: Clone + Default,

Source§

fn default() -> RollingBuffer<T>

Returns the “default value” for a type. Read more
Source§

impl<T> Rolling<T> for RollingBuffer<T>
where T: Clone + Default,

Source§

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)

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>

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>

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 last_mut(&mut self) -> Option<&mut T>

Last added element’s mutable reference.

Source§

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 len(&self) -> usize

Returns theoretical len as if it was a Vec.

Source§

fn size(&self) -> usize

Returns the maximum number of elements that can be stored.

Source§

fn raw(&self) -> &Vec<T>

Returns the underlying vector as it is stored inside the RollingBuffer.

Source§

fn last_removed(&self) -> &Option<T>

Returns the last removed element. Can be very useful if needed for debugging or other purposes.

Source§

fn count(&self) -> usize

Returns ‘expected’ number of elements as if the RollingBuffer was a Vec. i.e. the number of elements that would be in the Vec if it was not a RollingBuffer.

Source§

fn is_empty(&self) -> bool

Returns true if the RollingBuffer is empty.

Source§

fn to_vec(&self) -> Vec<T>

Creates a new Vec, which contains all elements in the RollingBuffer in correct order.

Auto Trait Implementations§

§

impl<T> Freeze for RollingBuffer<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for RollingBuffer<T>
where T: RefUnwindSafe,

§

impl<T> Send for RollingBuffer<T>
where T: Send,

§

impl<T> Sync for RollingBuffer<T>
where T: Sync,

§

impl<T> Unpin for RollingBuffer<T>
where T: Unpin,

§

impl<T> UnwindSafe for RollingBuffer<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.