RKTK API Docs RKTK Home Repo

rktk_log/
macros.rs

1// HACK: Nested macro_rules
2// https://github.com/rust-lang/rust/issues/35853#issuecomment-415993963
3#[rustfmt::skip]
4macro_rules! defmt_or_core {
5    ($d:tt $name:ident) => {
6        #[macro_export]
7        macro_rules! $name {
8            ($d($d x:tt)*) => {
9                #[cfg(not(feature = "defmt"))]
10                ::core::$name!($d($d x)*);
11
12                #[cfg(feature = "defmt")]
13                ::defmt::$name!($d($d x)*);
14            }
15        }
16    }
17}
18
19#[rustfmt::skip]
20macro_rules! defmt_or_log_or_noop {
21    ($d:tt $name:ident) => {
22        #[macro_export]
23        macro_rules! $name {
24            ($s:literal $d(, $d x:expr)* $d(,)?) => {
25                #[cfg(feature = "defmt")]
26                ::defmt::$name!($s $d(, $d x)*);
27
28                #[cfg(feature = "log")]
29                ::log::$name!($s $d(, $d x)*);
30
31                #[cfg(all(not(feature = "defmt"), not(feature = "log")))]
32                let _ = ($d( & $d x ),*);
33            }
34        }
35    }
36}
37
38defmt_or_core!($ assert);
39defmt_or_core!($ assert_eq);
40defmt_or_core!($ assert_ne);
41defmt_or_core!($ debug_assert);
42defmt_or_core!($ debug_assert_eq);
43defmt_or_core!($ debug_assert_ne);
44defmt_or_core!($ todo);
45defmt_or_core!($ unreachable);
46defmt_or_core!($ panic);
47
48defmt_or_log_or_noop!($ trace);
49defmt_or_log_or_noop!($ debug);
50defmt_or_log_or_noop!($ info);
51defmt_or_log_or_noop!($ warn);
52defmt_or_log_or_noop!($ error);
53
54#[macro_export]
55macro_rules! intern {
56    ($s:literal) => {
57        #[cfg(not(feature = "defmt"))]
58        $s
59        #[cfg(feature = "defmt")]
60        ::defmt::intern!($s)
61    };
62}
63
64#[macro_export]
65macro_rules! unwrap {
66    ($arg:expr) => {
67        #[cfg(feature = "defmt")]
68        ::defmt::unwrap!($arg)
69
70        #[cfg(not(feature = "defmt"))]
71        match $crate::macros::unwrap_helper::Try::into_result($arg) {
72            ::core::result::Result::Ok(t) => t,
73            ::core::result::Result::Err(e) => {
74                ::core::panic!("unwrap of `{}` failed: {:?}", ::core::stringify!($arg), e);
75            }
76        }
77    };
78    ($arg:expr, $($msg:expr),+ $(,)? ) => {
79        #[cfg(feature = "defmt")]
80        ::defmt::unwrap!($arg:expr, $($msg:expr),+)
81
82        #[cfg(not(feature = "defmt"))]
83        match $crate::macros::unwrap_helper::Try::into_result($arg) {
84            ::core::result::Result::Ok(t) => t,
85            ::core::result::Result::Err(e) => {
86                ::core::panic!("unwrap of `{}` failed: {}: {:?}", ::core::stringify!($arg), ::core::format_args!($($msg,)*), e);
87            }
88        }
89    }
90}
91
92pub mod unwrap_helper {
93    #[derive(Debug, Copy, Clone, Eq, PartialEq)]
94    pub struct NoneError;
95
96    pub trait Try {
97        type Ok;
98        type Error;
99        fn into_result(self) -> Result<Self::Ok, Self::Error>;
100    }
101
102    impl<T> Try for Option<T> {
103        type Ok = T;
104        type Error = NoneError;
105
106        #[inline]
107        fn into_result(self) -> Result<T, NoneError> {
108            self.ok_or(NoneError)
109        }
110    }
111
112    impl<T, E> Try for Result<T, E> {
113        type Ok = T;
114        type Error = E;
115
116        #[inline]
117        fn into_result(self) -> Self {
118            self
119        }
120    }
121}