@@ -19,7 +19,16 @@ use crate::ops::{Deref, DerefMut};
1919/// {
2020/// // Create a new guard that will do something
2121/// // when dropped.
22- /// let _guard = defer! { println!("Goodbye, world!") };
22+ /// defer! { println!("Goodbye, world!") };
23+ ///
24+ /// // The guard will be dropped here, printing:
25+ /// // "Goodbye, world!"
26+ /// }
27+ ///
28+ /// {
29+ /// // Create a new guard that will do something
30+ /// // when dropped.
31+ /// let _guard = DropGuard::new(|| println!("Goodbye, world!"));
2332///
2433/// // The guard will be dropped here, printing:
2534/// // "Goodbye, world!"
@@ -29,7 +38,7 @@ use crate::ops::{Deref, DerefMut};
2938/// // Create a new guard around a string that will
3039/// // print its value when dropped.
3140/// let s = String::from("Chashu likes tuna");
32- /// let mut s = DropGuard::new (s, |s| println!("{s}"));
41+ /// let mut s = DropGuard::with_value (s, |s| println!("{s}"));
3342///
3443/// // Modify the string contained in the guard.
3544/// s.push_str("!!!");
@@ -41,15 +50,18 @@ use crate::ops::{Deref, DerefMut};
4150#[ unstable( feature = "drop_guard" , issue = "144426" ) ]
4251#[ doc( alias = "ScopeGuard" ) ]
4352#[ doc( alias = "defer" ) ]
44- pub struct DropGuard < T , F >
53+ pub struct DropGuard < T = ( ) , F = UnitFn < fn ( ) > >
4554where
4655 F : FnOnce ( T ) ,
4756{
4857 inner : ManuallyDrop < T > ,
4958 f : ManuallyDrop < F > ,
5059}
5160
52- /// Create a new instance of `DropGuard` with a cleanup closure.
61+ /// Create an anonymous `DropGuard` with a cleanup closure.
62+ ///
63+ /// The macro takes statements, which are the body of a closure
64+ /// that will run when the scope is exited.
5365///
5466/// # Example
5567///
@@ -59,11 +71,34 @@ where
5971///
6072/// use std::mem::defer;
6173///
62- /// let _guard = defer! { println!("Goodbye, world!") };
74+ /// defer! { println!("Goodbye, world!") };
6375/// ```
6476#[ unstable( feature = "drop_guard" , issue = "144426" ) ]
6577pub macro defer( $( $t: tt) * ) {
66- $crate:: mem:: DropGuard :: new ( ( ) , |( ) | { $( $t) * } )
78+ let _guard = $crate:: mem:: DropGuard :: new ( || { $( $t) * } ) ;
79+ }
80+
81+ impl < F > DropGuard < ( ) , UnitFn < F > >
82+ where
83+ F : FnOnce ( ) ,
84+ {
85+ /// Create a new instance of `DropGuard` with a cleanup closure.
86+ ///
87+ /// # Example
88+ ///
89+ /// ```rust
90+ /// # #![allow(unused)]
91+ /// #![feature(drop_guard)]
92+ ///
93+ /// use std::mem::DropGuard;
94+ ///
95+ /// let guard = DropGuard::new(|| println!("Goodbye, world!"));
96+ /// ```
97+ #[ unstable( feature = "drop_guard" , issue = "144426" ) ]
98+ #[ must_use]
99+ pub const fn new ( f : F ) -> Self {
100+ Self { inner : ManuallyDrop :: new ( ( ) ) , f : ManuallyDrop :: new ( UnitFn ( f) ) }
101+ }
67102}
68103
69104impl < T , F > DropGuard < T , F >
@@ -81,11 +116,11 @@ where
81116 /// use std::mem::DropGuard;
82117 ///
83118 /// let value = String::from("Chashu likes tuna");
84- /// let guard = DropGuard::new (value, |s| println!("{s}"));
119+ /// let guard = DropGuard::with_value (value, |s| println!("{s}"));
85120 /// ```
86121 #[ unstable( feature = "drop_guard" , issue = "144426" ) ]
87122 #[ must_use]
88- pub const fn new ( inner : T , f : F ) -> Self {
123+ pub const fn with_value ( inner : T , f : F ) -> Self {
89124 Self { inner : ManuallyDrop :: new ( inner) , f : ManuallyDrop :: new ( f) }
90125 }
91126
@@ -105,7 +140,7 @@ where
105140 /// use std::mem::DropGuard;
106141 ///
107142 /// let value = String::from("Nori likes chicken");
108- /// let guard = DropGuard::new (value, |s| println!("{s}"));
143+ /// let guard = DropGuard::with_value (value, |s| println!("{s}"));
109144 /// assert_eq!(DropGuard::dismiss(guard), "Nori likes chicken");
110145 /// ```
111146 #[ unstable( feature = "drop_guard" , issue = "144426" ) ]
@@ -185,3 +220,28 @@ where
185220 fmt:: Debug :: fmt ( & * * self , f)
186221 }
187222}
223+
224+ #[ unstable( feature = "drop_guard" , issue = "144426" ) ]
225+ pub struct UnitFn < F > ( F ) ;
226+
227+ #[ unstable( feature = "drop_guard" , issue = "144426" ) ]
228+ impl < F > FnOnce < ( ( ) , ) > for UnitFn < F >
229+ where
230+ F : FnOnce ( ) ,
231+ {
232+ type Output = ( ) ;
233+
234+ extern "rust-call" fn call_once ( self , _args : ( ( ) , ) ) -> Self :: Output {
235+ ( self . 0 ) ( )
236+ }
237+ }
238+
239+ #[ unstable( feature = "drop_guard" , issue = "144426" ) ]
240+ impl < F > Debug for UnitFn < F >
241+ where
242+ F : FnOnce ( ) ,
243+ {
244+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
245+ write ! ( f, "UnitFn" )
246+ }
247+ }
0 commit comments