geng_rodio/source/
empty_callback.rs

1use std::marker::PhantomData;
2use std::time::Duration;
3
4use crate::{Sample, Source};
5
6/// An empty source which executes a callback function
7pub struct EmptyCallback<S> {
8    pub phantom_data: PhantomData<S>,
9    pub callback: Box<dyn Send + Fn()>,
10}
11
12impl<S> EmptyCallback<S> {
13    #[inline]
14    pub fn new(callback: Box<dyn Send + Fn()>) -> EmptyCallback<S> {
15        EmptyCallback {
16            phantom_data: PhantomData,
17            callback,
18        }
19    }
20}
21
22impl<S> Iterator for EmptyCallback<S> {
23    type Item = S;
24
25    #[inline]
26    fn next(&mut self) -> Option<S> {
27        (self.callback)();
28        None
29    }
30}
31
32impl<S> Source for EmptyCallback<S>
33where
34    S: Sample,
35{
36    #[inline]
37    fn current_frame_len(&self) -> Option<usize> {
38        None
39    }
40
41    #[inline]
42    fn channels(&self) -> u16 {
43        1
44    }
45
46    #[inline]
47    fn sample_rate(&self) -> u32 {
48        48000
49    }
50
51    #[inline]
52    fn total_duration(&self) -> Option<Duration> {
53        Some(Duration::new(0, 0))
54    }
55}