@@ -159,6 +159,23 @@ pub fn take_hook() -> Box<Fn(&PanicInfo) + 'static + Sync + Send> {
159
159
}
160
160
161
161
/// A struct providing information about a panic.
162
+ ///
163
+ /// `PanicInfo` structure is passed to a panic hook set by the [`set_hook()`]
164
+ /// function.
165
+ ///
166
+ /// [`set_hook()`]: ../../std/panic/fn.set_hook.html
167
+ ///
168
+ /// # Examples
169
+ ///
170
+ /// ```should_panic
171
+ /// use std::panic;
172
+ ///
173
+ /// panic::set_hook(Box::new(|panic_info| {
174
+ /// println!("panic occured: {:?}", panic_info.payload().downcast_ref::<&str>().unwrap());
175
+ /// }));
176
+ ///
177
+ /// panic!("Normal panic");
178
+ /// ```
162
179
#[ stable( feature = "panic_hooks" , since = "1.10.0" ) ]
163
180
pub struct PanicInfo < ' a > {
164
181
payload : & ' a ( Any + Send ) ,
@@ -168,7 +185,21 @@ pub struct PanicInfo<'a> {
168
185
impl < ' a > PanicInfo < ' a > {
169
186
/// Returns the payload associated with the panic.
170
187
///
171
- /// This will commonly, but not always, be a `&'static str` or `String`.
188
+ /// This will commonly, but not always, be a `&'static str` or [`String`].
189
+ ///
190
+ /// [`String`]: ../../std/string/struct.String.html
191
+ ///
192
+ /// # Examples
193
+ ///
194
+ /// ```should_panic
195
+ /// use std::panic;
196
+ ///
197
+ /// panic::set_hook(Box::new(|panic_info| {
198
+ /// println!("panic occured: {:?}", panic_info.payload().downcast_ref::<&str>().unwrap());
199
+ /// }));
200
+ ///
201
+ /// panic!("Normal panic");
202
+ /// ```
172
203
#[ stable( feature = "panic_hooks" , since = "1.10.0" ) ]
173
204
pub fn payload ( & self ) -> & ( Any + Send ) {
174
205
self . payload
@@ -177,15 +208,54 @@ impl<'a> PanicInfo<'a> {
177
208
/// Returns information about the location from which the panic originated,
178
209
/// if available.
179
210
///
180
- /// This method will currently always return `Some`, but this may change
211
+ /// This method will currently always return [ `Some`] , but this may change
181
212
/// in future versions.
213
+ ///
214
+ /// [`Some`]: ../../std/option/enum.Option.html#variant.Some
215
+ ///
216
+ /// # Examples
217
+ ///
218
+ /// ```should_panic
219
+ /// use std::panic;
220
+ ///
221
+ /// panic::set_hook(Box::new(|panic_info| {
222
+ /// if let Some(location) = panic_info.location() {
223
+ /// println!("panic occured in file '{}' at line {}", location.file(), location.line());
224
+ /// } else {
225
+ /// println!("panic occured but can't get location information...");
226
+ /// }
227
+ /// }));
228
+ ///
229
+ /// panic!("Normal panic");
230
+ /// ```
182
231
#[ stable( feature = "panic_hooks" , since = "1.10.0" ) ]
183
232
pub fn location ( & self ) -> Option < & Location > {
184
233
Some ( & self . location )
185
234
}
186
235
}
187
236
188
237
/// A struct containing information about the location of a panic.
238
+ ///
239
+ /// This structure is created by the [`location()`] method of [`PanicInfo`].
240
+ ///
241
+ /// [`location()`]: ../../std/panic/struct.PanicInfo.html#method.location
242
+ /// [`PanicInfo`]: ../../std/panic/struct.PanicInfo.html
243
+ ///
244
+ /// # Examples
245
+ ///
246
+ /// ```should_panic
247
+ /// use std::panic;
248
+ ///
249
+ /// panic::set_hook(Box::new(|panic_info| {
250
+ /// if let Some(location) = panic_info.location() {
251
+ /// println!("panic occured in file '{}' at line {}", location.file(), location.line());
252
+ /// } else {
253
+ /// println!("panic occured but can't get location information...");
254
+ /// }
255
+ /// }));
256
+ ///
257
+ /// panic!("Normal panic");
258
+ /// ```
189
259
#[ stable( feature = "panic_hooks" , since = "1.10.0" ) ]
190
260
pub struct Location < ' a > {
191
261
file : & ' a str ,
@@ -194,12 +264,44 @@ pub struct Location<'a> {
194
264
195
265
impl < ' a > Location < ' a > {
196
266
/// Returns the name of the source file from which the panic originated.
267
+ ///
268
+ /// # Examples
269
+ ///
270
+ /// ```should_panic
271
+ /// use std::panic;
272
+ ///
273
+ /// panic::set_hook(Box::new(|panic_info| {
274
+ /// if let Some(location) = panic_info.location() {
275
+ /// println!("panic occured in file '{}'", location.file());
276
+ /// } else {
277
+ /// println!("panic occured but can't get location information...");
278
+ /// }
279
+ /// }));
280
+ ///
281
+ /// panic!("Normal panic");
282
+ /// ```
197
283
#[ stable( feature = "panic_hooks" , since = "1.10.0" ) ]
198
284
pub fn file ( & self ) -> & str {
199
285
self . file
200
286
}
201
287
202
288
/// Returns the line number from which the panic originated.
289
+ ///
290
+ /// # Examples
291
+ ///
292
+ /// ```should_panic
293
+ /// use std::panic;
294
+ ///
295
+ /// panic::set_hook(Box::new(|panic_info| {
296
+ /// if let Some(location) = panic_info.location() {
297
+ /// println!("panic occured at line {}", location.line());
298
+ /// } else {
299
+ /// println!("panic occured but can't get location information...");
300
+ /// }
301
+ /// }));
302
+ ///
303
+ /// panic!("Normal panic");
304
+ /// ```
203
305
#[ stable( feature = "panic_hooks" , since = "1.10.0" ) ]
204
306
pub fn line ( & self ) -> u32 {
205
307
self . line
0 commit comments