From 00b56ca845d80338a14fff3b8b793d81f7a428d7 Mon Sep 17 00:00:00 2001 From: Guillaume Desmottes Date: Wed, 14 Feb 2024 14:50:16 +0100 Subject: [PATCH] uriplaylistbin: stop using an iterator to manage the playlist Will make it easier to update the playlist while playing. Part-of: --- .../uriplaylistbin/src/uriplaylistbin/imp.rs | 65 ++++++------------- 1 file changed, 21 insertions(+), 44 deletions(-) diff --git a/utils/uriplaylistbin/src/uriplaylistbin/imp.rs b/utils/uriplaylistbin/src/uriplaylistbin/imp.rs index ed29a2d8..fdf55e81 100644 --- a/utils/uriplaylistbin/src/uriplaylistbin/imp.rs +++ b/utils/uriplaylistbin/src/uriplaylistbin/imp.rs @@ -121,63 +121,40 @@ struct ItemInner { } struct Playlist { - items: Box + Send>, uris: Vec, + iterations: u32, + + next_index: usize, } impl Playlist { fn new(uris: Vec, iterations: u32) -> Self { Self { - items: Self::create_items(uris.clone(), iterations), uris, - } - } - - fn create_items( - uris: Vec, - iterations: u32, - ) -> Box + Send + Sync> { - fn infinite_iter(uris: Vec) -> Box + Send + Sync> { - Box::new( - uris.into_iter() - .cycle() - .enumerate() - .map(|(index, uri)| Item::new(uri, index)), - ) - } - fn finite_iter( - uris: Vec, - iterations: u32, - ) -> Box + Send + Sync> { - let n = (iterations as usize) - .checked_mul(uris.len()) - .unwrap_or(usize::MAX); - - Box::new( - uris.into_iter() - .cycle() - .take(n) - .enumerate() - .map(|(index, uri)| Item::new(uri, index)), - ) - } - - if iterations == 0 { - infinite_iter(uris) - } else { - finite_iter(uris, iterations) + iterations, + next_index: 0, } } fn next(&mut self) -> Option { - let item = match self.items.next() { - None => return None, - Some(item) => item, - }; + let uris_len = self.uris.len(); + let (iteration, uri_index) = ( + (self.next_index / uris_len) as u32, + (self.next_index % uris_len), + ); - if item.index() == usize::MAX { + if self.iterations != 0 && iteration >= self.iterations { + // playlist is done + return None; + } + + let uri = self.uris[uri_index].clone(); + let item = Item::new(uri, self.next_index); + + self.next_index += 1; + if self.next_index == usize::MAX { // prevent overflow with infinite playlist - self.items = Self::create_items(self.uris.clone(), 0); + self.next_index = 0; } Some(item)