Add DelogPanic trait and delog_panic macro

This commit is contained in:
Robin Krahl
2024-10-14 17:16:15 +02:00
parent bdb2b12160
commit c2e43b8abf
2 changed files with 52 additions and 0 deletions
+2
View File
@@ -113,11 +113,13 @@ pub mod hex;
mod logger;
mod macros;
mod panic;
pub mod render;
pub use logger::{
dequeue, enqueue, try_enqueue, Delogger, State, Statistics, TryLog, TryLogWithStatistics,
};
pub use panic::DelogPanic;
/// A way to pass on logs, user supplied.
///
+50
View File
@@ -0,0 +1,50 @@
use core::fmt::Debug;
#[macro_export]
macro_rules! delog_panic {
($($arg:tt)*) => {{
error_now!($($arg)*);
panic!();
}};
}
pub trait DelogPanic<T> {
fn delog_unwrap(self) -> T;
fn delog_expect(self, msg: &str) -> T;
}
impl<T> DelogPanic<T> for Option<T> {
#[inline(always)]
fn delog_unwrap(self) -> T {
match self {
Some(t) => t,
None => delog_panic!("Called `Option::delog_unwrap` on a `None` value"),
}
}
#[inline(always)]
fn delog_expect(self, _msg: &str) -> T {
match self {
Some(t) => t,
None => delog_panic!("{_msg}"),
}
}
}
impl<T, E: Debug> DelogPanic<T> for Result<T, E> {
#[inline(always)]
fn delog_unwrap(self) -> T {
match self {
Ok(t) => t,
Err(_e) => delog_panic!("Called `Result::delog_unwrap` on an `Err` value: {_e:?}"),
}
}
#[inline(always)]
fn delog_expect(self, _msg: &str) -> T {
match self {
Ok(t) => t,
Err(_e) => delog_panic!("{_msg}: {_e:?}"),
}
}
}