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
use core::fmt::{Debug, Formatter, Result};
pub struct SliceHexFormatter<'a> {
slice: &'a [u8],
}
impl<'a> SliceHexFormatter<'a> {
pub fn new(slice: &'a [u8]) -> Self {
Self { slice }
}
}
impl<'a> Debug for SliceHexFormatter<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "0x")?;
for elem in self.slice {
write!(f, "{:02X}", elem)?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn slice_hex_formatter() {
assert_eq!(
format!(
"{:?}",
SliceHexFormatter::new(&[0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x01])
),
"0xDEADBEEF0001".to_string()
);
}
}