From 4d6c3a67b1a51f36733855a5340ef54f7a6e2333 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Fri, 6 Apr 2018 21:36:02 +0300 Subject: [PATCH] Lazy_load: Avoid manually indexing. make the data: Vec mutable, then reverse the vector so it can be used as a stack, and then use the ::pop() method to retrieve the item. This also avoid the constrain for Clone on T. --- hammond-gtk/src/widgets/episode.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/hammond-gtk/src/widgets/episode.rs b/hammond-gtk/src/widgets/episode.rs index 034b2f6..a464856 100644 --- a/hammond-gtk/src/widgets/episode.rs +++ b/hammond-gtk/src/widgets/episode.rs @@ -396,25 +396,25 @@ pub fn episodes_listbox(pd: Arc, sender: Sender) -> Result(data: Vec, container: Z, mut predicate: P) +fn lazy_load(mut data: Vec, container: Z, mut predicate: P) where - T: Clone + 'static, + T: 'static, Z: ContainerExt + 'static, P: FnMut(T) -> U + 'static, U: IsA, { - let mut idx = 0; + // to use it as a stack + data.reverse(); gtk::idle_add(move || { - if idx >= data.len() { + if data.is_empty() { return glib::Continue(false); } - data.get(idx).cloned().map(|x| { + data.pop().map(|x| { let widget = predicate(x); container.add(&widget); }); - idx += 1; glib::Continue(true) }); }