Skip to content

Commit

Permalink
Add an unstable PanicInfo::message(&self) -> Option<&fmt::Arguments> …
Browse files Browse the repository at this point in the history
…method
  • Loading branch information
SimonSapin committed Jan 23, 2018
1 parent 2f98f4b commit 9e96c1e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/libcore/panic.rs
Expand Up @@ -15,6 +15,7 @@
issue = "44489")]

use any::Any;
use fmt;

/// A struct providing information about a panic.
///
Expand All @@ -38,6 +39,7 @@ use any::Any;
#[derive(Debug)]
pub struct PanicInfo<'a> {
payload: &'a (Any + Send),
message: Option<&'a fmt::Arguments<'a>>,
location: Location<'a>,
}

Expand All @@ -47,8 +49,11 @@ impl<'a> PanicInfo<'a> {
and related macros",
issue = "0")]
#[doc(hidden)]
pub fn internal_constructor(payload: &'a (Any + Send), location: Location<'a>,) -> Self {
PanicInfo { payload, location }
pub fn internal_constructor(payload: &'a (Any + Send),
message: Option<&'a fmt::Arguments<'a>>,
location: Location<'a>)
-> Self {
PanicInfo { payload, location, message }
}

/// Returns the payload associated with the panic.
Expand All @@ -73,6 +78,16 @@ impl<'a> PanicInfo<'a> {
self.payload
}

/// If the `panic!` macro from the `core` crate (not from `std`)
/// was used with a formatting string and some additional arguments,
/// returns that message ready to be used for example with [`fmt::write`]
///
/// [`fmt::write`]: ../fmt/fn.write.html
#[unstable(feature = "panic_info_message", issue = "44489")]
pub fn message(&self) -> Option<&fmt::Arguments> {
self.message
}

/// Returns information about the location from which the panic originated,
/// if available.
///
Expand Down
1 change: 1 addition & 0 deletions src/libstd/panicking.rs
Expand Up @@ -391,6 +391,7 @@ fn rust_panic_with_hook(msg: Box<Any + Send>,
unsafe {
let info = PanicInfo::internal_constructor(
&*msg,
None,
Location::internal_constructor(file, line, col),
);
HOOK_LOCK.read();
Expand Down

0 comments on commit 9e96c1e

Please sign in to comment.