1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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)
}