refraction-types 0.1.1

Zero-dependency bounded ring buffer for networking workloads with predictable overwrite-on-full semantics, minimal allocations, and an ergonomic byte-oriented API.
Documentation
use std::iter::Chain;

use crate::ring_buffer::BoundedRingBuffer;

/// Owning iterator returned when consuming a [`BoundedRingBuffer`].
///
/// Yields values in logical dequeue order.
pub struct IntoIter<T>(pub BoundedRingBuffer<T>);

/// Shared iterator over readable elements of a [`BoundedRingBuffer`].
///
/// The iterator follows queue order (oldest to newest), even when the
/// underlying storage is physically wrapped.
pub struct Iter<'a, T>(pub Chain<std::slice::Iter<'a, T>, std::slice::Iter<'a, T>>);

/// Mutable iterator over readable elements of a [`BoundedRingBuffer`].
///
/// The iterator follows queue order (oldest to newest), even when the
/// underlying storage is physically wrapped.
pub struct IterMut<'a, T>(pub Chain<std::slice::IterMut<'a, T>, std::slice::IterMut<'a, T>>);

impl<T> Iterator for IntoIter<T>
where
    T: Copy + Default,
{
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        self.0.dequeue()
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.0.len, Some(self.0.len))
    }
}

impl<'a, T> Iterator for Iter<'a, T>
where
    T: Copy + Default,
{
    type Item = &'a T;

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next()
    }
}

impl<'a, T> Iterator for IterMut<'a, T>
where
    T: Copy + Default,
{
    type Item = &'a mut T;

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next()
    }
}