From c79a92f3b2893732f8dea369e0d500983932f76b Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Fri, 6 Apr 2018 22:26:35 +0300 Subject: [PATCH] Lazy_load: accept an iterator instead a Vec<_> over T. --- hammond-gtk/src/widgets/episode.rs | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/hammond-gtk/src/widgets/episode.rs b/hammond-gtk/src/widgets/episode.rs index a464856..182845f 100644 --- a/hammond-gtk/src/widgets/episode.rs +++ b/hammond-gtk/src/widgets/episode.rs @@ -383,7 +383,7 @@ pub fn episodes_listbox(pd: Arc, sender: Sender) -> Result return glib::Continue(false), }; - lazy_load(episodes, list.clone(), clone!(sender => move |ep| { + lazy_load(episodes.into_iter(), list.clone(), clone!(sender => move |ep| { let w = EpisodeWidget::new(ep, sender.clone()); w.container.clone() })); @@ -396,25 +396,21 @@ pub fn episodes_listbox(pd: Arc, sender: Sender) -> Result(mut data: Vec, container: Z, mut predicate: P) +fn lazy_load(mut data: T, container: Z, mut predicate: P) where - T: 'static, + T: Iterator + 'static, + T::Item: 'static, Z: ContainerExt + 'static, - P: FnMut(T) -> U + 'static, + P: FnMut(T::Item) -> U + 'static, U: IsA, { - // to use it as a stack - data.reverse(); gtk::idle_add(move || { - if data.is_empty() { - return glib::Continue(false); - } - - data.pop().map(|x| { - let widget = predicate(x); - container.add(&widget); - }); - - glib::Continue(true) + data.next() + .and_then(|x| { + container.add(&predicate(x)); + Some(glib::Continue(true)) + }) + .or(Some(glib::Continue(false))) + .unwrap() }); }