Skip to content

Commit 9598fc8

Browse files
committed
Added drop guard
1 parent 66bda4e commit 9598fc8

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/map.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,20 +1281,28 @@ where
12811281
table: &'a mut RawTable<(K, V)>,
12821282
}
12831283

1284-
impl<K, V, F> Drop for DrainFilter<'_, K, V, F>
1284+
impl<'a, K, V, F> Drop for DrainFilter<'a, K, V, F>
12851285
where
12861286
F: FnMut(&K, &mut V) -> bool,
12871287
{
12881288
fn drop(&mut self) {
1289-
unsafe {
1290-
while let Some(item) = self.iter.next() {
1291-
let &mut (ref key, ref mut value) = item.as_mut();
1292-
if !(self.f)(key, value) {
1293-
self.table.erase_no_drop(&item);
1294-
item.drop();
1295-
}
1289+
struct DropGuard<'r, 'a, K, V, F>(&'r mut DrainFilter<'a, K, V, F>)
1290+
where
1291+
F: FnMut(&K, &mut V) -> bool;
1292+
1293+
impl<'r, 'a, K, V, F> Drop for DropGuard<'r, 'a, K, V, F>
1294+
where
1295+
F: FnMut(&K, &mut V) -> bool,
1296+
{
1297+
fn drop(&mut self) {
1298+
while let Some(_) = self.0.next() {}
12961299
}
12971300
}
1301+
while let Some(item) = self.next() {
1302+
let guard = DropGuard(self);
1303+
drop(item);
1304+
mem::forget(guard);
1305+
}
12981306
}
12991307
}
13001308

0 commit comments

Comments
 (0)