pub struct RingBuffer<T, const N: usize>(/* private fields */);Expand description
A ring Buffer with constant size. Makes use of a fixed-size array internally.
let mut circ_buffer = RingBuffer::<i64, 4>::default();
// These entries will be made into free space in the buffer.
circ_buffer.push(1_i64);
circ_buffer.push(2_i64);
circ_buffer.push(3_i64);
circ_buffer.push(4_i64);
// Now it will start truncating initial entries.
circ_buffer.push(5_i64);
circ_buffer.push(6_i64);
circ_buffer.push(7_i64);
let mut elements = circ_buffer.iter();
assert_eq!(elements.next(), Some(&4));
assert_eq!(elements.next(), Some(&5));
assert_eq!(elements.next(), Some(&6));
assert_eq!(elements.next(), Some(&7));Implementations§
Source§impl<T, const N: usize> RingBuffer<T, N>
impl<T, const N: usize> RingBuffer<T, N>
Source§impl<T, const N: usize> RingBuffer<T, N>
impl<T, const N: usize> RingBuffer<T, N>
Sourcepub fn push(&mut self, new_item: T)
pub fn push(&mut self, new_item: T)
Append one element to the buffer.
This will not grow the buffer but instead replace existing entries when the maximum size is reached.
let mut circ_buffer = RingBuffer::<f64, 5>::default();
circ_buffer.push(1.0);
circ_buffer.push(2.0);
circ_buffer.push(3.0);
circ_buffer.push(4.0);
circ_buffer.push(5.0);
// Now we begin to drop the first entry when pushing more values.
circ_buffer.push(6.0);
let elements = circ_buffer.iter().collect::<Vec<_>>();
assert_eq!(elements, vec![&2.0, &3.0, &4.0, &5.0, &6.0])Sourcepub fn iter<'a>(&'a self) -> RingBufferIterRef<'a, T, N> ⓘ
pub fn iter<'a>(&'a self) -> RingBufferIterRef<'a, T, N> ⓘ
Iterate over references to elements of the RingBuffer.
Trait Implementations§
Source§impl<T: Clone, const N: usize> Clone for RingBuffer<T, N>
impl<T: Clone, const N: usize> Clone for RingBuffer<T, N>
Source§fn clone(&self) -> RingBuffer<T, N>
fn clone(&self) -> RingBuffer<T, N>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<T, const N: usize> Default for RingBuffer<T, N>
impl<T, const N: usize> Default for RingBuffer<T, N>
Source§impl<T, const N: usize> IntoIterator for RingBuffer<T, N>
impl<T, const N: usize> IntoIterator for RingBuffer<T, N>
Auto Trait Implementations§
impl<T, const N: usize> Freeze for RingBuffer<T, N>where
T: Freeze,
impl<T, const N: usize> RefUnwindSafe for RingBuffer<T, N>where
T: RefUnwindSafe,
impl<T, const N: usize> Send for RingBuffer<T, N>where
T: Send,
impl<T, const N: usize> Sync for RingBuffer<T, N>where
T: Sync,
impl<T, const N: usize> Unpin for RingBuffer<T, N>where
T: Unpin,
impl<T, const N: usize> UnwindSafe for RingBuffer<T, N>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more