1 | /// A type that wraps a single byte with a convenient fmt::Debug impl that |
2 | /// escapes the byte. |
3 | pub(crate) struct DebugByte(pub(crate) u8); |
4 | |
5 | impl core::fmt::Debug for DebugByte { |
6 | fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
7 | // Special case ASCII space. It's too hard to read otherwise, so |
8 | // put quotes around it. I sometimes wonder whether just '\x20' would |
9 | // be better... |
10 | if self.0 == b' ' { |
11 | return write!(f, "' '" ); |
12 | } |
13 | // 10 bytes is enough to cover any output from ascii::escape_default. |
14 | let mut bytes: [u8; 10] = [0u8; 10]; |
15 | let mut len: usize = 0; |
16 | for (i: usize, mut b: u8) in core::ascii::escape_default(self.0).enumerate() { |
17 | // capitalize \xab to \xAB |
18 | if i >= 2 && b'a' <= b && b <= b'f' { |
19 | b -= 32; |
20 | } |
21 | bytes[len] = b; |
22 | len += 1; |
23 | } |
24 | write!(f, " {}" , core::str::from_utf8(&bytes[..len]).unwrap()) |
25 | } |
26 | } |
27 | |