Skip to content

Commit 6fbd576

Browse files
Hofer-Julianbilelmoussaoui
authored andcommitted
Use iter on gio::ListModel
That way we don't have to allocate an intermediate `Vec` anymore. Fixes #1032
1 parent ecc8567 commit 6fbd576

File tree

8 files changed

+24
-37
lines changed

8 files changed

+24
-37
lines changed

book/listings/todo/2/window/imp.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::fs::File;
44
use gio::Settings;
55
use glib::signal::Inhibit;
66
use glib::subclass::InitializingObject;
7-
use gtk::glib::Cast;
87
use gtk::prelude::*;
98
use gtk::subclass::prelude::*;
109
use gtk::{gio, glib, CompositeTemplate, Entry, ListView};
@@ -74,10 +73,9 @@ impl WindowImpl for Window {
7473
let backup_data: Vec<TaskData> = self
7574
.obj()
7675
.tasks()
77-
.snapshot()
78-
.iter()
79-
.filter_map(Cast::downcast_ref::<TaskObject>)
80-
.map(TaskObject::task_data)
76+
.iter::<TaskObject>()
77+
.filter_map(Result::ok)
78+
.map(|task_object| task_object.task_data())
8179
.collect();
8280

8381
// Save state to file

book/listings/todo/3/window/imp.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::fs::File;
44
use gio::Settings;
55
use glib::signal::Inhibit;
66
use glib::subclass::InitializingObject;
7-
use gtk::glib::Cast;
87
use gtk::prelude::*;
98
use gtk::subclass::prelude::*;
109
use gtk::{gio, glib, CompositeTemplate, Entry, ListView};
@@ -69,10 +68,9 @@ impl WindowImpl for Window {
6968
let backup_data: Vec<TaskData> = self
7069
.obj()
7170
.tasks()
72-
.snapshot()
73-
.iter()
74-
.filter_map(Cast::downcast_ref::<TaskObject>)
75-
.map(TaskObject::task_data)
71+
.iter::<TaskObject>()
72+
.filter_map(Result::ok)
73+
.map(|task_object| task_object.task_data())
7674
.collect();
7775

7876
// Save state to file

book/listings/todo/4/window/imp.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::fs::File;
44
use gio::Settings;
55
use glib::signal::Inhibit;
66
use glib::subclass::InitializingObject;
7-
use gtk::glib::Cast;
87
use gtk::prelude::*;
98
use gtk::subclass::prelude::*;
109
use gtk::{gio, glib, CompositeTemplate, Entry, ListView};
@@ -69,10 +68,9 @@ impl WindowImpl for Window {
6968
let backup_data: Vec<TaskData> = self
7069
.obj()
7170
.tasks()
72-
.snapshot()
73-
.iter()
74-
.filter_map(Cast::downcast_ref::<TaskObject>)
75-
.map(TaskObject::task_data)
71+
.iter::<TaskObject>()
72+
.filter_map(Result::ok)
73+
.map(|task_object| task_object.task_data())
7674
.collect();
7775

7876
// Save state to file

book/listings/todo/5/window/imp.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::fs::File;
44
use gio::Settings;
55
use glib::signal::Inhibit;
66
use glib::subclass::InitializingObject;
7-
use gtk::glib::Cast;
87
use gtk::prelude::*;
98
use gtk::subclass::prelude::*;
109
use gtk::{gio, glib, CompositeTemplate, Entry, ListView};
@@ -69,10 +68,9 @@ impl WindowImpl for Window {
6968
let backup_data: Vec<TaskData> = self
7069
.obj()
7170
.tasks()
72-
.snapshot()
73-
.iter()
74-
.filter_map(Cast::downcast_ref::<TaskObject>)
75-
.map(TaskObject::task_data)
71+
.iter::<TaskObject>()
72+
.filter_map(Result::ok)
73+
.map(|task_object| task_object.task_data())
7674
.collect();
7775

7876
// Save state to file

book/listings/todo/6/window/imp.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,9 @@ impl WindowImpl for Window {
7171
let backup_data: Vec<TaskData> = self
7272
.obj()
7373
.tasks()
74-
.snapshot()
75-
.iter()
76-
.filter_map(Cast::downcast_ref::<TaskObject>)
77-
.map(TaskObject::task_data)
74+
.iter::<TaskObject>()
75+
.filter_map(Result::ok)
76+
.map(|task_object| task_object.task_data())
7877
.collect();
7978

8079
// Save state to file

book/listings/todo/7/window/imp.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,9 @@ impl WindowImpl for Window {
7272
let backup_data: Vec<TaskData> = self
7373
.obj()
7474
.tasks()
75-
.snapshot()
76-
.iter()
77-
.filter_map(Cast::downcast_ref::<TaskObject>)
78-
.map(TaskObject::task_data)
75+
.iter::<TaskObject>()
76+
.filter_map(Result::ok)
77+
.map(|task_object| task_object.task_data())
7978
.collect();
8079

8180
// Save state to file

book/listings/todo/8/collection_object/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@ impl CollectionObject {
2525
let title = self.imp().title.borrow().clone();
2626
let tasks_data = self
2727
.tasks()
28-
.snapshot()
29-
.iter()
30-
.filter_map(Cast::downcast_ref::<TaskObject>)
31-
.map(TaskObject::task_data)
28+
.iter::<TaskObject>()
29+
.filter_map(Result::ok)
30+
.map(|task_object| task_object.task_data())
3231
.collect();
33-
3432
CollectionData { title, tasks_data }
3533
}
3634

book/listings/todo/8/window/imp.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,9 @@ impl WindowImpl for Window {
8888
let backup_data: Vec<CollectionData> = self
8989
.obj()
9090
.collections()
91-
.snapshot()
92-
.iter()
93-
.filter_map(Cast::downcast_ref::<CollectionObject>)
94-
.map(CollectionObject::to_collection_data)
91+
.iter::<CollectionObject>()
92+
.filter_map(|collection_object| collection_object.ok())
93+
.map(|collection_object| collection_object.to_collection_data())
9594
.collect();
9695

9796
// Save state to file

0 commit comments

Comments
 (0)