Skip to content

Commit 9e96c1e

Browse files
committed
Add an unstable PanicInfo::message(&self) -> Option<&fmt::Arguments> method
1 parent 2f98f4b commit 9e96c1e

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/libcore/panic.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
issue = "44489")]
1616

1717
use any::Any;
18+
use fmt;
1819

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

@@ -47,8 +49,11 @@ impl<'a> PanicInfo<'a> {
4749
and related macros",
4850
issue = "0")]
4951
#[doc(hidden)]
50-
pub fn internal_constructor(payload: &'a (Any + Send), location: Location<'a>,) -> Self {
51-
PanicInfo { payload, location }
52+
pub fn internal_constructor(payload: &'a (Any + Send),
53+
message: Option<&'a fmt::Arguments<'a>>,
54+
location: Location<'a>)
55+
-> Self {
56+
PanicInfo { payload, location, message }
5257
}
5358

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

81+
/// If the `panic!` macro from the `core` crate (not from `std`)
82+
/// was used with a formatting string and some additional arguments,
83+
/// returns that message ready to be used for example with [`fmt::write`]
84+
///
85+
/// [`fmt::write`]: ../fmt/fn.write.html
86+
#[unstable(feature = "panic_info_message", issue = "44489")]
87+
pub fn message(&self) -> Option<&fmt::Arguments> {
88+
self.message
89+
}
90+
7691
/// Returns information about the location from which the panic originated,
7792
/// if available.
7893
///

src/libstd/panicking.rs

+1
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ fn rust_panic_with_hook(msg: Box<Any + Send>,
391391
unsafe {
392392
let info = PanicInfo::internal_constructor(
393393
&*msg,
394+
None,
394395
Location::internal_constructor(file, line, col),
395396
);
396397
HOOK_LOCK.read();

0 commit comments

Comments
 (0)