esp_wifi/
fmt.rs

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