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