#[inline]
pub fn lossy<B: AsRef<[u8]>>(buf: &B) -> LossyFmt<'_> {
LossyFmt(buf.as_ref())
}
pub struct LossyFmt<'a>(&'a [u8]);
impl std::fmt::Display for LossyFmt<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for &b in self.0 {
if b.is_ascii_graphic() || b.is_ascii_whitespace() {
write!(f, "{}", b as char)?;
} else {
write!(f, "\\x{b:x}")?;
}
}
Ok(())
}
}
impl std::fmt::Debug for LossyFmt<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "b\"{self}\"")
}
}