Skip to content

Commit c67f0ed

Browse files
committed
Added drop guard
1 parent 66bda4e commit c67f0ed

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/map.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,20 +1281,27 @@ 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 F: FnMut(&K, &mut V) -> bool;
1291+
1292+
impl<'r, 'a, K, V, F> Drop for DropGuard<'r, 'a, K, V, F>
1293+
where
1294+
F: FnMut(&K, &mut V) -> bool,
1295+
{
1296+
fn drop(&mut self) {
1297+
while let Some(_) = self.0.next() {}
12961298
}
12971299
}
1300+
while let Some(item) = self.next() {
1301+
let guard = DropGuard(self);
1302+
drop(item);
1303+
mem::forget(guard);
1304+
}
12981305
}
12991306
}
13001307

0 commit comments

Comments
 (0)