ds_ring/oracle.rs
1//! Naive reference implementations: the forever-oracle.
2//!
3//! Plain `Vec` with shift-on-pop — spec forms, not optimized (the shift is
4//! deliberately quadratic on eviction). Differential tests in
5//! tests/props.rs prove the real rings match these exactly; the oracle is
6//! never deleted (ds-kit AGENTS.md). Do not optimize this module.
7
8use alloc::vec::Vec;
9
10/// Oracle for [`crate::SampleRing`]: oldest value at index `0`, eviction
11/// shifts the whole `Vec`.
12#[derive(Debug, Clone, Default)]
13pub struct OracleSampleRing<T, const N: usize> {
14 /// Live values, oldest first; `len() <= N`.
15 buf: Vec<T>,
16}
17
18impl<T, const N: usize> OracleSampleRing<T, N> {
19 /// New empty oracle ring.
20 #[must_use]
21 pub fn new() -> Self {
22 Self { buf: Vec::new() }
23 }
24
25 /// Push one value; mirrors [`crate::SampleRing::push`] exactly:
26 /// `None` when accepted without eviction, `Some(old)` with the evicted
27 /// oldest when full, and the pushed value straight back when `N == 0`.
28 pub fn push(&mut self, value: T) -> Option<T> {
29 if N == 0 {
30 return Some(value);
31 }
32 if self.buf.len() == N {
33 let oldest = self.buf.remove(0);
34 self.buf.push(value);
35 Some(oldest)
36 } else {
37 self.buf.push(value);
38 None
39 }
40 }
41
42 /// Oldest-first iterator over the live values.
43 pub fn iter(&self) -> core::slice::Iter<'_, T> {
44 self.buf.iter()
45 }
46
47 /// Number of live values (`0..=N`).
48 #[must_use]
49 pub fn len(&self) -> usize {
50 self.buf.len()
51 }
52
53 /// True when the oracle ring holds no values.
54 #[must_use]
55 pub fn is_empty(&self) -> bool {
56 self.buf.is_empty()
57 }
58
59 /// Newest value, or `None` when empty.
60 #[must_use]
61 pub fn last(&self) -> Option<&T> {
62 self.buf.last()
63 }
64}
65
66impl<'a, T, const N: usize> IntoIterator for &'a OracleSampleRing<T, N> {
67 type Item = &'a T;
68 type IntoIter = core::slice::Iter<'a, T>;
69
70 fn into_iter(self) -> Self::IntoIter {
71 self.buf.iter()
72 }
73}
74
75/// Oracle for [`crate::SpscRing`]: plain `Vec` bounded by `capacity`,
76/// shift-on-pop FIFO.
77#[derive(Debug, Clone)]
78pub struct OracleSpsc<T> {
79 /// Fixed bound (never mutated after construction).
80 capacity: usize,
81 /// Live values, oldest first.
82 buf: Vec<T>,
83}
84
85impl<T> OracleSpsc<T> {
86 /// New empty oracle queue with `capacity` slots; `None` for capacity
87 /// `0` (the real ring refuses `CAP < 2` at compile time — power of two
88 /// and the CAP-1 sequence collision — so the oracle's runtime bound
89 /// only needs to stay positive and generous).
90 #[must_use]
91 pub fn new(capacity: usize) -> Option<Self> {
92 if capacity == 0 {
93 None
94 } else {
95 Some(Self {
96 capacity,
97 buf: Vec::new(),
98 })
99 }
100 }
101
102 /// Push one value; `Err(value)` when full — the push-back-on-full
103 /// contract the real ring's `try_push` mirrors as `Some(value)`.
104 ///
105 /// # Errors
106 ///
107 /// Returns `Err(value)` unchanged when the queue is full; the value is
108 /// not stored.
109 pub fn push(&mut self, value: T) -> Result<(), T> {
110 if self.buf.len() == self.capacity {
111 Err(value)
112 } else {
113 self.buf.push(value);
114 Ok(())
115 }
116 }
117
118 /// Pop the oldest value, or `None` when empty — the FIFO contract the
119 /// real ring's `try_pop` must reproduce.
120 pub fn pop(&mut self) -> Option<T> {
121 if self.buf.is_empty() {
122 None
123 } else {
124 Some(self.buf.remove(0))
125 }
126 }
127
128 /// Number of live values.
129 #[must_use]
130 pub fn len(&self) -> usize {
131 self.buf.len()
132 }
133
134 /// True when the oracle queue holds no values.
135 #[must_use]
136 pub fn is_empty(&self) -> bool {
137 self.buf.is_empty()
138 }
139}