Skip to content

Commit a573ce9

Browse files
committed
Use ManuallyDrop instead of mem::forget in into_inner
1 parent c1ae996 commit a573ce9

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/lib.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ extern crate core as std;
193193

194194
use std::fmt;
195195
use std::marker::PhantomData;
196-
use std::mem::{self, ManuallyDrop};
196+
use std::mem::ManuallyDrop;
197197
use std::ops::{Deref, DerefMut};
198198
use std::ptr;
199199

@@ -346,16 +346,15 @@ impl<T, F, S> ScopeGuard<T, F, S>
346346
/// ```
347347
#[inline]
348348
pub fn into_inner(guard: Self) -> T {
349-
// Cannot move out of Drop-implementing types, so
350-
// ptr::read the value and forget the guard.
349+
// Cannot move out of Drop-implementing types, so ptr::read the value
350+
// and forget the guard.
351+
let guard = ManuallyDrop::new(guard);
351352
unsafe {
352353
let value = ptr::read(&*guard.value);
353-
// read the closure so that it is dropped, and assign it to a local
354-
// variable to ensure that it is only dropped after the guard has
355-
// been forgotten. (In case the Drop impl of the closure, or that
356-
// of any consumed captured variable, panics).
357-
let _dropfn = ptr::read(&*guard.dropfn);
358-
mem::forget(guard);
354+
// Read the closure so that it gets dropped. Do this after value has
355+
// also been read so that, if the closure's drop function panics,
356+
// unwinding still tries to drop value.
357+
ptr::read(&*guard.dropfn)
359358
value
360359
}
361360
}

0 commit comments

Comments
 (0)