gtk-rs / gtk-rs-core

Rust bindings for GNOME libraries
https://gtk-rs.org/gtk-rs-core
MIT License
272 stars 103 forks source link

[BUG] ListModel IntoIter implementation doesn't apply to subclasses #1416

Closed AdrianVovk closed 1 month ago

AdrianVovk commented 1 month ago

Bug description

The IntoIter impl for ListModel doesn't exist outside of the ListModel type itself; you can't use it from subclasses

use gtk::{glib, gio};

fn main() -> glib::ExitCode {
    let store = gio::ListStore::new::<glib::Object>();
    let filtered = gtk::FilterListModel::new(Some(store), None);

    for obj in &filtered {
        dbg!(&obj);
    }

    0.into()
}

instead you have to call upcast_ref::<gio::ListModel>() on it

Backtrace

error[E0277]: `&FilterListModel` is not an iterator
 --> src/main.rs:8:16
  |
8 |     for obj in &filtered {
  |                ^^^^^^^^^ `&FilterListModel` is not an iterator
  |
  = help: the trait `Iterator` is not implemented for `&FilterListModel`, which is required by `&FilterListModel: IntoIterator`
  = note: required for `&FilterListModel` to implement `IntoIterator`

For more information about this error, try `rustc --explain E0277`.
sdroege commented 1 month ago

Due to the trait impl orphan rules, this trait can't be implemented for all IsA<ListModel>.

We could add a into_iter() method on ListModelExtManual but that would have to be called manually instead of just using a for loop like you wrote above. Do you think that's worth adding?

AdrianVovk commented 1 month ago

ugh orphan rules... right.

into_iter() is essentially the same thing as .iter<glib::Object>() so it's probably not worth adding. If it'll need to be a manual call you can already call .iter manually. So closing