Skip to content

Commit cf984e0

Browse files
committed
Move downcasting panic payload to str to a function.
1 parent a18eeac commit cf984e0

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

std/src/panicking.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,7 @@ fn default_hook(info: &PanicInfo<'_>) {
248248
// The current implementation always returns `Some`.
249249
let location = info.location().unwrap();
250250

251-
let msg = match info.payload().downcast_ref::<&'static str>() {
252-
Some(s) => *s,
253-
None => match info.payload().downcast_ref::<String>() {
254-
Some(s) => &s[..],
255-
None => "Box<dyn Any>",
256-
},
257-
};
251+
let msg = payload_as_str(info.payload());
258252
let thread = thread::try_current();
259253
let name = thread.as_ref().and_then(|t| t.name()).unwrap_or("<unnamed>");
260254

@@ -731,6 +725,16 @@ pub const fn begin_panic<M: Any + Send>(msg: M) -> ! {
731725
}
732726
}
733727

728+
fn payload_as_str(payload: &dyn Any) -> &str {
729+
if let Some(&s) = payload.downcast_ref::<&'static str>() {
730+
s
731+
} else if let Some(s) = payload.downcast_ref::<String>() {
732+
s.as_str()
733+
} else {
734+
"Box<dyn Any>"
735+
}
736+
}
737+
734738
/// Central point for dispatching panics.
735739
///
736740
/// Executes the primary logic for a panic, including checking for recursive

0 commit comments

Comments
 (0)