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>
41        = B::Channel<'this>
42    where
43        Self: 'this;
44
45    type IterChannels<'this>
46        = IterChannels<B::IterChannels<'this>>
47    where
48        Self: 'this;
49
50    fn frames_hint(&self) -> Option<usize> {
51        let frames = self.buf.frames_hint()?;
52        Some(frames.saturating_sub(self.n))
53    }
54
55    fn channels(&self) -> usize {
56        self.buf.channels()
57    }
58
59    fn get_channel(&self, channel: usize) -> Option<Self::Channel<'_>> {
60        Some(self.buf.get_channel(channel)?.skip(self.n))
61    }
62
63    fn iter_channels(&self) -> Self::IterChannels<'_> {
64        IterChannels {
65            iter: self.buf.iter_channels(),
66            n: self.n,
67        }
68    }
69}
70
71impl<B> BufMut for Skip<B>
72where
73    B: BufMut,
74{
75    type ChannelMut<'a>
76        = B::ChannelMut<'a>
77    where
78        Self: 'a;
79
80    type IterChannelsMut<'a>
81        = IterChannelsMut<B::IterChannelsMut<'a>>
82    where
83        Self: 'a;
84
85    fn get_channel_mut(&mut self, channel: usize) -> Option<Self::ChannelMut<'_>> {
86        Some(self.buf.get_channel_mut(channel)?.skip(self.n))
87    }
88
89    fn copy_channel(&mut self, from: usize, to: usize)
90    where
91        Self::Sample: Copy,
92    {
93        self.buf.copy_channel(from, to);
94    }
95
96    fn iter_channels_mut(&mut self) -> Self::IterChannelsMut<'_> {
97        IterChannelsMut {
98            iter: self.buf.iter_channels_mut(),
99            n: self.n,
100        }
101    }
102}
103
104/// [Skip] adjusts the implementation of [ExactSizeBuf].
105///
106/// ```
107/// use audio::{Buf, ExactSizeBuf};
108///
109/// let buf = audio::interleaved![[0; 4]; 2];
110///
111/// assert_eq!((&buf).skip(0).frames(), 4);
112/// assert_eq!((&buf).skip(1).frames(), 3);
113/// assert_eq!((&buf).skip(5).frames(), 0);
114/// ```
115impl<B> ExactSizeBuf for Skip<B>
116where
117    B: ExactSizeBuf,
118{
119    fn frames(&self) -> usize {
120        self.buf.frames().saturating_sub(self.n)
121    }
122}
123
124impl<B> ReadBuf for Skip<B>
125where
126    B: ReadBuf,
127{
128    fn remaining(&self) -> usize {
129        self.buf.remaining().saturating_sub(self.n)
130    }
131
132    fn advance(&mut self, n: usize) {
133        self.buf.advance(self.n.saturating_add(n));
134    }
135}
136
137iterators!(n: usize => self.skip(n));