Skip to content

Commit 7b79419

Browse files
authored
Merge pull request #1032 from BrainBlasted/generic-liststore-new
gio: Make ListStore::new() take a type parameter
2 parents 7c06442 + e2235a6 commit 7b79419

File tree

5 files changed

+32
-30
lines changed

5 files changed

+32
-30
lines changed

gio/Gir.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,9 @@ manual_traits = ["ListModelExtManual"]
927927
name = "Gio.ListStore"
928928
status = "generate"
929929
generate_builder = true
930+
[[object.function]]
931+
name = "new"
932+
manual = true # Disable the generated constructors so we can use ones with type parameters
930933
[[object.function]]
931934
name = "insert_sorted"
932935
manual = true

gio/src/auto/list_store.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ glib::wrapper! {
1919
}
2020

2121
impl ListStore {
22-
#[doc(alias = "g_list_store_new")]
23-
pub fn new(item_type: glib::types::Type) -> ListStore {
24-
unsafe { from_glib_full(ffi::g_list_store_new(item_type.into_glib())) }
25-
}
26-
2722
// rustdoc-stripper-ignore-next
2823
/// Creates a new builder-pattern struct instance to construct [`ListStore`] objects.
2924
///
@@ -84,12 +79,6 @@ impl ListStore {
8479
}
8580
}
8681

87-
impl Default for ListStore {
88-
fn default() -> Self {
89-
glib::object::Object::new::<Self>()
90-
}
91-
}
92-
9382
// rustdoc-stripper-ignore-next
9483
/// A [builder-pattern] type to construct [`ListStore`] objects.
9584
///

