File tree 1 file changed +13
-13
lines changed
1 file changed +13
-13
lines changed Original file line number Diff line number Diff line change @@ -252,20 +252,20 @@ fn default_hook(info: &PanicInfo<'_>) {
252
252
let name = thread. as_ref ( ) . and_then ( |t| t. name ( ) ) . unwrap_or ( "<unnamed>" ) ;
253
253
254
254
let write = |err : & mut dyn crate :: io:: Write | {
255
- // Use the panic message directly if available, otherwise take it from
256
- // the payload.
257
- if let Some ( msg) = info. message ( ) {
258
- let _ = writeln ! ( err, "thread '{name}' panicked at {location}:\n {msg}" ) ;
255
+ // The std panic runtime always sets a `&str` or `String` payload for `panic!` and related
256
+ // macros with the formatted message.
257
+ // We try using the payload first to avoid formatting the message twice.
258
+ let msg: & dyn fmt:: Display = if let Some ( s) = info. payload ( ) . downcast_ref :: < & ' static str > ( )
259
+ {
260
+ s
261
+ } else if let Some ( s) = info. payload ( ) . downcast_ref :: < String > ( ) {
262
+ s
263
+ } else if let Some ( msg) = info. message ( ) {
264
+ msg
259
265
} else {
260
- let msg = if let Some ( s) = info. payload ( ) . downcast_ref :: < & ' static str > ( ) {
261
- * s
262
- } else if let Some ( s) = info. payload ( ) . downcast_ref :: < String > ( ) {
263
- & s[ ..]
264
- } else {
265
- "Box<dyn Any>"
266
- } ;
267
- let _ = writeln ! ( err, "thread '{name}' panicked at {location}:\n {msg}" ) ;
268
- }
266
+ & "Box<dyn Any>"
267
+ } ;
268
+ let _ = writeln ! ( err, "thread '{name}' panicked at {location}:\n {msg}" ) ;
269
269
270
270
static FIRST_PANIC : AtomicBool = AtomicBool :: new ( true ) ;
271
271
You can’t perform that action at this time.
0 commit comments