pub struct SampleRing<T, const N: usize> { /* private fields */ }Expand description
Fixed-capacity sample/latency ring, stack-allocated, oldest-first.
Capacity N is a const generic: the whole ring lives inline in the
owner ([Option<T>; N] plus two indices), so construction allocates
nothing and push allocates nothing — eviction returns the oldest
value by move. FIFO order is preserved: iter walks oldest-first and
push returns the evicted oldest when full.
With N == 0 nothing can be stored; SampleRing::push hands the
value straight back (Some(value)), i.e. the value is “evicted” the
moment it arrives. There is deliberately no indexing API.
use ds_ring::SampleRing;
let mut ring = SampleRing::<u64, 2>::new();
assert_eq!(ring.push(1), None);
assert_eq!(ring.push(2), None);
assert_eq!(ring.push(3), Some(1)); // 1 is the evicted oldest
assert_eq!(ring.iter().copied().collect::<Vec<_>>(), vec![2, 3]);
assert_eq!(ring.last(), Some(&3));Implementations§
Source§impl<T, const N: usize> SampleRing<T, N>
impl<T, const N: usize> SampleRing<T, N>
Sourcepub fn push(&mut self, value: T) -> Option<T>
pub fn push(&mut self, value: T) -> Option<T>
Push one value. Returns None when the value was accepted without
eviction, Some(old) when the ring was full and old (the oldest
value) was evicted to make room. With N == 0 the pushed value is
returned immediately (nothing can be stored). Never allocates,
never panics.