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