#[macro_export]
macro_rules! create_enum {
(
$(#[$container_mac:meta])*
$v:vis enum $enum_ident:ident<$n:ty> {
$(
$(#[$variant_mac_fixed:meta])*
$variant_ident_fixed:ident = ($variant_n_fixed:literal $(, $variant_str_fixed:literal)? $(| $variant_str_fixed_n:literal)*)
),* $(,)?
}
) => {
$(#[$container_mac])*
$v enum $enum_ident {
$($(#[$variant_mac_fixed])* $variant_ident_fixed,)*
}
#[allow(dead_code, reason = "outside may or may not use methods")]
impl $enum_ident {
#[inline]
$v fn all() -> [Self; { Self::len() }] {
[$( $enum_ident::$variant_ident_fixed, )*]
}
#[inline]
$v const fn len() -> usize {
const { 0 $( + { let _: $n = $variant_n_fixed; 1 })* }
}
#[inline]
$v const fn strings(&self) -> $crate::misc::EnumVarStrings<{
#[allow(unused_mut, reason = "macro stuff")]
let mut n;
$({
#[allow(unused_mut, reason = "repetition can be empty")]
let mut local_n = 0;
let _: $n = $variant_n_fixed;
$({ let _ = $variant_str_fixed; local_n += 1; })?
$({ let _ = $variant_str_fixed_n; local_n += 1; })*
#[allow(unused_assignments, reason = "repetition can be empty")]
{ n = local_n; }
})*
n
}> {
match self {
$(
$enum_ident::$variant_ident_fixed => $crate::misc::EnumVarStrings {
custom: [$($variant_str_fixed,)? $($variant_str_fixed_n,)*],
ident: stringify!($variant_ident_fixed),
number: stringify!($variant_n_fixed),
},
)*
}
}
}
#[allow(
unused_qualifications,
reason = "macro shouldn't control what the outside uses"
)]
impl core::fmt::Display for $enum_ident {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.strings().ident)
}
}
impl From<$enum_ident> for $n {
#[inline]
fn from(from: $enum_ident) -> Self {
match from {
$($enum_ident::$variant_ident_fixed => $variant_n_fixed,)*
}
}
}
impl core::str::FromStr for $enum_ident {
type Err = $crate::Error;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
s.try_into()
}
}
impl TryFrom<$n> for $enum_ident {
type Error = $crate::Error;
#[inline]
fn try_from(from: $n) -> $crate::Result<Self> {
let rslt = match from {
$($variant_n_fixed => Self::$variant_ident_fixed,)*
_ => return Err($crate::Error::UnexpectedUint { received: from.into() }),
};
Ok(rslt)
}
}
impl TryFrom<&str> for $enum_ident {
type Error = $crate::Error;
#[inline]
fn try_from(from: &str) -> $crate::Result<Self> {
from.as_bytes().try_into()
}
}
impl TryFrom<&[u8]> for $enum_ident {
type Error = $crate::Error;
#[inline]
fn try_from(from: &[u8]) -> $crate::Result<Self> {
match from {
$(
from if from == const { stringify!($variant_ident_fixed).as_bytes() }
|| from == const { stringify!($variant_n_fixed).as_bytes() }
$(|| from == const { $variant_str_fixed.as_bytes() })?
$(|| from == const { $variant_str_fixed_n.as_bytes() })* =>
{
Ok(Self::$variant_ident_fixed)
},
)*
_ => Err($crate::Error::UnexpectedBytes {
length: from.len().try_into().unwrap_or(u16::MAX),
ty: core::any::type_name::<Self>().into(),
}),
}
}
}
}
}
macro_rules! _debug {
($($tt:tt)+) => {
#[cfg(feature = "tracing")]
tracing::debug!($($tt)+);
};
}
macro_rules! doc_epoch {
() => {
"`secs` is the amount of seconds passed since the UNIX epoch. This parameter is only relevant
for few `no_std` devices that can't natively provide time measurements, as such, regular users
should simply pass `zero`."
};
}
macro_rules! doc_many_elems_cap_overflow {
() => {
"There is no capacity left to insert a set of new elements."
};
}
macro_rules! doc_reserve_overflow {
() => {
"It was not possible to reserve more memory"
};
}
macro_rules! doc_single_elem_cap_overflow {
() => {
"There is no capacity left to insert a new element."
};
}
macro_rules! _internal_buffer_doc {
() => {
"Buffer used for internal operations."
};
}
macro_rules! _internal_doc {
() => {
"Internal element not meant for public usage."
};
}
macro_rules! _max_continuation_frames {
() => {
16
};
}
macro_rules! _max_frames_mismatches {
() => {
32
};
}
macro_rules! _simd {
(
4 => $_4:expr,
16 => $_16:expr,
32 => $_32:expr,
64 => $_64:expr $(,)?
) => {{
cfg_select! {
target_feature = "avx512f" => $_64,
target_feature = "avx2" => $_32,
any(target_feature = "neon", target_feature = "sse2") => $_16,
_ => $_4
}
}};
}
macro_rules! _trace {
($($tt:tt)+) => {
#[cfg(feature = "tracing")]
tracing::trace!($($tt)+)
};
}
macro_rules! _trace_span {
($($tt:tt)+) => {
crate::misc::span::Span::new(
#[cfg(feature = "tracing")]
tracing::trace_span!($($tt)+),
#[cfg(not(feature = "tracing"))]
()
)
};
}