rodio/source/
from_factory.rs

1use crate::source::{from_iter, FromIter};
2
3/// Builds a source that chains sources built from a factory.
4///
5/// The `factory` parameter is a function that produces a source. The source is then played.
6/// Whenever the source ends, `factory` is called again in order to produce the source that is
7/// played next.
8///
9/// If the `factory` closure returns `None`, then the sound ends.
10pub fn from_factory<F, S>(factory: F) -> FromIter<FromFactoryIter<F>>
11where
12    F: FnMut() -> Option<S>,
13{
14    from_iter(FromFactoryIter { factory })
15}
16
17/// Internal type used by `from_factory`.
18pub struct FromFactoryIter<F> {
19    factory: F,
20}
21
22impl<F, S> Iterator for FromFactoryIter<F>
23where
24    F: FnMut() -> Option<S>,
25{
26    type Item = S;
27
28    #[inline]
29    fn next(&mut self) -> Option<S> {
30        (self.factory)()
31    }
32
33    #[inline]
34    fn size_hint(&self) -> (usize, Option<usize>) {
35        (0, None)
36    }
37}