Skip to main content

refraction_types/
ring_iter.rs

1use std::iter::Chain;
2
3use crate::ring_buffer::BoundedRingBuffer;
4
5/// Owning iterator returned when consuming a [`BoundedRingBuffer`].
6///
7/// Yields values in logical dequeue order.
8pub struct IntoIter<T>(pub BoundedRingBuffer<T>);
9
10/// Shared iterator over readable elements of a [`BoundedRingBuffer`].
11///
12/// The iterator follows queue order (oldest to newest), even when the
13/// underlying storage is physically wrapped.
14pub struct Iter<'a, T>(pub Chain<std::slice::Iter<'a, T>, std::slice::Iter<'a, T>>);
15
16/// Mutable iterator over readable elements of a [`BoundedRingBuffer`].
17///
18/// The iterator follows queue order (oldest to newest), even when the
19/// underlying storage is physically wrapped.
20pub struct IterMut<'a, T>(pub Chain<std::slice::IterMut<'a, T>, std::slice::IterMut<'a, T>>);
21
22impl<T> Iterator for IntoIter<T>
23where
24    T: Copy + Default,
25{
26    type Item = T;
27
28    fn next(&mut self) -> Option<Self::Item> {
29        self.0.dequeue()
30    }
31
32    fn size_hint(&self) -> (usize, Option<usize>) {
33        (self.0.len, Some(self.0.len))
34    }
35}
36
37impl<'a, T> Iterator for Iter<'a, T>
38where
39    T: Copy + Default,
40{
41    type Item = &'a T;
42
43    fn next(&mut self) -> Option<Self::Item> {
44        self.0.next()
45    }
46}
47
48impl<'a, T> Iterator for IterMut<'a, T>
49where
50    T: Copy + Default,
51{
52    type Item = &'a mut T;
53
54    fn next(&mut self) -> Option<Self::Item> {
55        self.0.next()
56    }
57}