Skip to content

Commit cbdb616

Browse files
authored
Unrolled build for #148755
Rollup merge of #148755 - nxsaken:const_drop_guard, r=dtolnay Constify `DropGuard::dismiss` and trait impls Feature: `drop_guard` (#144426), `const_convert` (#143773), `const_drop_guard` (no tracking issue yet) Constifies `DropGuard::dismiss` and trait impls. I reused `const_convert` (#143773) for the `Deref*` impls.
2 parents 08de25c + 0ecf91a commit cbdb616

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

library/core/src/mem/drop_guard.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::fmt::{self, Debug};
2+
use crate::marker::Destruct;
23
use crate::mem::ManuallyDrop;
34
use crate::ops::{Deref, DerefMut};
45

@@ -78,32 +79,37 @@ where
7879
///
7980
/// let value = String::from("Nori likes chicken");
8081
/// let guard = DropGuard::new(value, |s| println!("{s}"));
81-
/// assert_eq!(guard.dismiss(), "Nori likes chicken");
82+
/// assert_eq!(DropGuard::dismiss(guard), "Nori likes chicken");
8283
/// ```
8384
#[unstable(feature = "drop_guard", issue = "144426")]
85+
#[rustc_const_unstable(feature = "const_drop_guard", issue = "none")]
8486
#[inline]
85-
pub fn dismiss(self) -> T {
87+
pub const fn dismiss(guard: Self) -> T
88+
where
89+
F: [const] Destruct,
90+
{
8691
// First we ensure that dropping the guard will not trigger
8792
// its destructor
88-
let mut this = ManuallyDrop::new(self);
93+
let mut guard = ManuallyDrop::new(guard);
8994

9095
// Next we manually read the stored value from the guard.
9196
//
9297
// SAFETY: this is safe because we've taken ownership of the guard.
93-
let value = unsafe { ManuallyDrop::take(&mut this.inner) };
98+
let value = unsafe { ManuallyDrop::take(&mut guard.inner) };
9499

95100
// Finally we drop the stored closure. We do this *after* having read
96101
// the value, so that even if the closure's `drop` function panics,
97102
// unwinding still tries to drop the value.
98103
//
99104
// SAFETY: this is safe because we've taken ownership of the guard.
100-
unsafe { ManuallyDrop::drop(&mut this.f) };
105+
unsafe { ManuallyDrop::drop(&mut guard.f) };
101106
value
102107
}
103108
}
104109

105110
#[unstable(feature = "drop_guard", issue = "144426")]
106-
impl<T, F> Deref for DropGuard<T, F>
111+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
112+
impl<T, F> const Deref for DropGuard<T, F>
107113
where
108114
F: FnOnce(T),
109115
{
@@ -115,7 +121,8 @@ where
115121
}
116122

117123
#[unstable(feature = "drop_guard", issue = "144426")]
118-
impl<T, F> DerefMut for DropGuard<T, F>
124+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
125+
impl<T, F> const DerefMut for DropGuard<T, F>
119126
where
120127
F: FnOnce(T),
121128
{
@@ -125,9 +132,10 @@ where
125132
}
126133

127134
#[unstable(feature = "drop_guard", issue = "144426")]
128-
impl<T, F> Drop for DropGuard<T, F>
135+
#[rustc_const_unstable(feature = "const_drop_guard", issue = "none")]
136+
impl<T, F> const Drop for DropGuard<T, F>
129137
where
130-
F: FnOnce(T),
138+
F: [const] FnOnce(T),
131139
{
132140
fn drop(&mut self) {
133141
// SAFETY: `DropGuard` is in the process of being dropped.

library/coretests/tests/mem.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ fn drop_guard_into_inner() {
815815
let dropped = Cell::new(false);
816816
let value = DropGuard::new(42, |_| dropped.set(true));
817817
let guard = DropGuard::new(value, |_| dropped.set(true));
818-
let inner = guard.dismiss();
818+
let inner = DropGuard::dismiss(guard);
819819
assert_eq!(dropped.get(), false);
820820
assert_eq!(*inner, 42);
821821
}
@@ -837,7 +837,7 @@ fn drop_guard_always_drops_value_if_closure_drop_unwinds() {
837837
// run the destructor of the value we passed, which we validate.
838838
let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
839839
let guard = DropGuard::new(value_with_tracked_destruction, closure_that_panics_on_drop);
840-
guard.dismiss();
840+
DropGuard::dismiss(guard);
841841
}));
842842
assert!(value_was_dropped);
843843
}

0 commit comments

Comments
 (0)