Skip to main content

rodio/source/
empty.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.
9#[derive(Debug, Default, Copy, Clone)]
10pub struct Empty;
11
12impl Empty {
13    /// An empty source that immediately ends without ever returning a sample to
14    /// play
15    #[inline]
16    pub fn new() -> Self {
17        Self
18    }
19}
20
21impl Iterator for Empty {
22    type Item = Sample;
23
24    #[inline]
25    fn next(&mut self) -> Option<Self::Item> {
26        None
27    }
28
29    #[inline]
30    fn size_hint(&self) -> (usize, Option<usize>) {
31        (0, Some(0))
32    }
33}
34
35impl ExactSizeIterator for Empty {}
36
37impl Source for Empty {
38    #[inline]
39    fn current_span_len(&self) -> Option<usize> {
40        Some(0)
41    }
42
43    #[inline]
44    fn channels(&self) -> ChannelCount {
45        nz!(1)
46    }
47
48    #[inline]
49    fn sample_rate(&self) -> SampleRate {
50        nz!(48000)
51    }
52
53    #[inline]
54    fn total_duration(&self) -> Option<Duration> {
55        Some(Duration::ZERO)
56    }
57
58    #[inline]
59    fn try_seek(&mut self, _: Duration) -> Result<(), SeekError> {
60        Err(SeekError::NotSupported {
61            underlying_source: std::any::type_name::<Self>(),
62        })
63    }
64}