@@ -129,6 +129,9 @@ pub trait RefRecoverSafe {}
129
129
///
130
130
/// # Examples
131
131
///
132
+ /// One way to use `AssertRecoverSafe` is to assert that the entire closure
133
+ /// itself is recover safe, bypassing all checks for all variables:
134
+ ///
132
135
/// ```
133
136
/// #![feature(recover, std_panic)]
134
137
///
@@ -144,10 +147,33 @@ pub trait RefRecoverSafe {}
144
147
/// // });
145
148
///
146
149
/// // This, however, will compile due to the `AssertRecoverSafe` wrapper
150
+ /// let result = panic::recover(AssertRecoverSafe::new(|| {
151
+ /// variable += 3;
152
+ /// }));
153
+ /// // ...
154
+ /// ```
155
+ ///
156
+ /// Wrapping the entire closure amounts to a blanket assertion that all captured
157
+ /// variables are recover safe. This has the downside that if new captures are
158
+ /// added in the future, they will also be considered recover safe. Therefore,
159
+ /// you may prefer to just wrap individual captures, as shown below. This is
160
+ /// more annotation, but it ensures that if a new capture is added which is not
161
+ /// recover safe, you will get a compilation error at that time, which will
162
+ /// allow you to consider whether that new capture in fact represent a bug or
163
+ /// not.
164
+ ///
165
+ /// ```
166
+ /// #![feature(recover, std_panic)]
167
+ ///
168
+ /// use std::panic::{self, AssertRecoverSafe};
169
+ ///
170
+ /// let mut variable = 4;
171
+ /// let other_capture = 3;
172
+ ///
147
173
/// let result = {
148
174
/// let mut wrapper = AssertRecoverSafe::new(&mut variable);
149
175
/// panic::recover(move || {
150
- /// **wrapper += 3 ;
176
+ /// **wrapper += other_capture ;
151
177
/// })
152
178
/// };
153
179
/// // ...
@@ -215,6 +241,14 @@ impl<T> DerefMut for AssertRecoverSafe<T> {
215
241
}
216
242
}
217
243
244
+ impl < R , F : FnOnce ( ) -> R > FnOnce < ( ) > for AssertRecoverSafe < F > {
245
+ type Output = R ;
246
+
247
+ extern "rust-call" fn call_once ( self , _args : ( ) ) -> R {
248
+ ( self . 0 ) ( )
249
+ }
250
+ }
251
+
218
252
/// Invokes a closure, capturing the cause of panic if one occurs.
219
253
///
220
254
/// This function will return `Ok` with the closure's result if the closure
0 commit comments