1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//!Extension library to panic facilities to make it more usable
//!
//!- `alloc` - Enables `String` usage via `alloc`. This is useful until [message](https://doc.rust-lang.org/std/panic/struct.PanicInfo.html#method.message) is stable

#![no_std]
#![warn(missing_docs)]
#![allow(clippy::style)]

#[cfg(feature = "alloc")]
extern crate alloc;

use core::fmt;
use core::panic::{Location, PanicInfo};

///Panic's message definition
pub trait Message: fmt::Display + fmt::Debug {}

impl<T: fmt::Display + fmt::Debug> Message for T {}

#[inline(always)]
///Retrieves panic message from the dynamic payload
///
///Note that while it accepts any type, it is designed to work with panic's payload
pub fn downcast_payload<'a>(payload: &'a (dyn core::any::Any + Send + 'static)) -> &'a dyn Message {
    const DEFAULT_MESSAGE: &'static str = "panic occurred";
    match payload.downcast_ref::<&'static str>() {
        Some(message) => message,
        #[cfg(feature = "alloc")]
        None => match payload.downcast_ref::<alloc::string::String>() {
            Some(message) => message,
            None => &DEFAULT_MESSAGE,
        },
        #[cfg(not(feature = "alloc"))]
        None => &DEFAULT_MESSAGE,
    }
}

#[inline(always)]
///Retrieves panic message from the panic info
pub fn get_message<'a>(panic: &'a PanicInfo<'a>) -> &'a dyn Message {
    downcast_payload(panic.payload())
}

#[derive(Clone, Copy, Debug)]
///Panic details
pub struct PanicDetails<'a> {
    ///Panic location
    ///
    ///Location is optional in PanicInfo.
    ///
    ///Therefore if for some reason standard library shall remove it,
    ///instead it will be initialized with location of where message function is called
    pub location: &'a Location<'a>,
    ///Panic message which can string or
    ///[core::fmt::Arguments](https://doc.rust-lang.org/core/fmt/struct.Arguments.html)
    pub message: &'a dyn Message,
}

///Extension trait to provide better API for PanicInfo
pub trait PanicInfoExt<'a> {
    ///Access uniform details of panic
    fn panic_details(&'a self) -> PanicDetails<'a>;
}

impl<'a> PanicInfoExt<'a> for PanicInfo<'a> {
    #[track_caller]
    #[inline(always)]
    fn panic_details(&'a self) -> PanicDetails<'a> {
        let location = match self.location() {
            Some(location) => location,
            None => Location::caller(),
        };
        PanicDetails {
            location,
            message: get_message(self),
        }
    }
}

impl<'a> PanicInfoExt<'a> for &'a (dyn core::any::Any + Send + 'static) {
    #[track_caller]
    #[inline(always)]
    ///Retrieves panic details from the raw payload
    ///As raw payload doesn't have location information
    ///It shall be initialized from where this function is called
    fn panic_details(&'a self) -> PanicDetails<'a> {
        PanicDetails {
            location: Location::caller(),
            message: downcast_payload(*self),
        }
    }
}

#[cfg(feature = "alloc")]
impl<'a> PanicInfoExt<'a> for alloc::boxed::Box<dyn core::any::Any + Send + 'static> {
    #[track_caller]
    #[inline(always)]
    ///Retrieves panic details from the raw payload
    ///As raw payload doesn't have location information
    ///It shall be initialized from where this function is called
    fn panic_details(&'a self) -> PanicDetails<'a> {
        PanicDetails {
            location: Location::caller(),
            message: downcast_payload(self),
        }
    }
}

impl fmt::Display for PanicDetails<'_> {
    #[inline(always)]
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.location, fmt)?;
        fmt.write_str(": ")?;
        fmt::Display::fmt(self.message, fmt)
    }
}