Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retain method to ListStore #1133

Merged
merged 1 commit into from
Jul 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 97 additions & 1 deletion gio/src/list_store.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use std::cmp::Ordering;
use std::{cell::Cell, cmp::Ordering, rc::Rc};

use glib::{prelude::*, translate::*, Object};

Expand Down Expand Up @@ -76,6 +76,60 @@ impl ListStore {
self.splice(self.n_items(), 0, additions)
}

// rustdoc-stripper-ignore-next
/// Retains only the elements specified by the predicate.
/// This method operates in place, visiting each element exactly once in the original order,
/// and preserves the order of the retained elements.
/// Because the elements are visited exactly once in the original order,
/// external state may be used to decide which elements to keep.
///
/// # Panics
/// Panics if the predicate closure mutates the list by removing or adding items.
pub fn retain(&self, mut f: impl FnMut(&glib::Object) -> bool) {
let mut consec_removed = 0;
let mut i = 0;
const ADDITIONS: &[glib::Object] = &[]; // To satisfy the type checker

let changed = Rc::new(Cell::new(false));
let changed_clone = changed.clone();
let signal_id = self.connect_items_changed(move |_list, _, _, _| changed_clone.set(true));
ranfdev marked this conversation as resolved.
Show resolved Hide resolved

let _signal_guard = {
struct Guard<'a> {
list_store: &'a ListStore,
signal_id: Option<glib::SignalHandlerId>,
}
impl Drop for Guard<'_> {
fn drop(&mut self) {
self.list_store.disconnect(self.signal_id.take().unwrap());
}
}
Guard {
list_store: self,
signal_id: Some(signal_id),
}
};

while i < self.n_items() {
let keep = f(self.item(i).unwrap().as_ref());
if changed.get() {
panic!("The closure passed to ListStore::retain() must not mutate the list store");
}
if !keep {
consec_removed += 1;
} else if consec_removed > 0 {
self.splice(i - consec_removed, consec_removed, ADDITIONS);
changed.set(false);
i -= consec_removed;
consec_removed = 0;
}
i += 1;
sdroege marked this conversation as resolved.
Show resolved Hide resolved
}
if consec_removed > 0 {
self.splice(i - consec_removed, consec_removed, ADDITIONS);
}
}

#[cfg(feature = "v2_74")]
#[cfg_attr(docsrs, doc(cfg(feature = "v2_74")))]
#[doc(alias = "g_list_store_find_with_equal_func_full")]
Expand Down Expand Up @@ -223,4 +277,46 @@ mod tests {
let res = list.find_with_equal_func(|item| item == &item1);
assert_eq!(res, Some(1));
}

#[test]
fn retain() {
let list = {
let list = ListStore::new::<ListStore>();
for _ in 0..10 {
list.append(&ListStore::new::<ListStore>());
}
list
};

use std::cell::Cell;
use std::rc::Rc;

let signal_count = Rc::new(Cell::new(0));
let signal_count_clone = signal_count.clone();
list.connect_items_changed(move |_, _, _, _| {
signal_count_clone.set(signal_count_clone.get() + 1);
});

let to_keep = [
// list.item(0).unwrap(),
list.item(1).unwrap(),
// list.item(2).unwrap(),
list.item(3).unwrap(),
// list.item(4).unwrap(),
// list.item(5).unwrap(),
// list.item(6).unwrap(),
list.item(7).unwrap(),
// list.item(8).unwrap(),
// list.item(9).unwrap(),
];
list.retain(|item| to_keep.contains(item));

// Check that we removed the correct items
assert_eq!(list.n_items(), 3);
assert_eq!(list.item(0).as_ref(), Some(&to_keep[0]));
assert_eq!(list.item(1).as_ref(), Some(&to_keep[1]));
assert_eq!(list.item(2).as_ref(), Some(&to_keep[2]));

assert_eq!(signal_count.get(), 4);
}
}
Loading