Repo of no-std crates for my personal embedded projects
at main 4.9 kB view raw
1#![no_std] 2 3#[macro_export] 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 15#[macro_export] 16macro_rules! assert_eq { 17 ($($x:tt)*) => { 18 { 19 #[cfg(not(feature = "defmt"))] 20 ::core::assert_eq!($($x)*); 21 #[cfg(feature = "defmt")] 22 ::defmt::assert_eq!($($x)*); 23 } 24 }; 25} 26 27#[macro_export] 28macro_rules! assert_ne { 29 ($($x:tt)*) => { 30 { 31 #[cfg(not(feature = "defmt"))] 32 ::core::assert_ne!($($x)*); 33 #[cfg(feature = "defmt")] 34 ::defmt::assert_ne!($($x)*); 35 } 36 }; 37} 38 39#[macro_export] 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 51#[macro_export] 52macro_rules! debug_assert_eq { 53 ($($x:tt)*) => { 54 { 55 #[cfg(not(feature = "defmt"))] 56 ::core::debug_assert_eq!($($x)*); 57 #[cfg(feature = "defmt")] 58 ::defmt::debug_assert_eq!($($x)*); 59 } 60 }; 61} 62 63#[macro_export] 64macro_rules! debug_assert_ne { 65 ($($x:tt)*) => { 66 { 67 #[cfg(not(feature = "defmt"))] 68 ::core::debug_assert_ne!($($x)*); 69 #[cfg(feature = "defmt")] 70 ::defmt::debug_assert_ne!($($x)*); 71 } 72 }; 73} 74 75#[macro_export] 76macro_rules! todo { 77 ($($x:tt)*) => { 78 { 79 #[cfg(not(feature = "defmt"))] 80 ::core::todo!($($x)*); 81 #[cfg(feature = "defmt")] 82 ::defmt::todo!($($x)*); 83 } 84 }; 85} 86 87#[macro_export] 88#[cfg(not(feature = "defmt"))] 89macro_rules! unreachable { 90 ($($x:tt)*) => { 91 ::core::unreachable!($($x)*) 92 }; 93} 94 95#[macro_export] 96#[cfg(feature = "defmt")] 97macro_rules! unreachable { 98 ($($x:tt)*) => { 99 ::defmt::unreachable!($($x)*) 100 }; 101} 102 103#[macro_export] 104macro_rules! panic { 105 ($($x:tt)*) => { 106 { 107 #[cfg(not(feature = "defmt"))] 108 ::core::panic!($($x)*); 109 #[cfg(feature = "defmt")] 110 ::defmt::panic!($($x)*); 111 } 112 }; 113} 114 115#[macro_export] 116macro_rules! trace { 117 ($s:literal $(, $x:expr)* $(,)?) => { 118 { 119 #[cfg(feature = "defmt")] 120 ::defmt::trace!($s $(, $x)*); 121 #[cfg(feature="defmt")] 122 let _ = ($( & $x ),*); 123 } 124 }; 125} 126 127#[macro_export] 128macro_rules! debug { 129 ($s:literal $(, $x:expr)* $(,)?) => { 130 { 131 #[cfg(feature = "defmt")] 132 ::defmt::debug!($s $(, $x)*); 133 #[cfg(not(feature="defmt"))] 134 let _ = ($( & $x ),*); 135 } 136 }; 137} 138 139#[macro_export] 140macro_rules! info { 141 ($s:literal $(, $x:expr)* $(,)?) => { 142 { 143 #[cfg(feature = "defmt")] 144 ::defmt::info!($s $(, $x)*); 145 #[cfg(not(feature="defmt"))] 146 let _ = ($( & $x ),*); 147 } 148 }; 149} 150 151#[macro_export] 152macro_rules! _warn { 153 ($s:literal $(, $x:expr)* $(,)?) => { 154 { 155 #[cfg(feature = "defmt")] 156 ::defmt::warn!($s $(, $x)*); 157 #[cfg(not(feature="defmt"))] 158 let _ = ($( & $x ),*); 159 } 160 }; 161} 162 163#[macro_export] 164macro_rules! error { 165 ($s:literal $(, $x:expr)* $(,)?) => { 166 { 167 #[cfg(feature = "defmt")] 168 ::defmt::error!($s $(, $x)*); 169 #[cfg(not(feature="defmt"))] 170 let _ = ($( & $x ),*); 171 } 172 }; 173} 174 175#[macro_export] 176#[cfg(feature = "defmt")] 177macro_rules! unwrap { 178 ($($x:tt)*) => { 179 ::defmt::unwrap!($($x)*) 180 }; 181} 182 183#[macro_export] 184#[cfg(not(feature = "defmt"))] 185macro_rules! unwrap { 186 ($arg:expr) => { 187 match $crate::Try::into_result($arg) { 188 ::core::result::Result::Ok(t) => t, 189 ::core::result::Result::Err(_) => { 190 ::core::panic!(); 191 } 192 } 193 }; 194 ($arg:expr, $($msg:expr),+ $(,)? ) => { 195 match $crate::Try::into_result($arg) { 196 ::core::result::Result::Ok(t) => t, 197 ::core::result::Result::Err(_) => { 198 ::core::panic!(); 199 } 200 } 201 }; 202} 203 204#[derive(Debug, Copy, Clone, Eq, PartialEq)] 205pub struct NoneError; 206 207pub trait Try { 208 type Ok; 209 type Error; 210 fn into_result(self) -> Result<Self::Ok, Self::Error>; 211} 212 213impl<T> Try for Option<T> { 214 type Ok = T; 215 type Error = NoneError; 216 217 #[inline] 218 fn into_result(self) -> Result<T, NoneError> { 219 self.ok_or(NoneError) 220 } 221} 222 223impl<T, E> Try for Result<T, E> { 224 type Ok = T; 225 type Error = E; 226 227 #[inline] 228 fn into_result(self) -> Self { 229 self 230 } 231} 232 233pub use _warn as warn;