geng_rodio/source/
skippable.rs

1use std::time::Duration;
2
3use crate::Sample;
4use crate::Source;
5
6/// Internal function that builds a `Skippable` object.
7pub fn skippable<I>(source: I) -> Skippable<I> {
8    Skippable {
9        input: source,
10        do_skip: false,
11    }
12}
13
14#[derive(Clone, Debug)]
15pub struct Skippable<I> {
16    input: I,
17    do_skip: bool,
18}
19
20impl<I> Skippable<I> {
21    /// Skips the current source
22    #[inline]
23    pub fn skip(&mut self) {
24        self.do_skip = true;
25    }
26
27    /// Returns a reference to the inner source.
28    #[inline]
29    pub fn inner(&self) -> &I {
30        &self.input
31    }
32
33    /// Returns a mutable reference to the inner source.
34    #[inline]
35    pub fn inner_mut(&mut self) -> &mut I {
36        &mut self.input
37    }
38
39    /// Returns the inner source.
40    #[inline]
41    pub fn into_inner(self) -> I {
42        self.input
43    }
44}
45
46impl<I> Iterator for Skippable<I>
47where
48    I: Source,
49    I::Item: Sample,
50{
51    type Item = I::Item;
52
53    #[inline]
54    fn next(&mut self) -> Option<I::Item> {
55        if self.do_skip {
56            None
57        } else {
58            self.input.next()
59        }
60    }
61
62    #[inline]
63    fn size_hint(&self) -> (usize, Option<usize>) {
64        self.input.size_hint()
65    }
66}
67
68impl<I> Source for Skippable<I>
69where
70    I: Source,
71    I::Item: Sample,
72{
73    #[inline]
74    fn current_frame_len(&self) -> Option<usize> {
75        self.input.current_frame_len()
76    }
77
78    #[inline]
79    fn channels(&self) -> u16 {
80        self.input.channels()
81    }
82
83    #[inline]
84    fn sample_rate(&self) -> u32 {
85        self.input.sample_rate()
86    }
87
88    #[inline]
89    fn total_duration(&self) -> Option<Duration> {
90        self.input.total_duration()
91    }
92}