audio_core/buf/
skip.rs

1use crate::{Buf, BufMut, Channel, ChannelMut, ExactSizeBuf, ReadBuf};
2
3/// A buffer where a number of frames have been skipped over.
4///
5/// See [Buf::skip].
6pub struct Skip<B> {
7    buf: B,
8    n: usize,
9}
10
11impl<B> Skip<B> {
12    /// Construct a new buffer skip.
13    pub(crate) fn new(buf: B, n: usize) -> Self {
14        Self { buf, n }
15    }
16}
17
18/// [Skip] adjusts the implementation of [Buf].
19///
20/// ```
21/// use audio::{Buf, ExactSizeBuf};
22///
23/// let buf = audio::interleaved![[0; 4]; 2];
24///
25/// assert_eq!((&buf).skip(0).channels(), 2);
26/// assert_eq!((&buf).skip(0).frames_hint(), Some(4));
27///
28/// assert_eq!((&buf).skip(1).channels(), 2);
29/// assert_eq!((&buf).skip(1).frames_hint(), Some(3));
30///
31/// assert_eq!((&buf).skip(5).channels(), 2);
32/// assert_eq!((&buf).skip(5).frames_hint(), Some(0));
33/// ```
34impl<B> Buf for Skip<B>
35where
36    B: Buf,
37{
38    type Sample = B::Sample;
39
40    type Channel<'this> = B::Channel<'this>
41    where
42        Self: 'this;
43
44    type IterChannels<'this> = IterChannels<B::IterChannels<'this>>
45    where
46        Self: 'this;
47
48    fn frames_hint(&self) -> Option<usize> {
49        let frames = self.buf.frames_hint()?;
50        Some(frames.saturating_sub(self.n))
51    }
52
53    fn channels(&self) -> usize {
54        self.buf.channels()
55    }
56
57    fn get_channel(&self, channel: usize) -> Option<Self::Channel<'_>> {
58        Some(self.buf.get_channel(channel)?.skip(self.n))
59    }
60
61    fn iter_channels(&self) -> Self::IterChannels<'_> {
62        IterChannels {
63            iter: self.buf.iter_channels(),
64            n: self.n,
65        }
66    }
67}
68
69impl<B> BufMut for Skip<B>
70where
71    B: BufMut,
72{
73    type ChannelMut<'a> = B::ChannelMut<'a>
74    where
75        Self: 'a;
76
77    type IterChannelsMut<'a> = IterChannelsMut<B::IterChannelsMut<'a>>
78    where
79        Self: 'a;
80
81    fn get_channel_mut(&mut self, channel: usize) -> Option<Self::ChannelMut<'_>> {
82        Some(self.buf.get_channel_mut(channel)?.skip(self.n))
83    }
84
85    fn copy_channel(&mut self, from: usize, to: usize)
86    where
87        Self::Sample: Copy,
88    {
89        self.buf.copy_channel(from, to);
90    }
91
92    fn iter_channels_mut(&mut self) -> Self::IterChannelsMut<'_> {
93        IterChannelsMut {
94            iter: self.buf.iter_channels_mut(),
95            n: self.n,
96        }
97    }
98}
99
100/// [Skip] adjusts the implementation of [ExactSizeBuf].
101///
102/// ```
103/// use audio::{Buf, ExactSizeBuf};
104///
105/// let buf = audio::interleaved![[0; 4]; 2];
106///
107/// assert_eq!((&buf).skip(0).frames(), 4);
108/// assert_eq!((&buf).skip(1).frames(), 3);
109/// assert_eq!((&buf).skip(5).frames(), 0);
110/// ```
111impl<B> ExactSizeBuf for Skip<B>
112where
113    B: ExactSizeBuf,
114{
115    fn frames(&self) -> usize {
116        self.buf.frames().saturating_sub(self.n)
117    }
118}
119
120impl<B> ReadBuf for Skip<B>
121where
122    B: ReadBuf,
123{
124    fn remaining(&self) -> usize {
125        self.buf.remaining().saturating_sub(self.n)
126    }
127
128    fn advance(&mut self, n: usize) {
129        self.buf.advance(self.n.saturating_add(n));
130    }
131}
132
133iterators!(n: usize => self.skip(n));