rodio/source/
stoppable.rs
1use std::time::Duration;
2
3use crate::{Sample, Source};
4
5use super::SeekError;
6
7pub fn stoppable<I>(source: I) -> Stoppable<I> {
9 Stoppable {
10 input: source,
11 stopped: false,
12 }
13}
14
15#[derive(Clone, Debug)]
17pub struct Stoppable<I> {
18 input: I,
19 stopped: bool,
20}
21
22impl<I> Stoppable<I> {
23 #[inline]
25 pub fn stop(&mut self) {
26 self.stopped = true;
27 }
28
29 #[inline]
31 pub fn inner(&self) -> &I {
32 &self.input
33 }
34
35 #[inline]
37 pub fn inner_mut(&mut self) -> &mut I {
38 &mut self.input
39 }
40
41 #[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 I::Item: Sample,
52{
53 type Item = I::Item;
54
55 #[inline]
56 fn next(&mut self) -> Option<I::Item> {
57 if self.stopped {
58 None
59 } else {
60 self.input.next()
61 }
62 }
63
64 #[inline]
65 fn size_hint(&self) -> (usize, Option<usize>) {
66 self.input.size_hint()
67 }
68}
69
70impl<I> Source for Stoppable<I>
71where
72 I: Source,
73 I::Item: Sample,
74{
75 #[inline]
76 fn current_frame_len(&self) -> Option<usize> {
77 self.input.current_frame_len()
78 }
79
80 #[inline]
81 fn channels(&self) -> u16 {
82 self.input.channels()
83 }
84
85 #[inline]
86 fn sample_rate(&self) -> u32 {
87 self.input.sample_rate()
88 }
89
90 #[inline]
91 fn total_duration(&self) -> Option<Duration> {
92 self.input.total_duration()
93 }
94
95 #[inline]
96 fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
97 self.input.try_seek(pos)
98 }
99}