Skip to content

Commit beb7966

Browse files
authored
Merge pull request #250 from cuviper/vec-retain_mut
Use Vec::retain_mut
2 parents ca5f848 + d0243c3 commit beb7966

File tree

1 file changed

+3
-19
lines changed

1 file changed

+3
-19
lines changed

src/map/core.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -502,25 +502,9 @@ impl<K, V> IndexMapCore<K, V> {
502502
where
503503
F: FnMut(&mut K, &mut V) -> bool,
504504
{
505-
// FIXME: This could use Vec::retain_mut with MSRV 1.61.
506-
// Like Vec::retain in self.entries, but with mutable K and V.
507-
// We swap-shift all the items we want to keep, truncate the rest,
508-
// then rebuild the raw hash table with the new indexes.
509-
let len = self.entries.len();
510-
let mut n_deleted = 0;
511-
for i in 0..len {
512-
let will_keep = {
513-
let entry = &mut self.entries[i];
514-
keep(&mut entry.key, &mut entry.value)
515-
};
516-
if !will_keep {
517-
n_deleted += 1;
518-
} else if n_deleted > 0 {
519-
self.entries.swap(i - n_deleted, i);
520-
}
521-
}
522-
if n_deleted > 0 {
523-
self.entries.truncate(len - n_deleted);
505+
self.entries
506+
.retain_mut(|entry| keep(&mut entry.key, &mut entry.value));
507+
if self.entries.len() < self.indices.len() {
524508
self.rebuild_hash_table();
525509
}
526510
}

0 commit comments

Comments
 (0)