2020-12-01 16:46:56 +00:00
|
|
|
#![macro_use]
|
2021-06-06 22:10:54 +00:00
|
|
|
#![allow(unused_macros)]
|
2020-12-01 16:46:56 +00:00
|
|
|
|
|
|
|
#[cfg(all(feature = "defmt", feature = "log"))]
|
|
|
|
compile_error!("You may not enable both `defmt` and `log` features.");
|
|
|
|
|
2021-06-06 22:10:54 +00:00
|
|
|
macro_rules! assert {
|
|
|
|
($($x:tt)*) => {
|
|
|
|
{
|
|
|
|
#[cfg(not(feature = "defmt"))]
|
2021-06-07 01:15:05 +00:00
|
|
|
::core::assert!($($x)*);
|
2021-06-06 22:10:54 +00:00
|
|
|
#[cfg(feature = "defmt")]
|
2021-06-07 01:15:05 +00:00
|
|
|
::defmt::assert!($($x)*);
|
2021-06-06 22:10:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2020-12-01 16:46:56 +00:00
|
|
|
|
2021-06-06 22:10:54 +00:00
|
|
|
macro_rules! assert_eq {
|
|
|
|
($($x:tt)*) => {
|
|
|
|
{
|
|
|
|
#[cfg(not(feature = "defmt"))]
|
2021-06-07 01:15:05 +00:00
|
|
|
::core::assert_eq!($($x)*);
|
2021-06-06 22:10:54 +00:00
|
|
|
#[cfg(feature = "defmt")]
|
2021-06-07 01:15:05 +00:00
|
|
|
::defmt::assert_eq!($($x)*);
|
2021-06-06 22:10:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! assert_ne {
|
|
|
|
($($x:tt)*) => {
|
|
|
|
{
|
|
|
|
#[cfg(not(feature = "defmt"))]
|
2021-06-07 01:15:05 +00:00
|
|
|
::core::assert_ne!($($x)*);
|
2021-06-06 22:10:54 +00:00
|
|
|
#[cfg(feature = "defmt")]
|
2021-06-07 01:15:05 +00:00
|
|
|
::defmt::assert_ne!($($x)*);
|
2021-06-06 22:10:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! debug_assert {
|
|
|
|
($($x:tt)*) => {
|
|
|
|
{
|
|
|
|
#[cfg(not(feature = "defmt"))]
|
2021-06-07 01:15:05 +00:00
|
|
|
::core::debug_assert!($($x)*);
|
2021-06-06 22:10:54 +00:00
|
|
|
#[cfg(feature = "defmt")]
|
2021-06-07 01:15:05 +00:00
|
|
|
::defmt::debug_assert!($($x)*);
|
2021-06-06 22:10:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! debug_assert_eq {
|
|
|
|
($($x:tt)*) => {
|
|
|
|
{
|
|
|
|
#[cfg(not(feature = "defmt"))]
|
2021-06-07 01:15:05 +00:00
|
|
|
::core::debug_assert_eq!($($x)*);
|
2021-06-06 22:10:54 +00:00
|
|
|
#[cfg(feature = "defmt")]
|
2021-06-07 01:15:05 +00:00
|
|
|
::defmt::debug_assert_eq!($($x)*);
|
2021-06-06 22:10:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! debug_assert_ne {
|
|
|
|
($($x:tt)*) => {
|
|
|
|
{
|
|
|
|
#[cfg(not(feature = "defmt"))]
|
2021-06-07 01:15:05 +00:00
|
|
|
::core::debug_assert_ne!($($x)*);
|
2021-06-06 22:10:54 +00:00
|
|
|
#[cfg(feature = "defmt")]
|
2021-06-07 01:15:05 +00:00
|
|
|
::defmt::debug_assert_ne!($($x)*);
|
2021-06-06 22:10:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! todo {
|
|
|
|
($($x:tt)*) => {
|
|
|
|
{
|
|
|
|
#[cfg(not(feature = "defmt"))]
|
2021-06-07 01:15:05 +00:00
|
|
|
::core::todo!($($x)*);
|
2021-06-06 22:10:54 +00:00
|
|
|
#[cfg(feature = "defmt")]
|
2021-06-07 01:15:05 +00:00
|
|
|
::defmt::todo!($($x)*);
|
2021-06-06 22:10:54 +00:00
|
|
|
}
|
2020-12-01 16:46:56 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:10:54 +00:00
|
|
|
macro_rules! unreachable {
|
|
|
|
($($x:tt)*) => {
|
|
|
|
{
|
|
|
|
#[cfg(not(feature = "defmt"))]
|
2021-06-07 01:15:05 +00:00
|
|
|
::core::unreachable!($($x)*);
|
2021-06-06 22:10:54 +00:00
|
|
|
#[cfg(feature = "defmt")]
|
2021-06-07 01:15:05 +00:00
|
|
|
::defmt::unreachable!($($x)*);
|
2021-06-06 22:10:54 +00:00
|
|
|
}
|
2020-12-01 16:46:56 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:10:54 +00:00
|
|
|
macro_rules! panic {
|
|
|
|
($($x:tt)*) => {
|
|
|
|
{
|
|
|
|
#[cfg(not(feature = "defmt"))]
|
2021-06-07 01:15:05 +00:00
|
|
|
::core::panic!($($x)*);
|
2021-06-06 22:10:54 +00:00
|
|
|
#[cfg(feature = "defmt")]
|
2021-06-07 01:15:05 +00:00
|
|
|
::defmt::panic!($($x)*);
|
2021-06-06 22:10:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! trace {
|
2021-06-07 01:21:09 +00:00
|
|
|
($s:literal $(, $x:expr)* $(,)?) => {
|
2021-06-06 22:10:54 +00:00
|
|
|
{
|
|
|
|
#[cfg(feature = "log")]
|
2021-06-07 01:21:09 +00:00
|
|
|
::log::trace!($s $(, $x)*);
|
2021-06-06 22:10:54 +00:00
|
|
|
#[cfg(feature = "defmt")]
|
2021-06-07 01:21:09 +00:00
|
|
|
::defmt::trace!($s $(, $x)*);
|
|
|
|
#[cfg(not(any(feature = "log", feature="defmt")))]
|
|
|
|
let _ = ($( & $x ),*);
|
2021-06-06 22:10:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! debug {
|
2021-06-07 01:21:09 +00:00
|
|
|
($s:literal $(, $x:expr)* $(,)?) => {
|
2021-06-06 22:10:54 +00:00
|
|
|
{
|
2021-06-07 01:21:09 +00:00
|
|
|
#[cfg(feature = "log")]
|
|
|
|
::log::debug!($s $(, $x)*);
|
2021-06-06 22:10:54 +00:00
|
|
|
#[cfg(feature = "defmt")]
|
2021-06-07 01:21:09 +00:00
|
|
|
::defmt::debug!($s $(, $x)*);
|
|
|
|
#[cfg(not(any(feature = "log", feature="defmt")))]
|
|
|
|
let _ = ($( & $x ),*);
|
2021-06-06 22:10:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! info {
|
2021-06-07 01:21:09 +00:00
|
|
|
($s:literal $(, $x:expr)* $(,)?) => {
|
2021-06-06 22:10:54 +00:00
|
|
|
{
|
|
|
|
#[cfg(feature = "log")]
|
2021-06-07 01:21:09 +00:00
|
|
|
::log::info!($s $(, $x)*);
|
2021-06-06 22:10:54 +00:00
|
|
|
#[cfg(feature = "defmt")]
|
2021-06-07 01:21:09 +00:00
|
|
|
::defmt::info!($s $(, $x)*);
|
|
|
|
#[cfg(not(any(feature = "log", feature="defmt")))]
|
|
|
|
let _ = ($( & $x ),*);
|
2021-06-06 22:10:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2020-12-01 16:46:56 +00:00
|
|
|
|
2021-06-06 22:10:54 +00:00
|
|
|
macro_rules! warn {
|
2021-06-07 01:21:09 +00:00
|
|
|
($s:literal $(, $x:expr)* $(,)?) => {
|
2021-06-06 22:10:54 +00:00
|
|
|
{
|
|
|
|
#[cfg(feature = "log")]
|
2021-06-07 01:21:09 +00:00
|
|
|
::log::warn!($s $(, $x)*);
|
2021-06-06 22:10:54 +00:00
|
|
|
#[cfg(feature = "defmt")]
|
2021-06-07 01:21:09 +00:00
|
|
|
::defmt::warn!($s $(, $x)*);
|
|
|
|
#[cfg(not(any(feature = "log", feature="defmt")))]
|
|
|
|
let _ = ($( & $x ),*);
|
2021-06-06 22:10:54 +00:00
|
|
|
}
|
2020-12-01 16:46:56 +00:00
|
|
|
};
|
2021-06-06 22:10:54 +00:00
|
|
|
}
|
2020-12-01 16:46:56 +00:00
|
|
|
|
2021-06-06 22:10:54 +00:00
|
|
|
macro_rules! error {
|
2021-06-07 01:21:09 +00:00
|
|
|
($s:literal $(, $x:expr)* $(,)?) => {
|
2021-06-06 22:10:54 +00:00
|
|
|
{
|
|
|
|
#[cfg(feature = "log")]
|
2021-06-07 01:21:09 +00:00
|
|
|
::log::error!($s $(, $x)*);
|
2021-06-06 22:10:54 +00:00
|
|
|
#[cfg(feature = "defmt")]
|
2021-06-07 01:21:09 +00:00
|
|
|
::defmt::error!($s $(, $x)*);
|
|
|
|
#[cfg(not(any(feature = "log", feature="defmt")))]
|
|
|
|
let _ = ($( & $x ),*);
|
2021-06-06 22:10:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "defmt")]
|
|
|
|
macro_rules! unwrap {
|
|
|
|
($($x:tt)*) => {
|
2021-06-07 01:15:05 +00:00
|
|
|
::defmt::unwrap!($($x)*)
|
2021-06-06 22:10:54 +00:00
|
|
|
};
|
2020-12-01 16:46:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "defmt"))]
|
|
|
|
macro_rules! unwrap {
|
2020-12-13 20:17:58 +00:00
|
|
|
($arg:expr) => {
|
2020-12-01 16:46:56 +00:00
|
|
|
match $crate::fmt::Try::into_result($arg) {
|
|
|
|
::core::result::Result::Ok(t) => t,
|
|
|
|
::core::result::Result::Err(e) => {
|
2020-12-13 20:17:58 +00:00
|
|
|
::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);
|
2020-12-01 16:46:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
|
|
|
}
|
|
|
|
}
|