gio/src/list_model.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ impl<'a> std::iter::IntoIterator for &'a ListModel {
192192

193193
#[test]
194194
fn list_model_iter_ok() {
195-
let list = crate::ListStore::new(crate::Menu::static_type());
195+
let list = crate::ListStore::new::<crate::Menu>();
196196
let m1 = crate::Menu::new();
197197
let m2 = crate::Menu::new();
198198
let m3 = crate::Menu::new();
@@ -218,7 +218,7 @@ fn list_model_iter_ok() {
218218

219219
#[test]
220220
fn list_model_iter_err() {
221-
let list = crate::ListStore::new(crate::Menu::static_type());
221+
let list = crate::ListStore::new::<crate::Menu>();
222222
let m1 = crate::Menu::new();
223223
let m2 = crate::Menu::new();
224224
let m3 = crate::Menu::new();
@@ -251,7 +251,7 @@ fn list_model_iter_err() {
251251

252252
#[test]
253253
fn list_model_iter_nth() {
254-
let list = crate::ListStore::new(crate::Menu::static_type());
254+
let list = crate::ListStore::new::<crate::Menu>();
255255
let m1 = crate::Menu::new();
256256
let m2 = crate::Menu::new();
257257
let m3 = crate::Menu::new();
@@ -280,7 +280,7 @@ fn list_model_iter_nth() {
280280

281281
#[test]
282282
fn list_model_iter_last() {
283-
let list = crate::ListStore::new(crate::Menu::static_type());
283+
let list = crate::ListStore::new::<crate::Menu>();
284284
let m1 = crate::Menu::new();
285285
let m2 = crate::Menu::new();
286286
let m3 = crate::Menu::new();
@@ -297,7 +297,7 @@ fn list_model_iter_last() {
297297

298298
#[test]
299299
fn list_model_iter_count() {
300-
let list = crate::ListStore::new(crate::Menu::static_type());
300+
let list = crate::ListStore::new::<crate::Menu>();
301301
let m1 = crate::Menu::new();
302302
let m2 = crate::Menu::new();
303303
let m3 = crate::Menu::new();

gio/src/list_store.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ use glib::{prelude::*, translate::*, Object};
77
use crate::{prelude::*, ListModel, ListStore};
88

99
impl ListStore {
10+
#[doc(alias = "g_list_store_new")]
11+
pub fn new<T: StaticType>() -> Self {
12+
Self::with_type(T::static_type())
13+
}
14+
15+
#[doc(alias = "g_list_store_new")]
16+
pub fn with_type(type_: glib::types::Type) -> Self {
17+
unsafe { from_glib_full(ffi::g_list_store_new(type_.into_glib())) }
18+
}
19+
1020
#[doc(alias = "g_list_store_insert_sorted")]
1121
pub fn insert_sorted<P: IsA<glib::Object>, F: FnMut(&Object, &Object) -> Ordering>(
1222
&self,
@@ -119,7 +129,7 @@ impl ListStore {
119129

120130
impl<P: IsA<glib::Object>> std::iter::FromIterator<P> for ListStore {
121131
fn from_iter<I: IntoIterator<Item = P>>(iter: I) -> Self {
122-
let store = Self::new(P::static_type());
132+
let store = Self::new::<P>();
123133
for item in iter.into_iter() {
124134
store.append(&item)
125135
}
@@ -165,35 +175,35 @@ mod tests {
165175

166176
#[test]
167177
fn splice() {
168-
let item0 = ListStore::new(ListStore::static_type());
169-
let item1 = ListStore::new(ListStore::static_type());
170-
let list = ListStore::new(ListStore::static_type());
178+
let item0 = ListStore::new::<ListStore>();
179+
let item1 = ListStore::new::<ListStore>();
180+
let list = ListStore::new::<ListStore>();
171181
list.splice(0, 0, &[item0.clone(), item1.clone()]);
172182
assert_eq!(list.item(0), Some(item0.upcast()));
173183
assert_eq!(list.item(1), Some(item1.upcast()));
174184
}
175185

176186
#[test]
177187
fn extend() {
178-
let item0 = ListStore::new(ListStore::static_type());
179-
let item1 = ListStore::new(ListStore::static_type());
180-
let mut list = ListStore::new(ListStore::static_type());
188+
let item0 = ListStore::new::<ListStore>();
189+
let item1 = ListStore::new::<ListStore>();
190+
let mut list = ListStore::new::<ListStore>();
181191
list.extend([&item0, &item1]);
182192
assert_eq!(list.item(0).as_ref(), Some(item0.upcast_ref()));
183193
assert_eq!(list.item(1).as_ref(), Some(item1.upcast_ref()));
184194
list.extend([item0.clone(), item1.clone()]);
185195
assert_eq!(list.item(2).as_ref(), Some(item0.upcast_ref()));
186196
assert_eq!(list.item(3).as_ref(), Some(item1.upcast_ref()));
187197

188-
let list_from_slice = ListStore::new(ListStore::static_type());
198+
let list_from_slice = ListStore::new::<ListStore>();
189199
list_from_slice.extend_from_slice(&[item0, item1.clone()]);
190200
assert_eq!(list_from_slice.item(1).as_ref(), Some(item1.upcast_ref()));
191201
}
192202

193203
#[test]
194204
fn from_iterator() {
195-
let item0 = ListStore::new(ListStore::static_type());
196-
let item1 = ListStore::new(ListStore::static_type());
205+
let item0 = ListStore::new::<ListStore>();
206+
let item1 = ListStore::new::<ListStore>();
197207
let v = vec![item0.clone(), item1.clone()];
198208
let list = ListStore::from_iter(v);
199209
assert_eq!(list.item(0).as_ref(), Some(item0.upcast_ref()));
@@ -204,9 +214,9 @@ mod tests {
204214
#[cfg(feature = "v2_74")]
205215
#[test]
206216
fn find() {
207-
let item0 = ListStore::new(ListStore::static_type());
208-
let item1 = ListStore::new(ListStore::static_type());
209-
let list = ListStore::new(ListStore::static_type());
217+
let item0 = ListStore::new::<ListStore>();
218+
let item1 = ListStore::new::<ListStore>();
219+
let list = ListStore::new::<ListStore>();
210220
list.append(&item0);
211221
list.append(&item1);
212222

glib/src/boxed_any_object.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ glib::wrapper! {
8080
/// use gio::ListStore;
8181
///
8282
/// // The boxed data can be stored as a `glib::object::Object`
83-
/// let list = ListStore::new(BoxedAnyObject::static_type());
83+
/// let list = ListStore::new::<BoxedAnyObject>();
8484
/// list.append(&boxed);
8585
/// ```
8686
pub struct BoxedAnyObject(ObjectSubclass<imp::BoxedAnyObject>);

0 commit comments

Comments
 (0)