rodio/source/
skippable.rs

1use crate::common::{ChannelCount, SampleRate};
2use crate::Source;
3use std::time::Duration;
4
5use super::SeekError;
6
7/// Wrap the source in a skippable. It allows ending the current source early by
8/// calling [`Skippable::skip`]. If this source is in a queue such as the Sink
9/// ending the source early is equal to skipping the source.
10pub fn skippable<I>(source: I) -> Skippable<I> {
11    Skippable {
12        input: source,
13        do_skip: false,
14    }
15}
16
17/// Wrap the source in a skippable. It allows ending the current source early by
18/// calling [`Skippable::skip`]. If this source is in a queue such as the Sink
19/// ending the source early is equal to skipping the source.
20#[derive(Clone, Debug)]
21pub struct Skippable<I> {
22    input: I,
23    do_skip: bool,
24}
25
26impl<I> Skippable<I> {
27    /// Skips the current source
28    #[inline]
29    pub fn skip(&mut self) {
30        self.do_skip = true;
31    }
32
33    /// Returns a reference to the inner source.
34    #[inline]
35    pub fn inner(&self) -> &I {
36        &self.input
37    }
38
39    /// Returns a mutable reference to the inner source.
40    #[inline]
41    pub fn inner_mut(&mut self) -> &mut I {
42        &mut self.input
43    }
44
45    /// Returns the inner source.
46    #[inline]
47    pub fn into_inner(self) -> I {
48        self.input
49    }
50}
51
52impl<I> Iterator for Skippable<I>
53where
54    I: Source,
55{
56    type Item = I::Item;
57
58    #[inline]
59    fn next(&mut self) -> Option<I::Item> {
60        if self.do_skip {
61            None
62        } else {
63            self.input.next()
64        }
65    }
66
67    #[inline]
68    fn size_hint(&self) -> (usize, Option<usize>) {
69        self.input.size_hint()
70    }
71}
72
73impl<I> Source for Skippable<I>
74where
75    I: Source,
76{
77    #[inline]
78    fn current_span_len(&self) -> Option<usize> {
79        self.input.current_span_len()
80    }
81
82    #[inline]
83    fn channels(&self) -> ChannelCount {
84        self.input.channels()
85    }
86
87    #[inline]
88    fn sample_rate(&self) -> SampleRate {
89        self.input.sample_rate()
90    }
91
92    #[inline]
93    fn total_duration(&self) -> Option<Duration> {
94        self.input.total_duration()
95    }
96
97    #[inline]
98    fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
99        self.input.try_seek(pos)
100    }
101}