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