11use crate :: fmt:: { self , Debug } ;
2+ use crate :: marker:: Destruct ;
23use crate :: mem:: ManuallyDrop ;
34use 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 >
107113where
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 >
119126where
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 >
129137where
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.
0 commit comments