diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-05-17 03:01:30 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2021-05-17 03:16:58 +0200 |
| commit | f7858631d8cd876b41f02f2f4d6613b6d34bc68a (patch) | |
| tree | 11bcf1b7392893efd901bf1e4fe513d90ec75544 /embassy-stm32/src/fmt.rs | |
| parent | cd0d3c4b0da528a10c9ca510e5863bc8779fd28f (diff) | |
stm32: fix build, add ci
Diffstat (limited to 'embassy-stm32/src/fmt.rs')
| -rw-r--r-- | embassy-stm32/src/fmt.rs | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/embassy-stm32/src/fmt.rs b/embassy-stm32/src/fmt.rs new file mode 100644 index 000000000..160642ccd --- /dev/null +++ b/embassy-stm32/src/fmt.rs | |||
| @@ -0,0 +1,114 @@ | |||
| 1 | #![macro_use] | ||
| 2 | #![allow(clippy::module_inception)] | ||
| 3 | #![allow(unused)] | ||
| 4 | |||
| 5 | #[cfg(all(feature = "defmt", feature = "log"))] | ||
| 6 | compile_error!("You may not enable both `defmt` and `log` features."); | ||
| 7 | |||
| 8 | pub use fmt::*; | ||
| 9 | |||
| 10 | #[cfg(feature = "defmt")] | ||
| 11 | mod fmt { | ||
| 12 | pub use defmt::{ | ||
| 13 | assert, assert_eq, assert_ne, debug, debug_assert, debug_assert_eq, debug_assert_ne, error, | ||
| 14 | info, panic, todo, trace, unreachable, unwrap, warn, | ||
| 15 | }; | ||
| 16 | } | ||
| 17 | |||
| 18 | #[cfg(feature = "log")] | ||
| 19 | mod fmt { | ||
| 20 | pub use core::{ | ||
| 21 | assert, assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne, panic, todo, | ||
| 22 | unreachable, | ||
| 23 | }; | ||
| 24 | pub use log::{debug, error, info, trace, warn}; | ||
| 25 | } | ||
| 26 | |||
| 27 | #[cfg(not(any(feature = "defmt", feature = "log")))] | ||
| 28 | mod fmt { | ||
| 29 | #![macro_use] | ||
| 30 | |||
| 31 | pub use core::{ | ||
| 32 | assert, assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne, panic, todo, | ||
| 33 | unreachable, | ||
| 34 | }; | ||
| 35 | |||
| 36 | macro_rules! trace { | ||
| 37 | ($($msg:expr),+ $(,)?) => { | ||
| 38 | () | ||
| 39 | }; | ||
| 40 | } | ||
| 41 | |||
| 42 | macro_rules! debug { | ||
| 43 | ($($msg:expr),+ $(,)?) => { | ||
| 44 | () | ||
| 45 | }; | ||
| 46 | } | ||
| 47 | |||
| 48 | macro_rules! info { | ||
| 49 | ($($msg:expr),+ $(,)?) => { | ||
| 50 | () | ||
| 51 | }; | ||
| 52 | } | ||
| 53 | |||
| 54 | macro_rules! warn { | ||
| 55 | ($($msg:expr),+ $(,)?) => { | ||
| 56 | () | ||
| 57 | }; | ||
| 58 | } | ||
| 59 | |||
| 60 | macro_rules! error { | ||
| 61 | ($($msg:expr),+ $(,)?) => { | ||
| 62 | () | ||
| 63 | }; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | #[cfg(not(feature = "defmt"))] | ||
| 68 | macro_rules! unwrap { | ||
| 69 | ($arg:expr) => { | ||
| 70 | match $crate::fmt::Try::into_result($arg) { | ||
| 71 | ::core::result::Result::Ok(t) => t, | ||
| 72 | ::core::result::Result::Err(e) => { | ||
| 73 | ::core::panic!("unwrap of `{}` failed: {:?}", ::core::stringify!($arg), e); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | }; | ||
| 77 | ($arg:expr, $($msg:expr),+ $(,)? ) => { | ||
| 78 | match $crate::fmt::Try::into_result($arg) { | ||
| 79 | ::core::result::Result::Ok(t) => t, | ||
| 80 | ::core::result::Result::Err(e) => { | ||
| 81 | ::core::panic!("unwrap of `{}` failed: {}: {:?}", ::core::stringify!($arg), ::core::format_args!($($msg,)*), e); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] | ||
| 88 | pub struct NoneError; | ||
| 89 | |||
| 90 | pub trait Try { | ||
| 91 | type Ok; | ||
| 92 | type Error; | ||
| 93 | fn into_result(self) -> Result<Self::Ok, Self::Error>; | ||
| 94 | } | ||
| 95 | |||
| 96 | impl<T> Try for Option<T> { | ||
| 97 | type Ok = T; | ||
| 98 | type Error = NoneError; | ||
| 99 | |||
| 100 | #[inline] | ||
| 101 | fn into_result(self) -> Result<T, NoneError> { | ||
| 102 | self.ok_or(NoneError) | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | impl<T, E> Try for Result<T, E> { | ||
| 107 | type Ok = T; | ||
| 108 | type Error = E; | ||
| 109 | |||
| 110 | #[inline] | ||
| 111 | fn into_result(self) -> Self { | ||
| 112 | self | ||
| 113 | } | ||
| 114 | } | ||
