1use std::fmt::Debug;
2
3#[macro_export]
4macro_rules! unwrap_msg (
5 ($e:expr) => (
6 ($e).unwrap_log(file!(), line!(), module_path!(), stringify!($e))
7 )
8);
9
10pub trait UnwrapLog<T> {
11 fn unwrap_log(self, file: &str, line: u32, module_path: &str, expression: &str) -> T;
12}
13
14impl<T> UnwrapLog<T> for Option<T> {
15 fn unwrap_log(self, file: &str, line: u32, module_path: &str, expression: &str) -> T {
16 match self {
17 Some(t) => t,
18 None => panic!(
19 "{file}:{line} {module_path} this should not have panicked:\ntried to unwrap `None` in:\nunwrap_msg!({expression})"
20 ),
21 }
22 }
23}
24
25impl<T, E: Debug> UnwrapLog<T> for Result<T, E> {
26 fn unwrap_log(self, file: &str, line: u32, module_path: &str, expression: &str) -> T {
27 match self {
28 Ok(t) => t,
29 Err(e) => panic!(
30 "{file}:{line} {module_path} this should not have panicked:\ntried to unwrap Err({e:?}) in\nunwrap_msg!({expression})"
31 ),
32 }
33 }
34}
35
36#[macro_export]
37macro_rules! assert_size (
38 ($t:ty, $sz:expr) => (
39 assert_eq!(::std::mem::size_of::<$t>(), $sz);
40 );
41);