1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Panic support in the standard library.
13 #![unstable(feature = "core_panic_info",
14 reason = "newly available in libcore",
19 /// A struct providing information about a panic.
21 /// `PanicInfo` structure is passed to a panic hook set by the [`set_hook`]
24 /// [`set_hook`]: ../../std/panic/fn.set_hook.html
31 /// panic::set_hook(Box::new(|panic_info| {
32 /// println!("panic occurred: {:?}", panic_info.payload().downcast_ref::<&str>().unwrap());
35 /// panic!("Normal panic");
37 #[stable(feature = "panic_hooks", since = "1.10.0")]
39 pub struct PanicInfo<'a> {
40 payload: &'a (Any + Send),
41 location: Location<'a>,
44 impl<'a> PanicInfo<'a> {
45 #![unstable(feature = "panic_internals",
46 reason = "internal details of the implementation of the `panic!` \
50 pub fn internal_constructor(payload: &'a (Any + Send), location: Location<'a>,) -> Self {
51 PanicInfo { payload, location }
54 /// Returns the payload associated with the panic.
56 /// This will commonly, but not always, be a `&'static str` or [`String`].
58 /// [`String`]: ../../std/string/struct.String.html
65 /// panic::set_hook(Box::new(|panic_info| {
66 /// println!("panic occurred: {:?}", panic_info.payload().downcast_ref::<&str>().unwrap());
69 /// panic!("Normal panic");
71 #[stable(feature = "panic_hooks", since = "1.10.0")]
72 pub fn payload(&self) -> &(Any + Send) {
76 /// Returns information about the location from which the panic originated,
79 /// This method will currently always return [`Some`], but this may change
80 /// in future versions.
82 /// [`Some`]: ../../std/option/enum.Option.html#variant.Some
89 /// panic::set_hook(Box::new(|panic_info| {
90 /// if let Some(location) = panic_info.location() {
91 /// println!("panic occurred in file '{}' at line {}", location.file(),
94 /// println!("panic occurred but can't get location information...");
98 /// panic!("Normal panic");
100 #[stable(feature = "panic_hooks", since = "1.10.0")]
101 pub fn location(&self) -> Option<&Location> {
102 // NOTE: If this is changed to sometimes return None,
103 // deal with that case in std::panicking::default_hook.
108 /// A struct containing information about the location of a panic.
110 /// This structure is created by the [`location`] method of [`PanicInfo`].
112 /// [`location`]: ../../std/panic/struct.PanicInfo.html#method.location
113 /// [`PanicInfo`]: ../../std/panic/struct.PanicInfo.html
120 /// panic::set_hook(Box::new(|panic_info| {
121 /// if let Some(location) = panic_info.location() {
122 /// println!("panic occurred in file '{}' at line {}", location.file(), location.line());
124 /// println!("panic occurred but can't get location information...");
128 /// panic!("Normal panic");
131 #[stable(feature = "panic_hooks", since = "1.10.0")]
132 pub struct Location<'a> {
138 impl<'a> Location<'a> {
139 #![unstable(feature = "panic_internals",
140 reason = "internal details of the implementation of the `panic!` \
144 pub fn internal_constructor(file: &'a str, line: u32, col: u32) -> Self {
145 Location { file, line, col }
148 /// Returns the name of the source file from which the panic originated.
155 /// panic::set_hook(Box::new(|panic_info| {
156 /// if let Some(location) = panic_info.location() {
157 /// println!("panic occurred in file '{}'", location.file());
159 /// println!("panic occurred but can't get location information...");
163 /// panic!("Normal panic");
165 #[stable(feature = "panic_hooks", since = "1.10.0")]
166 pub fn file(&self) -> &str {
170 /// Returns the line number from which the panic originated.
177 /// panic::set_hook(Box::new(|panic_info| {
178 /// if let Some(location) = panic_info.location() {
179 /// println!("panic occurred at line {}", location.line());
181 /// println!("panic occurred but can't get location information...");
185 /// panic!("Normal panic");
187 #[stable(feature = "panic_hooks", since = "1.10.0")]
188 pub fn line(&self) -> u32 {
192 /// Returns the column from which the panic originated.
199 /// panic::set_hook(Box::new(|panic_info| {
200 /// if let Some(location) = panic_info.location() {
201 /// println!("panic occurred at column {}", location.column());
203 /// println!("panic occurred but can't get location information...");
207 /// panic!("Normal panic");
209 #[stable(feature = "panic_col", since = "1.25")]
210 pub fn column(&self) -> u32 {