Lazy_load: Avoid manually indexing.

make the data: Vec<T> 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.
This commit is contained in:
Jordan Petridis 2018-04-06 21:36:02 +03:00
parent 83abb5a825
commit 4d6c3a67b1
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6

View File

@ -396,25 +396,25 @@ pub fn episodes_listbox(pd: Arc<Podcast>, sender: Sender<Action>) -> Result<gtk:
use gtk::{IsA, Widget};
fn lazy_load<T, U, P, Z>(data: Vec<T>, container: Z, mut predicate: P)
fn lazy_load<T, U, P, Z>(mut data: Vec<T>, container: Z, mut predicate: P)
where
T: Clone + 'static,
T: 'static,
Z: ContainerExt + 'static,
P: FnMut(T) -> U + 'static,
U: IsA<Widget>,
{
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)
});
}