holey-bytes/hbvm/src/utils.rs

54 lines
1.6 KiB
Rust
Raw Normal View History

2023-08-19 16:46:47 -05:00
macro_rules! impl_display {
(for $ty:ty => $(|$selfty:pat_param|)? $fmt:literal $(, $($param:expr),+)? $(,)?) => {
impl ::core::fmt::Display for $ty {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
$(let $selfty = self;)?
write!(f, $fmt, $($param),*)
}
}
};
(for $ty:ty => $str:literal) => {
impl ::core::fmt::Display for $ty {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.write_str($str)
}
}
};
(for $ty:ty => match {$(
2023-08-19 16:57:48 -05:00
$bind:pat => $($const:ident)? $fmt:literal $(,$($params:tt)*)?;
2023-08-19 16:46:47 -05:00
)*}) => {
impl ::core::fmt::Display for $ty {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
match self {
$(
2023-08-19 17:24:27 -05:00
$bind => $crate::utils::internal::impl_display_match_fragment!($($const,)? f, $fmt $(, $($params)*)?)
2023-08-19 16:46:47 -05:00
),*
}
}
}
}
}
2023-08-19 16:57:48 -05:00
#[doc(hidden)]
2023-08-19 17:24:27 -05:00
pub(crate) mod internal {
2023-08-19 16:57:48 -05:00
macro_rules! impl_display_match_fragment {
(const, $f:expr, $lit:literal) => {
$f.write_str($lit)
};
($f:expr, $fmt:literal $(, $($params:tt)*)?) => {
write!($f, $fmt, $($($params)*)?)
};
}
pub(crate) use impl_display_match_fragment;
}
macro_rules! static_assert(($expr:expr $(,)?) => {
const _: [(); !$expr as usize] = [];
2023-08-19 16:46:47 -05:00
});
pub(crate) use {impl_display, static_assert};