Skip to content

Commit

Permalink
Move downcasting panic payload to str to a function.
Browse files Browse the repository at this point in the history
  • Loading branch information
m-ou-se committed Jun 11, 2024
1 parent a18eeac commit cf984e0
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions std/src/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,7 @@ fn default_hook(info: &PanicInfo<'_>) {
// The current implementation always returns `Some`.
let location = info.location().unwrap();

let msg = match info.payload().downcast_ref::<&'static str>() {
Some(s) => *s,
None => match info.payload().downcast_ref::<String>() {
Some(s) => &s[..],
None => "Box<dyn Any>",
},
};
let msg = payload_as_str(info.payload());
let thread = thread::try_current();
let name = thread.as_ref().and_then(|t| t.name()).unwrap_or("<unnamed>");

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

fn payload_as_str(payload: &dyn Any) -> &str {
if let Some(&s) = payload.downcast_ref::<&'static str>() {
s
} else if let Some(s) = payload.downcast_ref::<String>() {
s.as_str()
} else {
"Box<dyn Any>"
}
}

/// Central point for dispatching panics.
///
/// Executes the primary logic for a panic, including checking for recursive
Expand Down

0 comments on commit cf984e0

Please sign in to comment.