Skip to content

Commit c5b05d7

Browse files
authored
Merge pull request #954 from pbor/list_store
gio: implement FromIterator for ListStore
2 parents 4e4054f + 5d37089 commit c5b05d7

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

gio/src/list_store.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ impl ListStore {
107107
}
108108
}
109109

110+
impl<P: IsA<glib::Object>> std::iter::FromIterator<P> for ListStore {
111+
fn from_iter<I: IntoIterator<Item = P>>(iter: I) -> Self {
112+
let store = Self::new(P::static_type());
113+
for item in iter.into_iter() {
114+
store.append(&item)
115+
}
116+
store
117+
}
118+
}
119+
110120
impl<'a> std::iter::IntoIterator for &'a ListStore {
111121
type Item = <&'a ListModel as IntoIterator>::Item;
112122
type IntoIter = <&'a ListModel as IntoIterator>::IntoIter;
@@ -169,4 +179,15 @@ mod tests {
169179
list_from_slice.extend_from_slice(&[item0, item1.clone()]);
170180
assert_eq!(list_from_slice.item(1).as_ref(), Some(item1.upcast_ref()));
171181
}
182+
183+
#[test]
184+
fn from_iterator() {
185+
let item0 = ListStore::new(ListStore::static_type());
186+
let item1 = ListStore::new(ListStore::static_type());
187+
let v = vec![item0.clone(), item1.clone()];
188+
let list = ListStore::from_iter(v);
189+
assert_eq!(list.item(0).as_ref(), Some(item0.upcast_ref()));
190+
assert_eq!(list.item(1).as_ref(), Some(item1.upcast_ref()));
191+
assert_eq!(list.item(2).as_ref(), None);
192+
}
172193
}

0 commit comments

Comments
 (0)