Skip to content

Commit 8fd3010

Browse files
committed
Add second batch of feedback from review
1 parent 7b4d181 commit 8fd3010

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

library/core/src/mem/drop_guard.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use crate::ptr;
2727
/// }
2828
/// ```
2929
#[unstable(feature = "drop_guard", issue = "none")]
30-
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
3130
#[doc(alias = "ScopeGuard")]
3231
#[doc(alias = "defer")]
3332
pub struct DropGuard<T, F = fn(T)>

library/coretests/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#![feature(core_private_diy_float)]
3131
#![feature(cstr_display)]
3232
#![feature(dec2flt)]
33+
#![feature(drop_guard)]
3334
#![feature(duration_constants)]
3435
#![feature(duration_constructors)]
3536
#![feature(duration_constructors_lite)]

library/coretests/tests/mem.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,3 +819,24 @@ fn drop_guard_into_inner() {
819819
assert_eq!(dropped.get(), false);
820820
assert_eq!(*inner, 42);
821821
}
822+
823+
#[test]
824+
fn drop_guard_always_drops_value_if_closure_drop_unwinds() {
825+
// Create a value with a destructor, which we will validate ran successfully.
826+
let mut value_was_dropped = false;
827+
let value_with_tracked_destruction = DropGuard::new((), |_| value_was_dropped = true);
828+
829+
// Create a closure that will begin unwinding when dropped.
830+
let drop_bomb = DropGuard::new((), |_| panic!());
831+
let closure_that_panics_on_drop = move |_| {
832+
let _drop_bomb = drop_bomb;
833+
};
834+
835+
// This will run the closure, which will panic when dropped. This should
836+
// run the destructor of the value we passed, which we validate.
837+
let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
838+
let guard = DropGuard::new(value_with_tracked_destruction, closure_that_panics_on_drop);
839+
DropGuard::into_inner(guard);
840+
}));
841+
assert!(value_was_dropped);
842+
}

0 commit comments

Comments
 (0)