Add an unstable PanicInfo::message(&self) -> Option<&fmt::Arguments> method

This commit is contained in:
Simon Sapin 2018-01-23 17:24:19 +01:00
parent 2f98f4b12b
commit 9e96c1ef7f
2 changed files with 18 additions and 2 deletions

View File

@ -15,6 +15,7 @@
issue = "44489")] issue = "44489")]
use any::Any; use any::Any;
use fmt;
/// A struct providing information about a panic. /// A struct providing information about a panic.
/// ///
@ -38,6 +39,7 @@ use any::Any;
#[derive(Debug)] #[derive(Debug)]
pub struct PanicInfo<'a> { pub struct PanicInfo<'a> {
payload: &'a (Any + Send), payload: &'a (Any + Send),
message: Option<&'a fmt::Arguments<'a>>,
location: Location<'a>, location: Location<'a>,
} }
@ -47,8 +49,11 @@ impl<'a> PanicInfo<'a> {
and related macros", and related macros",
issue = "0")] issue = "0")]
#[doc(hidden)] #[doc(hidden)]
pub fn internal_constructor(payload: &'a (Any + Send), location: Location<'a>,) -> Self { pub fn internal_constructor(payload: &'a (Any + Send),
PanicInfo { payload, location } message: Option<&'a fmt::Arguments<'a>>,
location: Location<'a>)
-> Self {
PanicInfo { payload, location, message }
} }
/// Returns the payload associated with the panic. /// Returns the payload associated with the panic.
@ -73,6 +78,16 @@ impl<'a> PanicInfo<'a> {
self.payload 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, /// Returns information about the location from which the panic originated,
/// if available. /// if available.
/// ///

View File

@ -391,6 +391,7 @@ fn rust_panic_with_hook(msg: Box<Any + Send>,
unsafe { unsafe {
let info = PanicInfo::internal_constructor( let info = PanicInfo::internal_constructor(
&*msg, &*msg,
None,
Location::internal_constructor(file, line, col), Location::internal_constructor(file, line, col),
); );
HOOK_LOCK.read(); HOOK_LOCK.read();