Skip to main content

rodio/source/
empty_callback.rs

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