Skip to content

Commit d9b5ae9

Browse files
Add missing examples for panicking objects
1 parent 908dba0 commit d9b5ae9

File tree

1 file changed

+104
-2
lines changed

1 file changed

+104
-2
lines changed

src/libstd/panicking.rs

+104-2
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,23 @@ pub fn take_hook() -> Box<Fn(&PanicInfo) + 'static + Sync + Send> {
159159
}
160160

161161
/// 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+
/// ```
162179
#[stable(feature = "panic_hooks", since = "1.10.0")]
163180
pub struct PanicInfo<'a> {
164181
payload: &'a (Any + Send),
@@ -168,7 +185,21 @@ pub struct PanicInfo<'a> {
168185
impl<'a> PanicInfo<'a> {
169186
/// Returns the payload associated with the panic.
170187
///
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+
/// ```
172203
#[stable(feature = "panic_hooks", since = "1.10.0")]
173204
pub fn payload(&self) -> &(Any + Send) {
174205
self.payload
@@ -177,15 +208,54 @@ impl<'a> PanicInfo<'a> {
177208
/// Returns information about the location from which the panic originated,
178209
/// if available.
179210
///
180-
/// This method will currently always return `Some`, but this may change
211+
/// This method will currently always return [`Some`], but this may change
181212
/// 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+
/// ```
182231
#[stable(feature = "panic_hooks", since = "1.10.0")]
183232
pub fn location(&self) -> Option<&Location> {
184233
Some(&self.location)
185234
}
186235
}
187236

188237
/// 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+
/// ```
189259
#[stable(feature = "panic_hooks", since = "1.10.0")]
190260
pub struct Location<'a> {
191261
file: &'a str,
@@ -194,12 +264,44 @@ pub struct Location<'a> {
194264

195265
impl<'a> Location<'a> {
196266
/// 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+
/// ```
197283
#[stable(feature = "panic_hooks", since = "1.10.0")]
198284
pub fn file(&self) -> &str {
199285
self.file
200286
}
201287

202288
/// 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+
/// ```
203305
#[stable(feature = "panic_hooks", since = "1.10.0")]
204306
pub fn line(&self) -> u32 {
205307
self.line

0 commit comments

Comments
 (0)