tui_logger

Struct CircularBuffer

Source
pub struct CircularBuffer<T> { /* private fields */ }
Expand description

CircularBuffer is used to store the last elements of an endless sequence. Oldest elements will be overwritten. The implementation focus on speed. So memory allocations are avoided.

Usage example:

 extern crate tui_logger;

 use tui_logger::CircularBuffer;

 let mut cb : CircularBuffer<u64> = CircularBuffer::new(5);
 cb.push(1);
 cb.push(2);
 cb.push(3);
 cb.push(4);
 cb.push(5);
 cb.push(6); // This will overwrite the first element

 // Total elements pushed into the buffer is 6.
 assert_eq!(6,cb.total_elements());

 // Thus the buffer has wrapped around.
 assert_eq!(true,cb.has_wrapped());

 /// Iterate through the elements:
 {
     let mut iter = cb.iter();
     assert_eq!(Some(&2), iter.next());
     assert_eq!(Some(&3), iter.next());
     assert_eq!(Some(&4), iter.next());
     assert_eq!(Some(&5), iter.next());
     assert_eq!(Some(&6), iter.next());
     assert_eq!(None, iter.next());
 }

 /// Iterate backwards through the elements:
 {
     let mut iter = cb.rev_iter();
     assert_eq!(Some(&6), iter.next());
     assert_eq!(Some(&5), iter.next());
     assert_eq!(Some(&4), iter.next());
     assert_eq!(Some(&3), iter.next());
     assert_eq!(Some(&2), iter.next());
     assert_eq!(None, iter.next());
 }

 // The elements in the buffer are now:
 assert_eq!(vec![2,3,4,5,6],cb.take());

 // After taking all elements, the buffer is empty.
 let now_empty : Vec<u64> = vec![];
 assert_eq!(now_empty,cb.take());

Implementations§

Source§

impl<T> CircularBuffer<T>

Source

pub fn new(max_depth: usize) -> CircularBuffer<T>

Create a new CircularBuffer, which can hold max_depth elements

Source

pub fn len(&self) -> usize

Return the number of elements present in the buffer

Source

pub fn is_empty(&self) -> bool

Source

pub fn capacity(&self) -> usize

Source

pub fn push(&mut self, elem: T)

Push a new element into the buffer. Until the capacity is reached, elements are pushed. Afterwards the oldest elements will be overwritten.

Source

pub fn take(&mut self) -> Vec<T>

Take out all elements from the buffer, leaving an empty buffer behind

Source

pub fn total_elements(&self) -> usize

Total number of elements pushed into the buffer.

Source

pub fn has_wrapped(&self) -> bool

If has_wrapped() is true, then elements have been overwritten

Source

pub fn iter(&mut self) -> Chain<Iter<'_, T>, Iter<'_, T>>

Return an iterator to step through all elements in the sequence, as these have been pushed (FIFO)

Source

pub fn rev_iter(&mut self) -> Chain<Rev<Iter<'_, T>>, Rev<Iter<'_, T>>>

Return an iterator to step through all elements in the reverse sequence, as these have been pushed (LIFO)

Auto Trait Implementations§

§

impl<T> Freeze for CircularBuffer<T>

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for CircularBuffer<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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. 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.