Lazy_load: accept an iterator instead a Vec<_> over T.

This commit is contained in:
Jordan Petridis 2018-04-06 22:26:35 +03:00
parent 4d6c3a67b1
commit c79a92f3b2
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6

View File

@ -383,7 +383,7 @@ pub fn episodes_listbox(pd: Arc<Podcast>, sender: Sender<Action>) -> Result<gtk:
Err(Disconnected) => return glib::Continue(false), Err(Disconnected) => 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()); let w = EpisodeWidget::new(ep, sender.clone());
w.container.clone() w.container.clone()
})); }));
@ -396,25 +396,21 @@ pub fn episodes_listbox(pd: Arc<Podcast>, sender: Sender<Action>) -> Result<gtk:
use gtk::{IsA, Widget}; use gtk::{IsA, Widget};
fn lazy_load<T, U, P, Z>(mut data: Vec<T>, container: Z, mut predicate: P) fn lazy_load<T, U, P, Z>(mut data: T, container: Z, mut predicate: P)
where where
T: 'static, T: Iterator + 'static,
T::Item: 'static,
Z: ContainerExt + 'static, Z: ContainerExt + 'static,
P: FnMut(T) -> U + 'static, P: FnMut(T::Item) -> U + 'static,
U: IsA<Widget>, U: IsA<Widget>,
{ {
// to use it as a stack
data.reverse();
gtk::idle_add(move || { gtk::idle_add(move || {
if data.is_empty() { data.next()
return glib::Continue(false); .and_then(|x| {
} container.add(&predicate(x));
Some(glib::Continue(true))
data.pop().map(|x| { })
let widget = predicate(x); .or(Some(glib::Continue(false)))
container.add(&widget); .unwrap()
});
glib::Continue(true)
}); });
} }