rodio/source/
stoppable.rs

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