From e7dc5c0939d30ccba98418c42799c4e39f646d23 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Mon, 7 Jun 2021 00:10:54 +0200 Subject: fmt: make all macros `macro_rules` so scoping is consistent. --- embassy-extras/src/fmt.rs | 191 +++++++++++++++++++++++++++++--------- embassy-extras/src/ring_buffer.rs | 2 - 2 files changed, 146 insertions(+), 47 deletions(-) (limited to 'embassy-extras/src') diff --git a/embassy-extras/src/fmt.rs b/embassy-extras/src/fmt.rs index 160642ccd..6c5063f7c 100644 --- a/embassy-extras/src/fmt.rs +++ b/embassy-extras/src/fmt.rs @@ -1,67 +1,168 @@ #![macro_use] -#![allow(clippy::module_inception)] -#![allow(unused)] +#![allow(unused_macros)] #[cfg(all(feature = "defmt", feature = "log"))] compile_error!("You may not enable both `defmt` and `log` features."); -pub use fmt::*; +macro_rules! assert { + ($($x:tt)*) => { + { + #[cfg(not(feature = "defmt"))] + core::assert!($($x)*); + #[cfg(feature = "defmt")] + defmt::assert!($($x)*); + } + }; +} -#[cfg(feature = "defmt")] -mod fmt { - pub use defmt::{ - assert, assert_eq, assert_ne, debug, debug_assert, debug_assert_eq, debug_assert_ne, error, - info, panic, todo, trace, unreachable, unwrap, warn, +macro_rules! assert_eq { + ($($x:tt)*) => { + { + #[cfg(not(feature = "defmt"))] + core::assert_eq!($($x)*); + #[cfg(feature = "defmt")] + defmt::assert_eq!($($x)*); + } }; } -#[cfg(feature = "log")] -mod fmt { - pub use core::{ - assert, assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne, panic, todo, - unreachable, +macro_rules! assert_ne { + ($($x:tt)*) => { + { + #[cfg(not(feature = "defmt"))] + core::assert_ne!($($x)*); + #[cfg(feature = "defmt")] + defmt::assert_ne!($($x)*); + } }; - pub use log::{debug, error, info, trace, warn}; } -#[cfg(not(any(feature = "defmt", feature = "log")))] -mod fmt { - #![macro_use] +macro_rules! debug_assert { + ($($x:tt)*) => { + { + #[cfg(not(feature = "defmt"))] + core::debug_assert!($($x)*); + #[cfg(feature = "defmt")] + defmt::debug_assert!($($x)*); + } + }; +} - pub use core::{ - assert, assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne, panic, todo, - unreachable, +macro_rules! debug_assert_eq { + ($($x:tt)*) => { + { + #[cfg(not(feature = "defmt"))] + core::debug_assert_eq!($($x)*); + #[cfg(feature = "defmt")] + defmt::debug_assert_eq!($($x)*); + } }; +} - macro_rules! trace { - ($($msg:expr),+ $(,)?) => { - () - }; - } +macro_rules! debug_assert_ne { + ($($x:tt)*) => { + { + #[cfg(not(feature = "defmt"))] + core::debug_assert_ne!($($x)*); + #[cfg(feature = "defmt")] + defmt::debug_assert_ne!($($x)*); + } + }; +} - macro_rules! debug { - ($($msg:expr),+ $(,)?) => { - () - }; - } +macro_rules! todo { + ($($x:tt)*) => { + { + #[cfg(not(feature = "defmt"))] + core::todo!($($x)*); + #[cfg(feature = "defmt")] + defmt::todo!($($x)*); + } + }; +} - macro_rules! info { - ($($msg:expr),+ $(,)?) => { - () - }; - } +macro_rules! unreachable { + ($($x:tt)*) => { + { + #[cfg(not(feature = "defmt"))] + core::unreachable!($($x)*); + #[cfg(feature = "defmt")] + defmt::unreachable!($($x)*); + } + }; +} - macro_rules! warn { - ($($msg:expr),+ $(,)?) => { - () - }; - } +macro_rules! panic { + ($($x:tt)*) => { + { + #[cfg(not(feature = "defmt"))] + core::panic!($($x)*); + #[cfg(feature = "defmt")] + defmt::panic!($($x)*); + } + }; +} - macro_rules! error { - ($($msg:expr),+ $(,)?) => { - () - }; - } +macro_rules! trace { + ($($x:tt)*) => { + { + #[cfg(feature = "log")] + log::trace!($($x)*); + #[cfg(feature = "defmt")] + defmt::trace!($($x)*); + } + }; +} + +macro_rules! debug { + ($($x:tt)*) => { + { + #[cfg(fevature = "log")] + log::debug!($($x)*); + #[cfg(feature = "defmt")] + defmt::debug!($($x)*); + } + }; +} + +macro_rules! info { + ($($x:tt)*) => { + { + #[cfg(feature = "log")] + log::info!($($x)*); + #[cfg(feature = "defmt")] + defmt::info!($($x)*); + } + }; +} + +macro_rules! warn { + ($($x:tt)*) => { + { + #[cfg(feature = "log")] + log::warn!($($x)*); + #[cfg(feature = "defmt")] + defmt::warn!($($x)*); + } + }; +} + +macro_rules! error { + ($($x:tt)*) => { + { + #[cfg(feature = "log")] + log::error!($($x)*); + #[cfg(feature = "defmt")] + defmt::error!($($x)*); + } + }; +} + +#[cfg(feature = "defmt")] +macro_rules! unwrap { + ($($x:tt)*) => { + defmt::unwrap!($($x)*) + }; } #[cfg(not(feature = "defmt"))] diff --git a/embassy-extras/src/ring_buffer.rs b/embassy-extras/src/ring_buffer.rs index dafdd958a..18795787f 100644 --- a/embassy-extras/src/ring_buffer.rs +++ b/embassy-extras/src/ring_buffer.rs @@ -1,5 +1,3 @@ -use crate::fmt::assert; - pub struct RingBuffer<'a> { buf: &'a mut [u8], start: usize, -- cgit From ef1ebefec0c682553213406b3e59a30a79520291 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Mon, 7 Jun 2021 03:15:05 +0200 Subject: fmt: use absolute paths --- embassy-extras/src/fmt.rs | 58 +++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 29 deletions(-) (limited to 'embassy-extras/src') diff --git a/embassy-extras/src/fmt.rs b/embassy-extras/src/fmt.rs index 6c5063f7c..2646c57ab 100644 --- a/embassy-extras/src/fmt.rs +++ b/embassy-extras/src/fmt.rs @@ -8,9 +8,9 @@ macro_rules! assert { ($($x:tt)*) => { { #[cfg(not(feature = "defmt"))] - core::assert!($($x)*); + ::core::assert!($($x)*); #[cfg(feature = "defmt")] - defmt::assert!($($x)*); + ::defmt::assert!($($x)*); } }; } @@ -19,9 +19,9 @@ macro_rules! assert_eq { ($($x:tt)*) => { { #[cfg(not(feature = "defmt"))] - core::assert_eq!($($x)*); + ::core::assert_eq!($($x)*); #[cfg(feature = "defmt")] - defmt::assert_eq!($($x)*); + ::defmt::assert_eq!($($x)*); } }; } @@ -30,9 +30,9 @@ macro_rules! assert_ne { ($($x:tt)*) => { { #[cfg(not(feature = "defmt"))] - core::assert_ne!($($x)*); + ::core::assert_ne!($($x)*); #[cfg(feature = "defmt")] - defmt::assert_ne!($($x)*); + ::defmt::assert_ne!($($x)*); } }; } @@ -41,9 +41,9 @@ macro_rules! debug_assert { ($($x:tt)*) => { { #[cfg(not(feature = "defmt"))] - core::debug_assert!($($x)*); + ::core::debug_assert!($($x)*); #[cfg(feature = "defmt")] - defmt::debug_assert!($($x)*); + ::defmt::debug_assert!($($x)*); } }; } @@ -52,9 +52,9 @@ macro_rules! debug_assert_eq { ($($x:tt)*) => { { #[cfg(not(feature = "defmt"))] - core::debug_assert_eq!($($x)*); + ::core::debug_assert_eq!($($x)*); #[cfg(feature = "defmt")] - defmt::debug_assert_eq!($($x)*); + ::defmt::debug_assert_eq!($($x)*); } }; } @@ -63,9 +63,9 @@ macro_rules! debug_assert_ne { ($($x:tt)*) => { { #[cfg(not(feature = "defmt"))] - core::debug_assert_ne!($($x)*); + ::core::debug_assert_ne!($($x)*); #[cfg(feature = "defmt")] - defmt::debug_assert_ne!($($x)*); + ::defmt::debug_assert_ne!($($x)*); } }; } @@ -74,9 +74,9 @@ macro_rules! todo { ($($x:tt)*) => { { #[cfg(not(feature = "defmt"))] - core::todo!($($x)*); + ::core::todo!($($x)*); #[cfg(feature = "defmt")] - defmt::todo!($($x)*); + ::defmt::todo!($($x)*); } }; } @@ -85,9 +85,9 @@ macro_rules! unreachable { ($($x:tt)*) => { { #[cfg(not(feature = "defmt"))] - core::unreachable!($($x)*); + ::core::unreachable!($($x)*); #[cfg(feature = "defmt")] - defmt::unreachable!($($x)*); + ::defmt::unreachable!($($x)*); } }; } @@ -96,9 +96,9 @@ macro_rules! panic { ($($x:tt)*) => { { #[cfg(not(feature = "defmt"))] - core::panic!($($x)*); + ::core::panic!($($x)*); #[cfg(feature = "defmt")] - defmt::panic!($($x)*); + ::defmt::panic!($($x)*); } }; } @@ -107,9 +107,9 @@ macro_rules! trace { ($($x:tt)*) => { { #[cfg(feature = "log")] - log::trace!($($x)*); + ::log::trace!($($x)*); #[cfg(feature = "defmt")] - defmt::trace!($($x)*); + ::defmt::trace!($($x)*); } }; } @@ -118,9 +118,9 @@ macro_rules! debug { ($($x:tt)*) => { { #[cfg(fevature = "log")] - log::debug!($($x)*); + ::log::debug!($($x)*); #[cfg(feature = "defmt")] - defmt::debug!($($x)*); + ::defmt::debug!($($x)*); } }; } @@ -129,9 +129,9 @@ macro_rules! info { ($($x:tt)*) => { { #[cfg(feature = "log")] - log::info!($($x)*); + ::log::info!($($x)*); #[cfg(feature = "defmt")] - defmt::info!($($x)*); + ::defmt::info!($($x)*); } }; } @@ -140,9 +140,9 @@ macro_rules! warn { ($($x:tt)*) => { { #[cfg(feature = "log")] - log::warn!($($x)*); + ::log::warn!($($x)*); #[cfg(feature = "defmt")] - defmt::warn!($($x)*); + ::defmt::warn!($($x)*); } }; } @@ -151,9 +151,9 @@ macro_rules! error { ($($x:tt)*) => { { #[cfg(feature = "log")] - log::error!($($x)*); + ::log::error!($($x)*); #[cfg(feature = "defmt")] - defmt::error!($($x)*); + ::defmt::error!($($x)*); } }; } @@ -161,7 +161,7 @@ macro_rules! error { #[cfg(feature = "defmt")] macro_rules! unwrap { ($($x:tt)*) => { - defmt::unwrap!($($x)*) + ::defmt::unwrap!($($x)*) }; } -- cgit From 3be49d3e794d5819574fe33ffbfda9da1ddbe216 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Mon, 7 Jun 2021 03:21:09 +0200 Subject: fmt: Add dunmy use to avoid "unused variable" errors when no log is enabled. --- embassy-extras/src/fmt.rs | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) (limited to 'embassy-extras/src') diff --git a/embassy-extras/src/fmt.rs b/embassy-extras/src/fmt.rs index 2646c57ab..066970813 100644 --- a/embassy-extras/src/fmt.rs +++ b/embassy-extras/src/fmt.rs @@ -104,56 +104,66 @@ macro_rules! panic { } macro_rules! trace { - ($($x:tt)*) => { + ($s:literal $(, $x:expr)* $(,)?) => { { #[cfg(feature = "log")] - ::log::trace!($($x)*); + ::log::trace!($s $(, $x)*); #[cfg(feature = "defmt")] - ::defmt::trace!($($x)*); + ::defmt::trace!($s $(, $x)*); + #[cfg(not(any(feature = "log", feature="defmt")))] + let _ = ($( & $x ),*); } }; } macro_rules! debug { - ($($x:tt)*) => { + ($s:literal $(, $x:expr)* $(,)?) => { { - #[cfg(fevature = "log")] - ::log::debug!($($x)*); + #[cfg(feature = "log")] + ::log::debug!($s $(, $x)*); #[cfg(feature = "defmt")] - ::defmt::debug!($($x)*); + ::defmt::debug!($s $(, $x)*); + #[cfg(not(any(feature = "log", feature="defmt")))] + let _ = ($( & $x ),*); } }; } macro_rules! info { - ($($x:tt)*) => { + ($s:literal $(, $x:expr)* $(,)?) => { { #[cfg(feature = "log")] - ::log::info!($($x)*); + ::log::info!($s $(, $x)*); #[cfg(feature = "defmt")] - ::defmt::info!($($x)*); + ::defmt::info!($s $(, $x)*); + #[cfg(not(any(feature = "log", feature="defmt")))] + let _ = ($( & $x ),*); } }; } macro_rules! warn { - ($($x:tt)*) => { + ($s:literal $(, $x:expr)* $(,)?) => { { #[cfg(feature = "log")] - ::log::warn!($($x)*); + ::log::warn!($s $(, $x)*); #[cfg(feature = "defmt")] - ::defmt::warn!($($x)*); + ::defmt::warn!($s $(, $x)*); + #[cfg(not(any(feature = "log", feature="defmt")))] + let _ = ($( & $x ),*); } }; } macro_rules! error { - ($($x:tt)*) => { + ($s:literal $(, $x:expr)* $(,)?) => { { #[cfg(feature = "log")] - ::log::error!($($x)*); + ::log::error!($s $(, $x)*); #[cfg(feature = "defmt")] - ::defmt::error!($($x)*); + ::defmt::error!($s $(, $x)*); + #[cfg(not(any(feature = "log", feature="defmt")))] + let _ = ($( & $x ),*); } }; } -- cgit