diff options
author | Dario Nieuwenhuis <dirbaio@dirbaio.net> | 2021-06-07 15:31:52 +0200 |
---|---|---|
committer | Dario Nieuwenhuis <dirbaio@dirbaio.net> | 2021-06-07 15:31:52 +0200 |
commit | 0997666bf3d88087e3b2997cdb81377d31bf88c7 (patch) | |
tree | e0b1a536bbc1419c165b633594c83861d2d7fccf | |
parent | 79def1f5365952fdc9ccada9754da1592a20aaaa (diff) | |
download | nrf-softdevice-0997666bf3d88087e3b2997cdb81377d31bf88c7.zip |
fmt: add new macro_rules-based fmt.rs, to make scoping consistent.
-rw-r--r-- | nrf-softdevice/src/ble/central.rs | 1 | ||||
-rw-r--r-- | nrf-softdevice/src/ble/common.rs | 1 | ||||
-rw-r--r-- | nrf-softdevice/src/ble/connection.rs | 1 | ||||
-rw-r--r-- | nrf-softdevice/src/ble/gap.rs | 1 | ||||
-rw-r--r-- | nrf-softdevice/src/ble/gatt_client.rs | 1 | ||||
-rw-r--r-- | nrf-softdevice/src/ble/gatt_server.rs | 1 | ||||
-rw-r--r-- | nrf-softdevice/src/ble/gatt_traits.rs | 2 | ||||
-rw-r--r-- | nrf-softdevice/src/ble/l2cap.rs | 1 | ||||
-rw-r--r-- | nrf-softdevice/src/ble/mod.rs | 1 | ||||
-rw-r--r-- | nrf-softdevice/src/ble/peripheral.rs | 1 | ||||
-rw-r--r-- | nrf-softdevice/src/ble/types.rs | 1 | ||||
-rw-r--r-- | nrf-softdevice/src/events.rs | 1 | ||||
-rw-r--r-- | nrf-softdevice/src/flash.rs | 1 | ||||
-rw-r--r-- | nrf-softdevice/src/fmt.rs | 219 | ||||
-rw-r--r-- | nrf-softdevice/src/softdevice.rs | 1 | ||||
-rw-r--r-- | nrf-softdevice/src/util/drop_bomb.rs | 2 | ||||
-rw-r--r-- | nrf-softdevice/src/util/portal.rs | 1 | ||||
-rw-r--r-- | nrf-softdevice/src/util/signal.rs | 2 |
18 files changed, 167 insertions, 72 deletions
diff --git a/nrf-softdevice/src/ble/central.rs b/nrf-softdevice/src/ble/central.rs index 8318767..992c35b 100644 --- a/nrf-softdevice/src/ble/central.rs +++ b/nrf-softdevice/src/ble/central.rs @@ -9,7 +9,6 @@ use core::ptr; use crate::ble::gap; use crate::ble::types::*; use crate::ble::{Address, Connection}; -use crate::fmt::{assert, unreachable, *}; use crate::raw; use crate::util::{get_union_field, OnDrop, Portal}; use crate::{RawError, Softdevice}; diff --git a/nrf-softdevice/src/ble/common.rs b/nrf-softdevice/src/ble/common.rs index 0ea81eb..5bb61d4 100644 --- a/nrf-softdevice/src/ble/common.rs +++ b/nrf-softdevice/src/ble/common.rs @@ -1,4 +1,3 @@ -use crate::fmt::*; use crate::raw; pub(crate) unsafe fn on_evt(ble_evt: *const raw::ble_evt_t) { diff --git a/nrf-softdevice/src/ble/connection.rs b/nrf-softdevice/src/ble/connection.rs index b75eb25..4c34558 100644 --- a/nrf-softdevice/src/ble/connection.rs +++ b/nrf-softdevice/src/ble/connection.rs @@ -3,7 +3,6 @@ use core::cell::UnsafeCell; use crate::ble::types::*; use crate::ble::*; -use crate::fmt::{assert, *}; use crate::raw; use crate::RawError; diff --git a/nrf-softdevice/src/ble/gap.rs b/nrf-softdevice/src/ble/gap.rs index d574591..b195c5f 100644 --- a/nrf-softdevice/src/ble/gap.rs +++ b/nrf-softdevice/src/ble/gap.rs @@ -2,7 +2,6 @@ use core::mem; use core::ptr; use crate::ble::*; -use crate::fmt::{panic, *}; use crate::raw; use crate::util::get_union_field; use crate::RawError; diff --git a/nrf-softdevice/src/ble/gatt_client.rs b/nrf-softdevice/src/ble/gatt_client.rs index 41718cd..de5c92f 100644 --- a/nrf-softdevice/src/ble/gatt_client.rs +++ b/nrf-softdevice/src/ble/gatt_client.rs @@ -4,7 +4,6 @@ use heapless::Vec; use num_enum::{FromPrimitive, IntoPrimitive}; use crate::ble::*; -use crate::fmt::{assert, assert_ne, panic, unreachable}; use crate::raw; use crate::util::{get_flexarray, get_union_field, Portal}; use crate::RawError; diff --git a/nrf-softdevice/src/ble/gatt_server.rs b/nrf-softdevice/src/ble/gatt_server.rs index ccb17bb..cf961f2 100644 --- a/nrf-softdevice/src/ble/gatt_server.rs +++ b/nrf-softdevice/src/ble/gatt_server.rs @@ -6,7 +6,6 @@ use core::mem; use crate::ble::*; -use crate::fmt::{panic, *}; use crate::raw; use crate::util::{get_flexarray, get_union_field, BoundedLifetime, Portal}; use crate::RawError; diff --git a/nrf-softdevice/src/ble/gatt_traits.rs b/nrf-softdevice/src/ble/gatt_traits.rs index db247b6..799d826 100644 --- a/nrf-softdevice/src/ble/gatt_traits.rs +++ b/nrf-softdevice/src/ble/gatt_traits.rs @@ -2,8 +2,6 @@ use core::mem; use core::slice; use heapless::Vec; -use crate::fmt::panic; - pub enum FromGattError { InvalidLength, } diff --git a/nrf-softdevice/src/ble/l2cap.rs b/nrf-softdevice/src/ble/l2cap.rs index dc64b34..2200e5c 100644 --- a/nrf-softdevice/src/ble/l2cap.rs +++ b/nrf-softdevice/src/ble/l2cap.rs @@ -7,7 +7,6 @@ use core::sync::atomic::{AtomicBool, Ordering}; use core::u16; use crate::ble::*; -use crate::fmt::{assert, panic, unreachable, *}; use crate::raw; use crate::util::{get_union_field, Portal}; use crate::{RawError, Softdevice}; diff --git a/nrf-softdevice/src/ble/mod.rs b/nrf-softdevice/src/ble/mod.rs index 14031ae..e8236d3 100644 --- a/nrf-softdevice/src/ble/mod.rs +++ b/nrf-softdevice/src/ble/mod.rs @@ -28,7 +28,6 @@ pub mod l2cap; use core::mem; -use crate::fmt::*; use crate::{raw, RawError, Softdevice}; pub(crate) unsafe fn on_evt(ble_evt: *const raw::ble_evt_t) { diff --git a/nrf-softdevice/src/ble/peripheral.rs b/nrf-softdevice/src/ble/peripheral.rs index 8ea1842..0658d82 100644 --- a/nrf-softdevice/src/ble/peripheral.rs +++ b/nrf-softdevice/src/ble/peripheral.rs @@ -4,7 +4,6 @@ use core::mem; use core::ptr; use crate::ble::*; -use crate::fmt::{assert, unreachable, *}; use crate::raw; use crate::util::get_union_field; use crate::util::{OnDrop, Portal}; diff --git a/nrf-softdevice/src/ble/types.rs b/nrf-softdevice/src/ble/types.rs index 0a7ab9d..ebc9067 100644 --- a/nrf-softdevice/src/ble/types.rs +++ b/nrf-softdevice/src/ble/types.rs @@ -1,6 +1,5 @@ use core::mem; -use crate::fmt::panic; use crate::raw; use crate::RawError; diff --git a/nrf-softdevice/src/events.rs b/nrf-softdevice/src/events.rs index 68a39e9..403350f 100644 --- a/nrf-softdevice/src/events.rs +++ b/nrf-softdevice/src/events.rs @@ -2,7 +2,6 @@ use core::convert::TryFrom; use core::mem::MaybeUninit; use num_enum::{IntoPrimitive, TryFromPrimitive}; -use crate::fmt::{panic, *}; use crate::pac::interrupt; use crate::raw; use crate::util::Signal; diff --git a/nrf-softdevice/src/flash.rs b/nrf-softdevice/src/flash.rs index 7ff40ad..8a4296b 100644 --- a/nrf-softdevice/src/flash.rs +++ b/nrf-softdevice/src/flash.rs @@ -3,7 +3,6 @@ use core::marker::PhantomData; use core::sync::atomic::{AtomicBool, Ordering}; use embassy::traits::flash::Error as FlashError; -use crate::fmt::{panic, *}; use crate::raw; use crate::util::{DropBomb, Signal}; use crate::{RawError, Softdevice}; diff --git a/nrf-softdevice/src/fmt.rs b/nrf-softdevice/src/fmt.rs index 1abb329..0669708 100644 --- a/nrf-softdevice/src/fmt.rs +++ b/nrf-softdevice/src/fmt.rs @@ -1,80 +1,195 @@ #![macro_use] +#![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_export] - 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_export] - macro_rules! debug { - ($($msg:expr),* $(,)?) => { - () - }; - } +macro_rules! todo { + ($($x:tt)*) => { + { + #[cfg(not(feature = "defmt"))] + ::core::todo!($($x)*); + #[cfg(feature = "defmt")] + ::defmt::todo!($($x)*); + } + }; +} - #[macro_export] - macro_rules! info { - ($($msg:expr),* $(,)?) => { - () - }; - } +macro_rules! unreachable { + ($($x:tt)*) => { + { + #[cfg(not(feature = "defmt"))] + ::core::unreachable!($($x)*); + #[cfg(feature = "defmt")] + ::defmt::unreachable!($($x)*); + } + }; +} - #[macro_export] - macro_rules! warn { - ($($msg:expr),* $(,)?) => { - () - }; - } +macro_rules! panic { + ($($x:tt)*) => { + { + #[cfg(not(feature = "defmt"))] + ::core::panic!($($x)*); + #[cfg(feature = "defmt")] + ::defmt::panic!($($x)*); + } + }; +} - #[macro_export] - macro_rules! error { - ($($msg:expr),* $(,)?) => { - () - }; - } +macro_rules! trace { + ($s:literal $(, $x:expr)* $(,)?) => { + { + #[cfg(feature = "log")] + ::log::trace!($s $(, $x)*); + #[cfg(feature = "defmt")] + ::defmt::trace!($s $(, $x)*); + #[cfg(not(any(feature = "log", feature="defmt")))] + let _ = ($( & $x ),*); + } + }; +} + +macro_rules! debug { + ($s:literal $(, $x:expr)* $(,)?) => { + { + #[cfg(feature = "log")] + ::log::debug!($s $(, $x)*); + #[cfg(feature = "defmt")] + ::defmt::debug!($s $(, $x)*); + #[cfg(not(any(feature = "log", feature="defmt")))] + let _ = ($( & $x ),*); + } + }; +} + +macro_rules! info { + ($s:literal $(, $x:expr)* $(,)?) => { + { + #[cfg(feature = "log")] + ::log::info!($s $(, $x)*); + #[cfg(feature = "defmt")] + ::defmt::info!($s $(, $x)*); + #[cfg(not(any(feature = "log", feature="defmt")))] + let _ = ($( & $x ),*); + } + }; +} + +macro_rules! warn { + ($s:literal $(, $x:expr)* $(,)?) => { + { + #[cfg(feature = "log")] + ::log::warn!($s $(, $x)*); + #[cfg(feature = "defmt")] + ::defmt::warn!($s $(, $x)*); + #[cfg(not(any(feature = "log", feature="defmt")))] + let _ = ($( & $x ),*); + } + }; +} + +macro_rules! error { + ($s:literal $(, $x:expr)* $(,)?) => { + { + #[cfg(feature = "log")] + ::log::error!($s $(, $x)*); + #[cfg(feature = "defmt")] + ::defmt::error!($s $(, $x)*); + #[cfg(not(any(feature = "log", feature="defmt")))] + let _ = ($( & $x ),*); + } + }; +} + +#[cfg(feature = "defmt")] +macro_rules! unwrap { + ($($x:tt)*) => { + ::defmt::unwrap!($($x)*) + }; } #[cfg(not(feature = "defmt"))] -#[macro_export] macro_rules! unwrap { - ($arg:expr$(,$msg:expr)*) => { + ($arg:expr) => { + match $crate::fmt::Try::into_result($arg) { + ::core::result::Result::Ok(t) => t, + ::core::result::Result::Err(e) => { + ::core::panic!("unwrap of `{}` failed: {:?}", ::core::stringify!($arg), e); + } + } + }; + ($arg:expr, $($msg:expr),+ $(,)? ) => { match $crate::fmt::Try::into_result($arg) { ::core::result::Result::Ok(t) => t, - ::core::result::Result::Err(_e) => { - ::core::panic!($($msg,)*); + ::core::result::Result::Err(e) => { + ::core::panic!("unwrap of `{}` failed: {}: {:?}", ::core::stringify!($arg), ::core::format_args!($($msg,)*), e); } } } diff --git a/nrf-softdevice/src/softdevice.rs b/nrf-softdevice/src/softdevice.rs index 5f003e5..981ec5f 100644 --- a/nrf-softdevice/src/softdevice.rs +++ b/nrf-softdevice/src/softdevice.rs @@ -3,7 +3,6 @@ use core::ptr; use core::sync::atomic::{AtomicBool, Ordering}; use embassy::util::Forever; -use crate::fmt::{panic, *}; use crate::pac; use crate::raw; use crate::RawError; diff --git a/nrf-softdevice/src/util/drop_bomb.rs b/nrf-softdevice/src/util/drop_bomb.rs index e5c44c2..db3d7d2 100644 --- a/nrf-softdevice/src/util/drop_bomb.rs +++ b/nrf-softdevice/src/util/drop_bomb.rs @@ -1,7 +1,5 @@ use core::mem; -use crate::fmt::panic; - pub struct DropBomb { _private: (), } diff --git a/nrf-softdevice/src/util/portal.rs b/nrf-softdevice/src/util/portal.rs index 1c37d08..28c1d88 100644 --- a/nrf-softdevice/src/util/portal.rs +++ b/nrf-softdevice/src/util/portal.rs @@ -3,7 +3,6 @@ use core::future::Future; use core::mem; use core::mem::MaybeUninit; -use crate::fmt::{assert, panic, unreachable}; use crate::util::{OnDrop, Signal}; /// Utility to call a closure across tasks. diff --git a/nrf-softdevice/src/util/signal.rs b/nrf-softdevice/src/util/signal.rs index 8b36fcb..d03868c 100644 --- a/nrf-softdevice/src/util/signal.rs +++ b/nrf-softdevice/src/util/signal.rs @@ -4,8 +4,6 @@ use core::mem; use core::pin::Pin; use core::task::{Context, Poll, Waker}; -use crate::fmt::{panic, unreachable}; - pub struct Signal<T> { state: UnsafeCell<State<T>>, } |