ds-ring 0.2.0

Fixed-capacity ring buffers: sample/latency ring and SPSC, loom-tested
Documentation
//! Naive reference implementations: the forever-oracle.
//!
//! Plain `Vec` with shift-on-pop — spec forms, not optimized (the shift is
//! deliberately quadratic on eviction). Differential tests in
//! tests/props.rs prove the real rings match these exactly; the oracle is
//! never deleted (ds-kit AGENTS.md). Do not optimize this module.

use alloc::vec::Vec;

/// Oracle for [`crate::SampleRing`]: oldest value at index `0`, eviction
/// shifts the whole `Vec`.
#[derive(Debug, Clone, Default)]
pub struct OracleSampleRing<T, const N: usize> {
    /// Live values, oldest first; `len() <= N`.
    buf: Vec<T>,
}

impl<T, const N: usize> OracleSampleRing<T, N> {
    /// New empty oracle ring.
    #[must_use]
    pub fn new() -> Self {
        Self { buf: Vec::new() }
    }

    /// Push one value; mirrors [`crate::SampleRing::push`] exactly:
    /// `None` when accepted without eviction, `Some(old)` with the evicted
    /// oldest when full, and the pushed value straight back when `N == 0`.
    pub fn push(&mut self, value: T) -> Option<T> {
        if N == 0 {
            return Some(value);
        }
        if self.buf.len() == N {
            let oldest = self.buf.remove(0);
            self.buf.push(value);
            Some(oldest)
        } else {
            self.buf.push(value);
            None
        }
    }

    /// Oldest-first iterator over the live values.
    pub fn iter(&self) -> core::slice::Iter<'_, T> {
        self.buf.iter()
    }

    /// Number of live values (`0..=N`).
    #[must_use]
    pub fn len(&self) -> usize {
        self.buf.len()
    }

    /// True when the oracle ring holds no values.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.buf.is_empty()
    }

    /// Newest value, or `None` when empty.
    #[must_use]
    pub fn last(&self) -> Option<&T> {
        self.buf.last()
    }
}

impl<'a, T, const N: usize> IntoIterator for &'a OracleSampleRing<T, N> {
    type Item = &'a T;
    type IntoIter = core::slice::Iter<'a, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.buf.iter()
    }
}

/// Oracle for [`crate::SpscRing`]: plain `Vec` bounded by `capacity`,
/// shift-on-pop FIFO.
#[derive(Debug, Clone)]
pub struct OracleSpsc<T> {
    /// Fixed bound (never mutated after construction).
    capacity: usize,
    /// Live values, oldest first.
    buf: Vec<T>,
}

impl<T> OracleSpsc<T> {
    /// New empty oracle queue with `capacity` slots; `None` for capacity
    /// `0` (the real ring refuses `CAP < 2` at compile time — power of two
    /// and the CAP-1 sequence collision — so the oracle's runtime bound
    /// only needs to stay positive and generous).
    #[must_use]
    pub fn new(capacity: usize) -> Option<Self> {
        if capacity == 0 {
            None
        } else {
            Some(Self {
                capacity,
                buf: Vec::new(),
            })
        }
    }

    /// Push one value; `Err(value)` when full — the push-back-on-full
    /// contract the real ring's `try_push` mirrors as `Some(value)`.
    ///
    /// # Errors
    ///
    /// Returns `Err(value)` unchanged when the queue is full; the value is
    /// not stored.
    pub fn push(&mut self, value: T) -> Result<(), T> {
        if self.buf.len() == self.capacity {
            Err(value)
        } else {
            self.buf.push(value);
            Ok(())
        }
    }

    /// Pop the oldest value, or `None` when empty — the FIFO contract the
    /// real ring's `try_pop` must reproduce.
    pub fn pop(&mut self) -> Option<T> {
        if self.buf.is_empty() {
            None
        } else {
            Some(self.buf.remove(0))
        }
    }

    /// Number of live values.
    #[must_use]
    pub fn len(&self) -> usize {
        self.buf.len()
    }

    /// True when the oracle queue holds no values.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.buf.is_empty()
    }
}