Skip to content

Commit bd25e9b

Browse files
Use filter_map instead of for loop
1 parent 1ab524a commit bd25e9b

File tree

1 file changed

+42
-42
lines changed

1 file changed

+42
-42
lines changed

lib/src/cache/store.rs

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -389,49 +389,49 @@ impl CacheKeyObjects {
389389
// But, we do it anyway, if we actually modified things.
390390
let mut count = 0;
391391
self.0.send_if_modified(|cache_key_objects| {
392-
let mut new_objects = HashMap::with_capacity(cache_key_objects.objects.len());
393-
394-
for (variant, value) in cache_key_objects.objects.drain() {
395-
let Some(present) = value.present.as_ref() else {
396-
// We may be considering an entry which is obligated, but not yet written.
397-
// In this case, we don't know its surrogate keys, so we leave it in the
398-
// set.
399-
new_objects.insert(variant, value);
400-
continue;
401-
};
402-
403-
if !present.get_meta().surrogate_keys.0.contains(key) {
404-
// Doesn't have this surrogate key; keep it.
405-
new_objects.insert(variant, value);
406-
continue;
407-
}
392+
cache_key_objects.objects = cache_key_objects
393+
.objects
394+
.drain()
395+
.filter_map(|(variant, value)| {
396+
let Some(present) = value.present.as_ref() else {
397+
// We may be considering an entry which is obligated, but not yet written.
398+
// In this case, we don't know its surrogate keys, so we leave it in the
399+
// set.
400+
return Some((variant, value));
401+
};
402+
403+
if !present.get_meta().surrogate_keys.0.contains(key) {
404+
// Doesn't have this surrogate key; keep it.
405+
return Some((variant, value));
406+
}
408407

409-
// Purge or soft purge. Either way:
410-
count += 1;
411-
412-
if soft_purge {
413-
present
414-
.meta
415-
.soft_purge
416-
.store(true, std::sync::atomic::Ordering::SeqCst);
417-
new_objects.insert(variant, value);
418-
} else if value.obligated {
419-
// This value has an outstanding obligation.
420-
// We don't want to clobber that, otherwise the obligee will be Confused;
421-
// So, keep the CacheValue but remove the "present".
422-
new_objects.insert(
423-
variant,
424-
CacheValue {
425-
present: None,
426-
obligated: true,
427-
},
428-
);
429-
} else {
430-
// By failing to insert the CacheValue again, we purge the whole key.
431-
// There's nothing to preserve.
432-
}
433-
}
434-
cache_key_objects.objects = new_objects;
408+
// Purge or soft purge. Either way:
409+
count += 1;
410+
411+
if soft_purge {
412+
present
413+
.meta
414+
.soft_purge
415+
.store(true, std::sync::atomic::Ordering::SeqCst);
416+
Some((variant, value))
417+
} else if value.obligated {
418+
// This value has an outstanding obligation.
419+
// We don't want to clobber that, otherwise the obligee will be Confused;
420+
// So, keep the CacheValue but remove the "present".
421+
Some((
422+
variant,
423+
CacheValue {
424+
present: None,
425+
obligated: true,
426+
},
427+
))
428+
} else {
429+
// By failing to insert the CacheValue again, we purge the whole key.
430+
// There's nothing to preserve.
431+
None
432+
}
433+
})
434+
.collect();
435435

436436
// Notifications matter (only) to tasks waiting on an obligation,
437437
// if the obligation was fulfilled or abandoned.

0 commit comments

Comments
 (0)