use std::fmt;
pub struct BinaryDisplay<'a>(pub &'a [u8]);
impl<'a> fmt::Display for BinaryDisplay<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "b'")?;
for &byte in self.0 {
match byte {
0x20..=0x7e => write!(f, "{}", byte as char)?,
_ => write!(f, "\\x{:02x}", byte)?,
}
}
write!(f, "'")
}
}
impl<'a> fmt::Debug for BinaryDisplay<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
pub fn display_bytes<T: AsRef<[u8]> + ?Sized>(bytes: &T) -> BinaryDisplay<'_> {
BinaryDisplay(bytes.as_ref())
}