about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--library/std/src/panicking.rs18
1 files changed, 11 insertions, 7 deletions
diff --git a/library/std/src/panicking.rs b/library/std/src/panicking.rs
index 6d6aca01561..53df0bb40b5 100644
--- a/library/std/src/panicking.rs
+++ b/library/std/src/panicking.rs
@@ -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>");
 
@@ -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