1pub struct HexDisplay<'a>(&'a [u8]);
6
7impl<'a> HexDisplay<'a> {
8 pub fn from<R: AsRef<[u8]> + ?Sized>(d: &'a R) -> Self {
9 HexDisplay(d.as_ref())
10 }
11}
12
13impl<'a> std::fmt::Display for HexDisplay<'a> {
14 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
15 for byte in self.0 {
16 write!(f, "{byte:02x}")?;
17 }
18 Ok(())
19 }
20}
21
22impl<'a> std::fmt::Debug for HexDisplay<'a> {
23 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
24 for byte in self.0 {
25 write!(f, "{byte:02x}")?;
26 }
27 Ok(())
28 }
29}
30
31pub fn hex<R: AsRef<[u8]> + ?Sized>(r: &R) -> HexDisplay<'_> {
32 HexDisplay::from(r)
33}