Skip to content

Commit

Permalink
Merge pull request #25 from willtunnels/refactor-mem-forget
Browse files Browse the repository at this point in the history
  • Loading branch information
Amanieu authored Aug 9, 2021
2 parents c1ae996 + c7fe76f commit cf4a287
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ extern crate core as std;

use std::fmt;
use std::marker::PhantomData;
use std::mem::{self, ManuallyDrop};
use std::mem::ManuallyDrop;
use std::ops::{Deref, DerefMut};
use std::ptr;

Expand Down Expand Up @@ -346,16 +346,15 @@ impl<T, F, S> ScopeGuard<T, F, S>
/// ```
#[inline]
pub fn into_inner(guard: Self) -> T {
// Cannot move out of Drop-implementing types, so
// ptr::read the value and forget the guard.
// Cannot move out of `Drop`-implementing types,
// so `ptr::read` the value and forget the guard.
let mut guard = ManuallyDrop::new(guard);
unsafe {
let value = ptr::read(&*guard.value);
// read the closure so that it is dropped, and assign it to a local
// variable to ensure that it is only dropped after the guard has
// been forgotten. (In case the Drop impl of the closure, or that
// of any consumed captured variable, panics).
let _dropfn = ptr::read(&*guard.dropfn);
mem::forget(guard);
// Drop the closure after `value` has been read, so that if the
// closure's `drop` function panics, unwinding still tries to drop
// `value`.
ManuallyDrop::drop(&mut guard.dropfn);
value
}
}
Expand Down

0 comments on commit cf4a287

Please sign in to comment.