Skip to content

Commit

Permalink
Merge pull request #1032 from BrainBlasted/generic-liststore-new
Browse files Browse the repository at this point in the history
gio: Make ListStore::new() take a type parameter
  • Loading branch information
sdroege authored Mar 5, 2023
2 parents 7c06442 + e2235a6 commit 7b79419
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 30 deletions.
3 changes: 3 additions & 0 deletions gio/Gir.toml
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,9 @@ manual_traits = ["ListModelExtManual"]
name = "Gio.ListStore"
status = "generate"
generate_builder = true
[[object.function]]
name = "new"
manual = true # Disable the generated constructors so we can use ones with type parameters
[[object.function]]
name = "insert_sorted"
manual = true
Expand Down
11 changes: 0 additions & 11 deletions gio/src/auto/list_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ glib::wrapper! {
}

impl ListStore {
#[doc(alias = "g_list_store_new")]
pub fn new(item_type: glib::types::Type) -> ListStore {
unsafe { from_glib_full(ffi::g_list_store_new(item_type.into_glib())) }
}

// rustdoc-stripper-ignore-next
/// Creates a new builder-pattern struct instance to construct [`ListStore`] objects.
///
Expand Down Expand Up @@ -84,12 +79,6 @@ impl ListStore {
}
}

impl Default for ListStore {
fn default() -> Self {
glib::object::Object::new::<Self>()
}
}

// rustdoc-stripper-ignore-next
/// A [builder-pattern] type to construct [`ListStore`] objects.
///
Expand Down
10 changes: 5 additions & 5 deletions gio/src/list_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl<'a> std::iter::IntoIterator for &'a ListModel {

#[test]
fn list_model_iter_ok() {
let list = crate::ListStore::new(crate::Menu::static_type());
let list = crate::ListStore::new::<crate::Menu>();
let m1 = crate::Menu::new();
let m2 = crate::Menu::new();
let m3 = crate::Menu::new();
Expand All @@ -218,7 +218,7 @@ fn list_model_iter_ok() {

#[test]
fn list_model_iter_err() {
let list = crate::ListStore::new(crate::Menu::static_type());
let list = crate::ListStore::new::<crate::Menu>();
let m1 = crate::Menu::new();
let m2 = crate::Menu::new();
let m3 = crate::Menu::new();
Expand Down Expand Up @@ -251,7 +251,7 @@ fn list_model_iter_err() {

#[test]
fn list_model_iter_nth() {
let list = crate::ListStore::new(crate::Menu::static_type());
let list = crate::ListStore::new::<crate::Menu>();
let m1 = crate::Menu::new();
let m2 = crate::Menu::new();
let m3 = crate::Menu::new();
Expand Down Expand Up @@ -280,7 +280,7 @@ fn list_model_iter_nth() {

#[test]
fn list_model_iter_last() {
let list = crate::ListStore::new(crate::Menu::static_type());
let list = crate::ListStore::new::<crate::Menu>();
let m1 = crate::Menu::new();
let m2 = crate::Menu::new();
let m3 = crate::Menu::new();
Expand All @@ -297,7 +297,7 @@ fn list_model_iter_last() {

#[test]
fn list_model_iter_count() {
let list = crate::ListStore::new(crate::Menu::static_type());
let list = crate::ListStore::new::<crate::Menu>();
let m1 = crate::Menu::new();
let m2 = crate::Menu::new();
let m3 = crate::Menu::new();
Expand Down
36 changes: 23 additions & 13 deletions gio/src/list_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ use glib::{prelude::*, translate::*, Object};
use crate::{prelude::*, ListModel, ListStore};

impl ListStore {
#[doc(alias = "g_list_store_new")]
pub fn new<T: StaticType>() -> Self {
Self::with_type(T::static_type())
}

#[doc(alias = "g_list_store_new")]
pub fn with_type(type_: glib::types::Type) -> Self {
unsafe { from_glib_full(ffi::g_list_store_new(type_.into_glib())) }
}

#[doc(alias = "g_list_store_insert_sorted")]
pub fn insert_sorted<P: IsA<glib::Object>, F: FnMut(&Object, &Object) -> Ordering>(
&self,
Expand Down Expand Up @@ -119,7 +129,7 @@ impl ListStore {

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

#[test]
fn splice() {
let item0 = ListStore::new(ListStore::static_type());
let item1 = ListStore::new(ListStore::static_type());
let list = ListStore::new(ListStore::static_type());
let item0 = ListStore::new::<ListStore>();
let item1 = ListStore::new::<ListStore>();
let list = ListStore::new::<ListStore>();
list.splice(0, 0, &[item0.clone(), item1.clone()]);
assert_eq!(list.item(0), Some(item0.upcast()));
assert_eq!(list.item(1), Some(item1.upcast()));
}

#[test]
fn extend() {
let item0 = ListStore::new(ListStore::static_type());
let item1 = ListStore::new(ListStore::static_type());
let mut list = ListStore::new(ListStore::static_type());
let item0 = ListStore::new::<ListStore>();
let item1 = ListStore::new::<ListStore>();
let mut list = ListStore::new::<ListStore>();
list.extend([&item0, &item1]);
assert_eq!(list.item(0).as_ref(), Some(item0.upcast_ref()));
assert_eq!(list.item(1).as_ref(), Some(item1.upcast_ref()));
list.extend([item0.clone(), item1.clone()]);
assert_eq!(list.item(2).as_ref(), Some(item0.upcast_ref()));
assert_eq!(list.item(3).as_ref(), Some(item1.upcast_ref()));

let list_from_slice = ListStore::new(ListStore::static_type());
let list_from_slice = ListStore::new::<ListStore>();
list_from_slice.extend_from_slice(&[item0, item1.clone()]);
assert_eq!(list_from_slice.item(1).as_ref(), Some(item1.upcast_ref()));
}

#[test]
fn from_iterator() {
let item0 = ListStore::new(ListStore::static_type());
let item1 = ListStore::new(ListStore::static_type());
let item0 = ListStore::new::<ListStore>();
let item1 = ListStore::new::<ListStore>();
let v = vec![item0.clone(), item1.clone()];
let list = ListStore::from_iter(v);
assert_eq!(list.item(0).as_ref(), Some(item0.upcast_ref()));
Expand All @@ -204,9 +214,9 @@ mod tests {
#[cfg(feature = "v2_74")]
#[test]
fn find() {
let item0 = ListStore::new(ListStore::static_type());
let item1 = ListStore::new(ListStore::static_type());
let list = ListStore::new(ListStore::static_type());
let item0 = ListStore::new::<ListStore>();
let item1 = ListStore::new::<ListStore>();
let list = ListStore::new::<ListStore>();
list.append(&item0);
list.append(&item1);

Expand Down
2 changes: 1 addition & 1 deletion glib/src/boxed_any_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ glib::wrapper! {
/// use gio::ListStore;
///
/// // The boxed data can be stored as a `glib::object::Object`
/// let list = ListStore::new(BoxedAnyObject::static_type());
/// let list = ListStore::new::<BoxedAnyObject>();
/// list.append(&boxed);
/// ```
pub struct BoxedAnyObject(ObjectSubclass<imp::BoxedAnyObject>);
Expand Down

0 comments on commit 7b79419

Please sign in to comment.