use core::fmt;
macro_rules! enum_with_unknown {
(
$( #[$enum_attr:meta] )*
$v:vis enum $name:ident($ty:ty) {
$(
$( #[$variant_attr:meta] )*
$variant:ident = $value:expr
),* $(,)?
}
) => {
$( #[$enum_attr] )*
$v enum $name {
$(
$( #[$variant_attr] )*
$variant,
)*
Unknown($ty)
}
impl ::core::convert::From<$ty> for $name {
fn from(value: $ty) -> Self {
match value {
$( $value => $name::$variant, )*
other => $name::Unknown(other)
}
}
}
impl ::core::convert::From<$name> for $ty {
fn from(value: $name) -> Self {
match value {
$( $name::$variant => $value, )*
$name::Unknown(other) => other
}
}
}
}
}
#[derive(Copy, Clone)]
pub struct HexSlice<T>(pub T)
where
T: AsRef<[u8]>;
impl<T: AsRef<[u8]>> fmt::Debug for HexSlice<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("[")?;
for (i, byte) in self.0.as_ref().iter().enumerate() {
if i != 0 {
f.write_str(", ")?;
}
write!(f, "{:02x}", byte)?;
}
f.write_str("]")
}
}
impl<T: AsRef<[u8]>> AsRef<T> for HexSlice<T> {
fn as_ref(&self) -> &T {
&self.0
}
}
#[derive(Copy, Clone)]
pub struct Hex<T>(pub T)
where
T: fmt::LowerHex;
impl<T: fmt::LowerHex> fmt::Debug for Hex<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:#x}", self.0)
}
}