use std::fmt;
pub struct BinaryDisplay<'a>(pub &'a [u8]);
impl fmt::Display for BinaryDisplay<'_> {
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{byte:02x}")?,
}
}
write!(f, "'")
}
}
impl fmt::Debug for BinaryDisplay<'_> {
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())
}