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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use crate::{backtrace::Backtrace, context::Context};
use std::{
any::Any,
fmt,
panic::{self, UnwindSafe},
};
#[inline]
pub fn maybe_unwind<F, R>(f: F) -> Result<R, Unwind>
where
F: FnOnce() -> R + UnwindSafe,
{
let mut captured: Option<Captured> = None;
let mut ctx = Context {
captured: &mut captured,
};
let res = with_set_ctx!(&mut ctx, { panic::catch_unwind(f) });
res.map_err(|payload| Unwind {
payload,
captured: captured.take(),
})
}
#[derive(Debug)]
pub struct Unwind {
payload: Box<dyn Any + Send + 'static>,
captured: Option<Captured>,
}
#[derive(Debug)]
pub(crate) struct Captured {
pub(crate) location: Option<Location>,
pub(crate) backtrace: Option<Backtrace>,
}
impl Unwind {
#[inline]
pub fn payload(&self) -> &(dyn Any + Send + 'static) {
&*self.payload
}
#[inline]
pub fn payload_str(&self) -> &str {
let payload = self.payload();
(payload.downcast_ref::<&str>().copied())
.or_else(|| payload.downcast_ref::<String>().map(|s| s.as_str()))
.unwrap_or_else(|| "Box<dyn Any>")
}
#[inline]
pub fn into_payload(self) -> Box<dyn Any + Send + 'static> {
self.payload
}
#[inline]
pub fn location(&self) -> Option<&Location> {
self.captured.as_ref()?.location.as_ref()
}
#[cfg(backtrace)]
#[inline]
pub fn backtrace(&self) -> Option<&Backtrace> {
self.captured.as_ref()?.backtrace.as_ref()
}
}
impl fmt::Display for Unwind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let msg = self.payload_str();
if !f.alternate() {
return f.write_str(msg);
}
if let Some(location) = self.location() {
writeln!(f, "panicked at {}: {}", location, msg)?;
} else {
writeln!(f, "panicked: {}", msg)?;
}
#[cfg(backtrace)]
{
use std::backtrace::BacktraceStatus;
if let Some(backtrace) = self.backtrace() {
if let BacktraceStatus::Captured = backtrace.status() {
writeln!(f, "stack backtrace:")?;
writeln!(f, "{}", backtrace)?;
}
}
}
Ok(())
}
}
#[derive(Debug)]
pub struct Location {
file: String,
line: u32,
column: u32,
}
impl Location {
#[inline]
pub(crate) fn from_std(loc: &panic::Location<'_>) -> Self {
Self {
file: loc.file().to_string(),
line: loc.line(),
column: loc.column(),
}
}
#[inline]
pub fn file(&self) -> &str {
self.file.as_str()
}
#[inline]
pub fn line(&self) -> u32 {
self.line
}
#[inline]
pub fn column(&self) -> u32 {
self.column
}
}
impl fmt::Display for Location {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}:{}", self.file, self.line, self.column)
}
}