gtk-rs / gtk4-rs

Rust bindings of GTK 4
https://gtk-rs.org/gtk4-rs/
MIT License
1.9k stars 174 forks source link

WidgetExt::iter() for children? #176

Closed sophie-h closed 3 years ago

sophie-h commented 3 years ago

I'm using for c in listbox.get_children() from gtk3-rs. This will probably become legacy code, because the handling of children can probably be done via model bindings in gtk4. However, it might still be handy to just have an iterator for children. Opinions?

sdroege commented 3 years ago

The main problem here is how to handle (and detect) iterator invalidation. You're iterating over a container that uses interior mutability, so everything can change under your feet. You could even get the same item twice if you're unlucky :)

bilelmoussaoui commented 3 years ago

Listbox is supposed to be used with models, especially with gtk4.

sdroege commented 3 years ago

That might be true but doesn't solve the problem. You'd have the same when trying to implement an iterator on the list model.

sophie-h commented 3 years ago

So a proper iterator would actually implement something like get_children() and store it to iterate over that. More like an into_iter(). But this would do the same as get_children(), which got removed ... for a reason I guess?

The only 'clean' solution I have in mind for ListModel would be for every .next() on the iterator to return a Result that is Err after connect_items_changed gets triggered. (.iter() could connect to connect_items_changed for the time the Iter it exists)

bilelmoussaoui commented 3 years ago

I think we can't really do much about it here. If you want such api to exists, upstream gtk should provide it. Feel free to open an issue there.

bilelmoussaoui commented 3 years ago

I'm going to go ahead and close this one. Feel free to re-open if you have any suggestions for something we can safely provide.