embedded_tls/
fmt.rs

1#![macro_use]
2#![allow(unused_macros)]
3
4macro_rules! assert {
5    ($($x:tt)*) => {
6        {
7            #[cfg(not(feature = "defmt"))]
8            ::core::assert!($($x)*);
9            #[cfg(feature = "defmt")]
10            ::defmt::assert!($($x)*);
11        }
12    };
13}
14
15macro_rules! assert_eq {
16    ($($x:tt)*) => {
17        {
18            #[cfg(not(feature = "defmt"))]
19            ::core::assert_eq!($($x)*);
20            #[cfg(feature = "defmt")]
21            ::defmt::assert_eq!($($x)*);
22        }
23    };
24}
25
26macro_rules! assert_ne {
27    ($($x:tt)*) => {
28        {
29            #[cfg(not(feature = "defmt"))]
30            ::core::assert_ne!($($x)*);
31            #[cfg(feature = "defmt")]
32            ::defmt::assert_ne!($($x)*);
33        }
34    };
35}
36
37macro_rules! debug_assert {
38    ($($x:tt)*) => {
39        {
40            #[cfg(not(feature = "defmt"))]
41            ::core::debug_assert!($($x)*);
42            #[cfg(feature = "defmt")]
43            ::defmt::debug_assert!($($x)*);
44        }
45    };
46}
47
48macro_rules! debug_assert_eq {
49    ($($x:tt)*) => {
50        {
51            #[cfg(not(feature = "defmt"))]
52            ::core::debug_assert_eq!($($x)*);
53            #[cfg(feature = "defmt")]
54            ::defmt::debug_assert_eq!($($x)*);
55        }
56    };
57}
58
59macro_rules! debug_assert_ne {
60    ($($x:tt)*) => {
61        {
62            #[cfg(not(feature = "defmt"))]
63            ::core::debug_assert_ne!($($x)*);
64            #[cfg(feature = "defmt")]
65            ::defmt::debug_assert_ne!($($x)*);
66        }
67    };
68}
69
70macro_rules! todo {
71    ($($x:tt)*) => {
72        {
73            #[cfg(not(feature = "defmt"))]
74            ::core::todo!($($x)*);
75            #[cfg(feature = "defmt")]
76            ::defmt::todo!($($x)*);
77        }
78    };
79}
80
81macro_rules! unreachable {
82    ($($x:tt)*) => {
83        {
84            #[cfg(not(feature = "defmt"))]
85            ::core::unreachable!($($x)*);
86            #[cfg(feature = "defmt")]
87            ::defmt::unreachable!($($x)*);
88        }
89    };
90}
91
92macro_rules! panic {
93    ($($x:tt)*) => {
94        {
95            #[cfg(not(feature = "defmt"))]
96            ::core::panic!($($x)*);
97            #[cfg(feature = "defmt")]
98            ::defmt::panic!($($x)*);
99        }
100    };
101}
102
103macro_rules! trace {
104    ($s:literal $(, $x:expr)* $(,)?) => {
105        {
106            #[cfg(feature = "log")]
107            ::log::trace!($s $(, $x)*);
108            #[cfg(feature = "defmt")]
109            ::defmt::trace!($s $(, $x)*);
110            #[cfg(not(any(feature = "log", feature="defmt")))]
111            let _ = ($( & $x ),*);
112        }
113    };
114}
115
116macro_rules! debug {
117    ($s:literal $(, $x:expr)* $(,)?) => {
118        {
119            #[cfg(feature = "log")]
120            ::log::debug!($s $(, $x)*);
121            #[cfg(feature = "defmt")]
122            ::defmt::debug!($s $(, $x)*);
123            #[cfg(not(any(feature = "log", feature="defmt")))]
124            let _ = ($( & $x ),*);
125        }
126    };
127}
128
129macro_rules! info {
130    ($s:literal $(, $x:expr)* $(,)?) => {
131        {
132            #[cfg(feature = "log")]
133            ::log::info!($s $(, $x)*);
134            #[cfg(feature = "defmt")]
135            ::defmt::info!($s $(, $x)*);
136            #[cfg(not(any(feature = "log", feature="defmt")))]
137            let _ = ($( & $x ),*);
138        }
139    };
140}
141
142macro_rules! warn {
143    ($s:literal $(, $x:expr)* $(,)?) => {
144        {
145            #[cfg(feature = "log")]
146            ::log::warn!($s $(, $x)*);
147            #[cfg(feature = "defmt")]
148            ::defmt::warn!($s $(, $x)*);
149            #[cfg(not(any(feature = "log", feature="defmt")))]
150            let _ = ($( & $x ),*);
151        }
152    };
153}
154
155macro_rules! error {
156    ($s:literal $(, $x:expr)* $(,)?) => {
157        {
158            #[cfg(feature = "log")]
159            ::log::error!($s $(, $x)*);
160            #[cfg(feature = "defmt")]
161            ::defmt::error!($s $(, $x)*);
162            #[cfg(not(any(feature = "log", feature="defmt")))]
163            let _ = ($( & $x ),*);
164        }
165    };
166}
167
168#[cfg(feature = "defmt")]
169macro_rules! unwrap {
170    ($($x:tt)*) => {
171        ::defmt::unwrap!($($x)*)
172    };
173}
174
175#[cfg(not(feature = "defmt"))]
176macro_rules! unwrap {
177    ($arg:expr) => {
178        match $crate::fmt::Try::into_result($arg) {
179            ::core::result::Result::Ok(t) => t,
180            ::core::result::Result::Err(e) => {
181                ::core::panic!("unwrap of `{}` failed: {:?}", ::core::stringify!($arg), e);
182            }
183        }
184    };
185    ($arg:expr, $($msg:expr),+ $(,)? ) => {
186        match $crate::fmt::Try::into_result($arg) {
187            ::core::result::Result::Ok(t) => t,
188            ::core::result::Result::Err(e) => {
189                ::core::panic!("unwrap of `{}` failed: {}: {:?}", ::core::stringify!($arg), ::core::format_args!($($msg,)*), e);
190            }
191        }
192    }
193}
194
195#[derive(Debug, Copy, Clone, Eq, PartialEq)]
196#[cfg_attr(feature = "defmt", derive(defmt::Format))]
197pub struct NoneError;
198
199pub trait Try {
200    type Ok;
201    type Error;
202    fn into_result(self) -> Result<Self::Ok, Self::Error>;
203}
204
205impl<T> Try for Option<T> {
206    type Ok = T;
207    type Error = NoneError;
208
209    #[inline]
210    fn into_result(self) -> Result<T, NoneError> {
211        self.ok_or(NoneError)
212    }
213}
214
215impl<T, E> Try for Result<T, E> {
216    type Ok = T;
217    type Error = E;
218
219    #[inline]
220    fn into_result(self) -> Self {
221        self
222    }
223}