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