forked from 1jehuang/jcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpanic_util.rs
More file actions
28 lines (25 loc) · 964 Bytes
/
panic_util.rs
File metadata and controls
28 lines (25 loc) · 964 Bytes
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
pub(crate) fn panic_payload_to_string(payload: &(dyn std::any::Any + Send)) -> String {
if let Some(s) = payload.downcast_ref::<&str>() {
(*s).to_string()
} else if let Some(s) = payload.downcast_ref::<String>() {
s.clone()
} else {
"unknown panic payload".to_string()
}
}
#[cfg(test)]
mod tests {
use super::panic_payload_to_string;
#[test]
fn panic_payload_to_string_handles_common_payloads() {
let str_payload: &(dyn std::any::Any + Send) = &"borrowed panic";
let string_payload: &(dyn std::any::Any + Send) = &String::from("owned panic");
let unknown_payload: &(dyn std::any::Any + Send) = &42usize;
assert_eq!(panic_payload_to_string(str_payload), "borrowed panic");
assert_eq!(panic_payload_to_string(string_payload), "owned panic");
assert_eq!(
panic_payload_to_string(unknown_payload),
"unknown panic payload"
);
}
}