Skip to content

Commit 459bd8d

Browse files
authored
Merge pull request #1015 from sdroege/list-store-find-with-equal-func-null-item
gio: Don't pass `NULL` to `g_list_store_find_with_equal_func_full()`
2 parents 20252e1 + f30110c commit 459bd8d

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

gio/src/list_store.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ impl ListStore {
8787
}
8888

8989
unsafe {
90+
// GIO requires a non-NULL item to be passed in so we're constructing a fake item here.
91+
// See https://gitlab.gnome.org/GNOME/glib/-/merge_requests/3284
92+
let item = glib::gobject_ffi::GObject {
93+
g_type_instance: glib::gobject_ffi::GTypeInstance {
94+
g_class: glib::gobject_ffi::g_type_class_peek(self.item_type().into_glib())
95+
as *mut _,
96+
},
97+
ref_count: 1,
98+
qdata: std::ptr::null_mut(),
99+
};
90100
let mut func = equal_func;
91101
let func_obj: &mut (dyn FnMut(&Object) -> bool) = &mut func;
92102
let func_ptr =
@@ -96,7 +106,7 @@ impl ListStore {
96106

97107
let found = bool::from_glib(ffi::g_list_store_find_with_equal_func_full(
98108
self.to_glib_none().0,
99-
std::ptr::null_mut(),
109+
mut_override(&item as *const _),
100110
Some(equal_func_trampoline),
101111
func_ptr,
102112
position.as_mut_ptr(),
@@ -190,4 +200,17 @@ mod tests {
190200
assert_eq!(list.item(1).as_ref(), Some(item1.upcast_ref()));
191201
assert_eq!(list.item(2).as_ref(), None);
192202
}
203+
204+
#[cfg(feature = "v2_74")]
205+
#[test]
206+
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());
210+
list.append(&item0);
211+
list.append(&item1);
212+
213+
let res = list.find_with_equal_func(|item| item == &item1);
214+
assert_eq!(res, Some(1));
215+
}
193216
}

0 commit comments

Comments
 (0)