rodio/source/
empty_callback.rs

1use std::marker::PhantomData;
2use std::time::Duration;
3
4use crate::{Sample, Source};
5
6use super::SeekError;
7
8/// An empty source which executes a callback function
9pub struct EmptyCallback<S> {
10    #[allow(missing_docs)] // See: https://github.com/RustAudio/rodio/issues/615
11    pub phantom_data: PhantomData<S>,
12    #[allow(missing_docs)] // See: https://github.com/RustAudio/rodio/issues/615
13    pub callback: Box<dyn Send + Fn()>,
14}
15
16impl<S> EmptyCallback<S> {
17    #[inline]
18    /// Create an empty source which executes a callback function.
19    /// Example use-case:
20    ///
21    /// Detect and do something when the source before this one has ended.
22    pub fn new(callback: Box<dyn Send + Fn()>) -> EmptyCallback<S> {
23        EmptyCallback {
24            phantom_data: PhantomData,
25            callback,
26        }
27    }
28}
29
30impl<S> Iterator for EmptyCallback<S> {
31    type Item = S;
32
33    #[inline]
34    fn next(&mut self) -> Option<S> {
35        (self.callback)();
36        None
37    }
38}
39
40impl<S> Source for EmptyCallback<S>
41where
42    S: Sample,
43{
44    #[inline]
45    fn current_frame_len(&self) -> Option<usize> {
46        None
47    }
48
49    #[inline]
50    fn channels(&self) -> u16 {
51        1
52    }
53
54    #[inline]
55    fn sample_rate(&self) -> u32 {
56        48000
57    }
58
59    #[inline]
60    fn total_duration(&self) -> Option<Duration> {
61        Some(Duration::new(0, 0))
62    }
63
64    #[inline]
65    fn try_seek(&mut self, _: Duration) -> Result<(), SeekError> {
66        Err(SeekError::NotSupported {
67            underlying_source: std::any::type_name::<Self>(),
68        })
69    }
70}