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 = "defmt")]
107 ::defmt::trace!($s $(, $x)*);
108 #[cfg(not(feature="defmt"))]
109 let _ = ($( & $x ),*);
110 }
111 };
112}
113
114macro_rules! debug {
115 ($s:literal $(, $x:expr)* $(,)?) => {
116 {
117 #[cfg(feature = "defmt")]
118 ::defmt::debug!($s $(, $x)*);
119 #[cfg(not(feature="defmt"))]
120 let _ = ($( & $x ),*);
121 }
122 };
123}
124
125macro_rules! info {
126 ($s:literal $(, $x:expr)* $(,)?) => {
127 {
128 #[cfg(feature = "defmt")]
129 ::defmt::info!($s $(, $x)*);
130 #[cfg(not(feature="defmt"))]
131 let _ = ($( & $x ),*);
132 }
133 };
134}
135
136macro_rules! warn {
137 ($s:literal $(, $x:expr)* $(,)?) => {
138 {
139 #[cfg(feature = "defmt")]
140 ::defmt::warn!($s $(, $x)*);
141 #[cfg(not(feature="defmt"))]
142 let _ = ($( & $x ),*);
143 }
144 };
145}
146
147macro_rules! error {
148 ($s:literal $(, $x:expr)* $(,)?) => {
149 {
150 #[cfg(feature = "defmt")]
151 ::defmt::error!($s $(, $x)*);
152 #[cfg(not(feature="defmt"))]
153 let _ = ($( & $x ),*);
154 }
155 };
156}
157
158#[cfg(feature = "defmt")]
159macro_rules! unwrap {
160 ($($x:tt)*) => {
161 ::defmt::unwrap!($($x)*)
162 };
163}
164
165#[cfg(not(feature = "defmt"))]
166macro_rules! unwrap {
167 ($arg:expr) => {
168 match $crate::fmt::Try::into_result($arg) {
169 ::core::result::Result::Ok(t) => t,
170 ::core::result::Result::Err(e) => {
171 ::core::panic!("unwrap of `{}` failed: {:?}", ::core::stringify!($arg), e);
172 }
173 }
174 };
175 ($arg:expr, $($msg:expr),+ $(,)? ) => {
176 match $crate::fmt::Try::into_result($arg) {
177 ::core::result::Result::Ok(t) => t,
178 ::core::result::Result::Err(e) => {
179 ::core::panic!("unwrap of `{}` failed: {}: {:?}", ::core::stringify!($arg), ::core::format_args!($($msg,)*), e);
180 }
181 }
182 }
183}
184
185#[derive(Debug, Copy, Clone, Eq, PartialEq)]
186pub struct NoneError;
187
188pub trait Try {
189 type Ok;
190 type Error;
191 fn into_result(self) -> Result<Self::Ok, Self::Error>;
192}
193
194impl<T> Try for Option<T> {
195 type Ok = T;
196 type Error = NoneError;
197
198 #[inline]
199 fn into_result(self) -> Result<T, NoneError> {
200 self.ok_or(NoneError)
201 }
202}
203
204impl<T, E> Try for Result<T, E> {
205 type Ok = T;
206 type Error = E;
207
208 #[inline]
209 fn into_result(self) -> Self {
210 self
211 }
212}