From 30cdb688481f8d21698d0601c2cdb2ff5afdae78 Mon Sep 17 00:00:00 2001 From: Yosh Date: Thu, 6 Nov 2025 17:27:12 +0100 Subject: [PATCH 1/3] `DropGuard::into_inner` -> `DropGuard::dismiss` Also changes it from a static method to an instance method. --- library/core/src/mem/drop_guard.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/library/core/src/mem/drop_guard.rs b/library/core/src/mem/drop_guard.rs index fecc94b815e97..314277bcd95fe 100644 --- a/library/core/src/mem/drop_guard.rs +++ b/library/core/src/mem/drop_guard.rs @@ -63,13 +63,10 @@ where /// Consumes the `DropGuard`, returning the wrapped value. /// - /// This will not execute the closure. This is implemented as an associated - /// function to prevent any potential conflicts with any other methods called - /// `into_inner` from the `Deref` and `DerefMut` impls. - /// - /// It is typically preferred to call this function instead of `mem::forget` - /// because it will return the stored value and drop variables captured - /// by the closure instead of leaking their owned resources. + /// This will not execute the closure. It is typically preferred to call + /// this function instead of `mem::forget` because it will return the stored + /// value and drop variables captured by the closure instead of leaking their + /// owned resources. /// /// # Example /// @@ -81,11 +78,11 @@ where /// /// let value = String::from("Nori likes chicken"); /// let guard = DropGuard::new(value, |s| println!("{s}")); - /// assert_eq!(DropGuard::into_inner(guard), "Nori likes chicken"); + /// assert_eq!(guard.dismiss(), "Nori likes chicken"); /// ``` #[unstable(feature = "drop_guard", issue = "144426")] #[inline] - pub fn into_inner(guard: Self) -> T { + pub fn dismiss(self) -> T { // First we ensure that dropping the guard will not trigger // its destructor let mut guard = ManuallyDrop::new(guard); From 6f8a1a6082d574bf9bbaeb413fd5f1b0fa55b29a Mon Sep 17 00:00:00 2001 From: Yosh Date: Thu, 6 Nov 2025 17:51:59 +0100 Subject: [PATCH 2/3] Fix tests --- library/coretests/tests/mem.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/coretests/tests/mem.rs b/library/coretests/tests/mem.rs index e896c61ef4881..00582109aa2c5 100644 --- a/library/coretests/tests/mem.rs +++ b/library/coretests/tests/mem.rs @@ -815,7 +815,7 @@ fn drop_guard_into_inner() { let dropped = Cell::new(false); let value = DropGuard::new(42, |_| dropped.set(true)); let guard = DropGuard::new(value, |_| dropped.set(true)); - let inner = DropGuard::into_inner(guard); + let inner = guard.dismiss(); assert_eq!(dropped.get(), false); assert_eq!(*inner, 42); } @@ -837,7 +837,7 @@ fn drop_guard_always_drops_value_if_closure_drop_unwinds() { // run the destructor of the value we passed, which we validate. let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { let guard = DropGuard::new(value_with_tracked_destruction, closure_that_panics_on_drop); - DropGuard::into_inner(guard); + guard.dismiss(); })); assert!(value_was_dropped); } From a15c21ecdbb4fa083a0116130864e93a904fd7ab Mon Sep 17 00:00:00 2001 From: Yosh Date: Thu, 6 Nov 2025 18:44:49 +0100 Subject: [PATCH 3/3] Update drop_guard.rs --- library/core/src/mem/drop_guard.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/core/src/mem/drop_guard.rs b/library/core/src/mem/drop_guard.rs index 314277bcd95fe..1301dedf1241a 100644 --- a/library/core/src/mem/drop_guard.rs +++ b/library/core/src/mem/drop_guard.rs @@ -85,19 +85,19 @@ where pub fn dismiss(self) -> T { // First we ensure that dropping the guard will not trigger // its destructor - let mut guard = ManuallyDrop::new(guard); + let mut this = ManuallyDrop::new(self); // Next we manually read the stored value from the guard. // // SAFETY: this is safe because we've taken ownership of the guard. - let value = unsafe { ManuallyDrop::take(&mut guard.inner) }; + let value = unsafe { ManuallyDrop::take(&mut this.inner) }; // Finally we drop the stored closure. We do this *after* having read // the value, so that even if the closure's `drop` function panics, // unwinding still tries to drop the value. // // SAFETY: this is safe because we've taken ownership of the guard. - unsafe { ManuallyDrop::drop(&mut guard.f) }; + unsafe { ManuallyDrop::drop(&mut this.f) }; value } }