sozu_lib/
util.rs

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!("{file}:{line} {module_path} this should not have panicked:\ntried to unwrap `None` in:\nunwrap_msg!({expression})")
19    }
20    }
21}
22
23impl<T, E: Debug> UnwrapLog<T> for Result<T, E> {
24    fn unwrap_log(self, file: &str, line: u32, module_path: &str, expression: &str) -> T {
25        match self {
26      Ok(t)  => t,
27      Err(e) => panic!("{file}:{line} {module_path} this should not have panicked:\ntried to unwrap Err({e:?}) in\nunwrap_msg!({expression})")
28    }
29    }
30}
31
32#[macro_export]
33macro_rules! assert_size (
34  ($t:ty, $sz:expr) => (
35    assert_eq!(::std::mem::size_of::<$t>(), $sz);
36  );
37);