Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ManuallyDrop instead of mem::forget in into_inner #25

Merged
merged 3 commits into from
Aug 9, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
willtunnels marked this conversation as resolved.
Show resolved Hide resolved
let 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);
// Read the closure so that it gets dropped. Do this after value has
// also been read so that, if the closure's drop function panics,
// unwinding still tries to drop value.
ptr::read(&*guard.dropfn)
willtunnels marked this conversation as resolved.
Show resolved Hide resolved
value
}
}
Expand Down