audio/channel/linear/
iter.rs

1use core::slice;
2
3/// An iterator over the frames in a linear channel.
4///
5/// Created with [LinearChannel::iter][super::LinearChannel::iter].
6pub struct Iter<'a, T> {
7    iter: slice::Iter<'a, T>,
8}
9
10impl<'a, T> Iter<'a, T> {
11    #[inline]
12    pub(crate) fn new(data: &'a [T]) -> Self {
13        Self { iter: data.iter() }
14    }
15
16    /// Views the underlying data as a subslice of the original data.
17    #[inline]
18    pub fn as_slice(&self) -> &'a [T] {
19        self.iter.as_slice()
20    }
21}
22
23impl<T> Iterator for Iter<'_, T>
24where
25    T: Copy,
26{
27    type Item = T;
28
29    #[inline]
30    fn next(&mut self) -> Option<Self::Item> {
31        Some(*self.iter.next()?)
32    }
33
34    #[inline]
35    fn nth(&mut self, n: usize) -> Option<Self::Item> {
36        Some(*self.iter.nth(n)?)
37    }
38
39    #[inline]
40    fn last(self) -> Option<Self::Item> {
41        Some(*self.iter.last()?)
42    }
43
44    #[inline]
45    fn size_hint(&self) -> (usize, Option<usize>) {
46        self.iter.size_hint()
47    }
48
49    #[inline]
50    fn count(self) -> usize {
51        self.iter.count()
52    }
53}
54
55impl<T> DoubleEndedIterator for Iter<'_, T>
56where
57    T: Copy,
58{
59    #[inline]
60    fn next_back(&mut self) -> Option<Self::Item> {
61        Some(*self.iter.next_back()?)
62    }
63
64    #[inline]
65    fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
66        Some(*self.iter.nth_back(n)?)
67    }
68}
69
70impl<T> ExactSizeIterator for Iter<'_, T>
71where
72    T: Copy,
73{
74    fn len(&self) -> usize {
75        self.iter.len()
76    }
77}
78
79/// A mutable iterator over the frames in a linear channel.
80///
81/// Created with [LinearChannelMut::iter_mut][super::LinearChannelMut::iter_mut].
82pub struct IterMut<'a, T> {
83    iter: slice::IterMut<'a, T>,
84}
85
86impl<'a, T> IterMut<'a, T> {
87    #[inline]
88    pub(crate) fn new(data: &'a mut [T]) -> Self {
89        Self {
90            iter: data.iter_mut(),
91        }
92    }
93
94    /// Views the underlying data as a subslice of the original data.
95    ///
96    /// To avoid creating `&mut` references that alias, this is forced to
97    /// consume the iterator.
98    pub fn into_slice(self) -> &'a [T] {
99        self.iter.into_slice()
100    }
101
102    /// Views the underlying data as a subslice of the original data.
103    pub fn as_slice(&self) -> &[T] {
104        self.iter.as_slice()
105    }
106}
107
108impl<'a, T> Iterator for IterMut<'a, T> {
109    type Item = &'a mut T;
110
111    #[inline]
112    fn next(&mut self) -> Option<Self::Item> {
113        self.iter.next()
114    }
115
116    #[inline]
117    fn nth(&mut self, n: usize) -> Option<Self::Item> {
118        self.iter.nth(n)
119    }
120
121    #[inline]
122    fn last(self) -> Option<Self::Item> {
123        self.iter.last()
124    }
125
126    #[inline]
127    fn size_hint(&self) -> (usize, Option<usize>) {
128        self.iter.size_hint()
129    }
130
131    #[inline]
132    fn count(self) -> usize {
133        self.iter.count()
134    }
135}
136
137impl<T> DoubleEndedIterator for IterMut<'_, T> {
138    #[inline]
139    fn next_back(&mut self) -> Option<Self::Item> {
140        self.iter.next_back()
141    }
142
143    #[inline]
144    fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
145        self.iter.nth_back(n)
146    }
147}
148
149impl<T> ExactSizeIterator for IterMut<'_, T> {
150    fn len(&self) -> usize {
151        self.iter.len()
152    }
153}