2021-02-03 05:09:37 +01:00
|
|
|
#![macro_use]
|
2021-06-07 00:10:54 +02:00
|
|
|
#![allow(unused_macros)]
|
2021-02-03 05:09:37 +01:00
|
|
|
|
2023-08-30 01:37:18 +02:00
|
|
|
use core::fmt::{Debug, Display, LowerHex};
|
|
|
|
|
2021-02-03 05:09:37 +01:00
|
|
|
#[cfg(all(feature = "defmt", feature = "log"))]
|
|
|
|
compile_error!("You may not enable both `defmt` and `log` features.");
|
|
|
|
|
2021-06-07 00:10:54 +02:00
|
|
|
macro_rules! assert {
|
|
|
|
($($x:tt)*) => {
|
|
|
|
{
|
|
|
|
#[cfg(not(feature = "defmt"))]
|
2021-06-07 03:15:05 +02:00
|
|
|
::core::assert!($($x)*);
|
2021-06-07 00:10:54 +02:00
|
|
|
#[cfg(feature = "defmt")]
|
2021-06-07 03:15:05 +02:00
|
|
|
::defmt::assert!($($x)*);
|
2021-06-07 00:10:54 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2021-02-03 05:09:37 +01:00
|
|
|
|
2021-06-07 00:10:54 +02:00
|
|
|
macro_rules! assert_eq {
|
|
|
|
($($x:tt)*) => {
|
|
|
|
{
|
|
|
|
#[cfg(not(feature = "defmt"))]
|
2021-06-07 03:15:05 +02:00
|
|
|
::core::assert_eq!($($x)*);
|
2021-06-07 00:10:54 +02:00
|
|
|
#[cfg(feature = "defmt")]
|
2021-06-07 03:15:05 +02:00
|
|
|
::defmt::assert_eq!($($x)*);
|
2021-06-07 00:10:54 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! assert_ne {
|
|
|
|
($($x:tt)*) => {
|
|
|
|
{
|
|
|
|
#[cfg(not(feature = "defmt"))]
|
2021-06-07 03:15:05 +02:00
|
|
|
::core::assert_ne!($($x)*);
|
2021-06-07 00:10:54 +02:00
|
|
|
#[cfg(feature = "defmt")]
|
2021-06-07 03:15:05 +02:00
|
|
|
::defmt::assert_ne!($($x)*);
|
2021-06-07 00:10:54 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! debug_assert {
|
|
|
|
($($x:tt)*) => {
|
|
|
|
{
|
|
|
|
#[cfg(not(feature = "defmt"))]
|
2021-06-07 03:15:05 +02:00
|
|
|
::core::debug_assert!($($x)*);
|
2021-06-07 00:10:54 +02:00
|
|
|
#[cfg(feature = "defmt")]
|
2021-06-07 03:15:05 +02:00
|
|
|
::defmt::debug_assert!($($x)*);
|
2021-06-07 00:10:54 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! debug_assert_eq {
|
|
|
|
($($x:tt)*) => {
|
|
|
|
{
|
|
|
|
#[cfg(not(feature = "defmt"))]
|
2021-06-07 03:15:05 +02:00
|
|
|
::core::debug_assert_eq!($($x)*);
|
2021-06-07 00:10:54 +02:00
|
|
|
#[cfg(feature = "defmt")]
|
2021-06-07 03:15:05 +02:00
|
|
|
::defmt::debug_assert_eq!($($x)*);
|
2021-06-07 00:10:54 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! debug_assert_ne {
|
|
|
|
($($x:tt)*) => {
|
|
|
|
{
|
|
|
|
#[cfg(not(feature = "defmt"))]
|
2021-06-07 03:15:05 +02:00
|
|
|
::core::debug_assert_ne!($($x)*);
|
2021-06-07 00:10:54 +02:00
|
|
|
#[cfg(feature = "defmt")]
|
2021-06-07 03:15:05 +02:00
|
|
|
::defmt::debug_assert_ne!($($x)*);
|
2021-06-07 00:10:54 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! todo {
|
|
|
|
($($x:tt)*) => {
|
|
|
|
{
|
|
|
|
#[cfg(not(feature = "defmt"))]
|
2021-06-07 03:15:05 +02:00
|
|
|
::core::todo!($($x)*);
|
2021-06-07 00:10:54 +02:00
|
|
|
#[cfg(feature = "defmt")]
|
2021-06-07 03:15:05 +02:00
|
|
|
::defmt::todo!($($x)*);
|
2021-06-07 00:10:54 +02:00
|
|
|
}
|
2021-02-03 05:09:37 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-08-30 01:37:18 +02:00
|
|
|
#[cfg(not(feature = "defmt"))]
|
2021-06-07 00:10:54 +02:00
|
|
|
macro_rules! unreachable {
|
|
|
|
($($x:tt)*) => {
|
2023-08-30 01:37:18 +02:00
|
|
|
::core::unreachable!($($x)*)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "defmt")]
|
|
|
|
macro_rules! unreachable {
|
|
|
|
($($x:tt)*) => {
|
|
|
|
::defmt::unreachable!($($x)*)
|
2021-02-03 05:09:37 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-07 00:10:54 +02:00
|
|
|
macro_rules! panic {
|
|
|
|
($($x:tt)*) => {
|
|
|
|
{
|
|
|
|
#[cfg(not(feature = "defmt"))]
|
2021-06-07 03:15:05 +02:00
|
|
|
::core::panic!($($x)*);
|
2021-06-07 00:10:54 +02:00
|
|
|
#[cfg(feature = "defmt")]
|
2021-06-07 03:15:05 +02:00
|
|
|
::defmt::panic!($($x)*);
|
2021-06-07 00:10:54 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! trace {
|
2021-06-07 03:21:09 +02:00
|
|
|
($s:literal $(, $x:expr)* $(,)?) => {
|
2021-06-07 00:10:54 +02:00
|
|
|
{
|
|
|
|
#[cfg(feature = "log")]
|
2021-06-07 03:21:09 +02:00
|
|
|
::log::trace!($s $(, $x)*);
|
2021-06-07 00:10:54 +02:00
|
|
|
#[cfg(feature = "defmt")]
|
2021-06-07 03:21:09 +02:00
|
|
|
::defmt::trace!($s $(, $x)*);
|
|
|
|
#[cfg(not(any(feature = "log", feature="defmt")))]
|
|
|
|
let _ = ($( & $x ),*);
|
2021-06-07 00:10:54 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! debug {
|
2021-06-07 03:21:09 +02:00
|
|
|
($s:literal $(, $x:expr)* $(,)?) => {
|
2021-06-07 00:10:54 +02:00
|
|
|
{
|
2021-06-07 03:21:09 +02:00
|
|
|
#[cfg(feature = "log")]
|
|
|
|
::log::debug!($s $(, $x)*);
|
2021-06-07 00:10:54 +02:00
|
|
|
#[cfg(feature = "defmt")]
|
2021-06-07 03:21:09 +02:00
|
|
|
::defmt::debug!($s $(, $x)*);
|
|
|
|
#[cfg(not(any(feature = "log", feature="defmt")))]
|
|
|
|
let _ = ($( & $x ),*);
|
2021-06-07 00:10:54 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! info {
|
2021-06-07 03:21:09 +02:00
|
|
|
($s:literal $(, $x:expr)* $(,)?) => {
|
2021-06-07 00:10:54 +02:00
|
|
|
{
|
|
|
|
#[cfg(feature = "log")]
|
2021-06-07 03:21:09 +02:00
|
|
|
::log::info!($s $(, $x)*);
|
2021-06-07 00:10:54 +02:00
|
|
|
#[cfg(feature = "defmt")]
|
2021-06-07 03:21:09 +02:00
|
|
|
::defmt::info!($s $(, $x)*);
|
|
|
|
#[cfg(not(any(feature = "log", feature="defmt")))]
|
|
|
|
let _ = ($( & $x ),*);
|
2021-06-07 00:10:54 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2021-02-03 05:09:37 +01:00
|
|
|
|
2021-06-07 00:10:54 +02:00
|
|
|
macro_rules! warn {
|
2021-06-07 03:21:09 +02:00
|
|
|
($s:literal $(, $x:expr)* $(,)?) => {
|
2021-06-07 00:10:54 +02:00
|
|
|
{
|
|
|
|
#[cfg(feature = "log")]
|
2021-06-07 03:21:09 +02:00
|
|
|
::log::warn!($s $(, $x)*);
|
2021-06-07 00:10:54 +02:00
|
|
|
#[cfg(feature = "defmt")]
|
2021-06-07 03:21:09 +02:00
|
|
|
::defmt::warn!($s $(, $x)*);
|
|
|
|
#[cfg(not(any(feature = "log", feature="defmt")))]
|
|
|
|
let _ = ($( & $x ),*);
|
2021-06-07 00:10:54 +02:00
|
|
|
}
|
2021-02-03 05:09:37 +01:00
|
|
|
};
|
2021-06-07 00:10:54 +02:00
|
|
|
}
|
2021-02-03 05:09:37 +01:00
|
|
|
|
2021-06-07 00:10:54 +02:00
|
|
|
macro_rules! error {
|
2021-06-07 03:21:09 +02:00
|
|
|
($s:literal $(, $x:expr)* $(,)?) => {
|
2021-06-07 00:10:54 +02:00
|
|
|
{
|
|
|
|
#[cfg(feature = "log")]
|
2021-06-07 03:21:09 +02:00
|
|
|
::log::error!($s $(, $x)*);
|
2021-06-07 00:10:54 +02:00
|
|
|
#[cfg(feature = "defmt")]
|
2021-06-07 03:21:09 +02:00
|
|
|
::defmt::error!($s $(, $x)*);
|
|
|
|
#[cfg(not(any(feature = "log", feature="defmt")))]
|
|
|
|
let _ = ($( & $x ),*);
|
2021-06-07 00:10:54 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "defmt")]
|
|
|
|
macro_rules! unwrap {
|
|
|
|
($($x:tt)*) => {
|
2021-06-07 03:15:05 +02:00
|
|
|
::defmt::unwrap!($($x)*)
|
2021-06-07 00:10:54 +02:00
|
|
|
};
|
2021-02-03 05:09:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "defmt"))]
|
|
|
|
macro_rules! unwrap {
|
|
|
|
($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!("unwrap of `{}` failed: {}: {:?}", ::core::stringify!($arg), ::core::format_args!($($msg,)*), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
|
|
|
pub struct NoneError;
|
|
|
|
|
|
|
|
pub trait Try {
|
|
|
|
type Ok;
|
|
|
|
type Error;
|
|
|
|
fn into_result(self) -> Result<Self::Ok, Self::Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Try for Option<T> {
|
|
|
|
type Ok = T;
|
|
|
|
type Error = NoneError;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn into_result(self) -> Result<T, NoneError> {
|
|
|
|
self.ok_or(NoneError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, E> Try for Result<T, E> {
|
|
|
|
type Ok = T;
|
|
|
|
type Error = E;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn into_result(self) -> Self {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
2023-08-30 01:37:18 +02:00
|
|
|
|
|
|
|
#[allow(unused)]
|
|
|
|
pub(crate) struct Bytes<'a>(pub &'a [u8]);
|
|
|
|
|
|
|
|
impl<'a> Debug for Bytes<'a> {
|
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
|
|
|
write!(f, "{:#02x?}", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Display for Bytes<'a> {
|
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
|
|
|
write!(f, "{:#02x?}", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> LowerHex for Bytes<'a> {
|
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
|
|
|
write!(f, "{:#02x?}", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "defmt")]
|
|
|
|
impl<'a> defmt::Format for Bytes<'a> {
|
|
|
|
fn format(&self, fmt: defmt::Formatter) {
|
|
|
|
defmt::write!(fmt, "{:02x}", self.0)
|
|
|
|
}
|
|
|
|
}
|