pub struct RingBuffer<T> { /* private fields */ }Expand description
Efficient ring buffer for streaming data.
A fixed-capacity circular buffer that overwrites the oldest elements when full. Provides O(1) push operations and maintains temporal ordering.
§Example
let mut buffer = RingBuffer::<f64>::new(100);
for i in 0..150 {
buffer.push(i as f64);
}
// Buffer now contains 50..150, oldest data was overwritten
assert_eq!(buffer.len(), 100);
assert_eq!(buffer.get(0), Some(&50.0)); // Oldest remainingImplementations§
Source§impl<T: Clone + Default> RingBuffer<T>
impl<T: Clone + Default> RingBuffer<T>
Sourcepub fn from_vec(data: Vec<T>, capacity: usize) -> Self
pub fn from_vec(data: Vec<T>, capacity: usize) -> Self
Create a new ring buffer with initial data.
If the data exceeds capacity, only the last capacity elements are kept.
Sourcepub fn push(&mut self, item: T)
pub fn push(&mut self, item: T)
Push a single item to the buffer.
O(1) operation. If the buffer is full, the oldest element is overwritten.
Sourcepub fn extend<I: IntoIterator<Item = T>>(&mut self, items: I)
pub fn extend<I: IntoIterator<Item = T>>(&mut self, items: I)
Extend the buffer with multiple items.
More efficient than calling push repeatedly.
Sourcepub fn get(&self, index: usize) -> Option<&T>
pub fn get(&self, index: usize) -> Option<&T>
Get an item by logical index (0 = oldest item).
Returns None if the index is out of bounds.
Sourcepub fn get_mut(&mut self, index: usize) -> Option<&mut T>
pub fn get_mut(&mut self, index: usize) -> Option<&mut T>
Get a mutable reference to an item by logical index.
Sourcepub fn has_wrapped(&self) -> bool
pub fn has_wrapped(&self) -> bool
Check if the buffer has wrapped (oldest data was overwritten).
Sourcepub fn total_written(&self) -> u64
pub fn total_written(&self) -> u64
Get the total number of items ever written.
Sourcepub fn iter(&self) -> RingBufferIter<'_, T> ⓘ
pub fn iter(&self) -> RingBufferIter<'_, T> ⓘ
Iterate over elements in order (oldest to newest).
Trait Implementations§
Source§impl<T: Clone> Clone for RingBuffer<T>
impl<T: Clone> Clone for RingBuffer<T>
Source§fn clone(&self) -> RingBuffer<T>
fn clone(&self) -> RingBuffer<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> Debug for RingBuffer<T>
impl<T: Debug> Debug for RingBuffer<T>
Auto Trait Implementations§
impl<T> Freeze for RingBuffer<T>
impl<T> RefUnwindSafe for RingBuffer<T>where
T: RefUnwindSafe,
impl<T> Send for RingBuffer<T>where
T: Send,
impl<T> Sync for RingBuffer<T>where
T: Sync,
impl<T> Unpin for RingBuffer<T>where
T: Unpin,
impl<T> UnsafeUnpin for RingBuffer<T>
impl<T> UnwindSafe for RingBuffer<T>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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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