pub struct HexDisplay<'a>(&'a [u8]);
impl<'a> HexDisplay<'a> {
pub fn from<R: std::convert::AsRef<[u8]> + ?Sized>(d: &'a R) -> Self { HexDisplay(d.as_ref()) }
}
impl<'a> std::fmt::Display for HexDisplay<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
for byte in self.0 {
f.write_fmt(format_args!("{:02x}", byte))?;
}
Ok(())
}
}
impl<'a> std::fmt::Debug for HexDisplay<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
for byte in self.0 {
f.write_fmt(format_args!("{:02x}", byte))?;
}
Ok(())
}
}
pub fn hex<'a, R: std::convert::AsRef<[u8]> + ?Sized>(r: &'a R) -> HexDisplay<'a> {
HexDisplay::from(r)
}