/rust/registry/src/index.crates.io-1949cf8c6b5b557f/base16ct-0.2.0/src/display.rs
Line | Count | Source |
1 | | use core::fmt; |
2 | | |
3 | | /// `core::fmt` presenter for binary data encoded as hexadecimal (Base16). |
4 | | #[derive(Copy, Clone, Debug, Eq, PartialEq)] |
5 | | pub struct HexDisplay<'a>(pub &'a [u8]); |
6 | | |
7 | | impl fmt::Display for HexDisplay<'_> { |
8 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
9 | 0 | write!(f, "{:X}", self) |
10 | 0 | } |
11 | | } |
12 | | |
13 | | impl fmt::UpperHex for HexDisplay<'_> { |
14 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
15 | 0 | let mut hex = [0u8; 2]; |
16 | | |
17 | 0 | for &byte in self.0 { |
18 | 0 | f.write_str(crate::upper::encode_str(&[byte], &mut hex)?)?; |
19 | | } |
20 | | |
21 | 0 | Ok(()) |
22 | 0 | } |
23 | | } |
24 | | |
25 | | impl fmt::LowerHex for HexDisplay<'_> { |
26 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
27 | 0 | let mut hex = [0u8; 2]; |
28 | | |
29 | 0 | for &byte in self.0 { |
30 | 0 | f.write_str(crate::lower::encode_str(&[byte], &mut hex)?)?; |
31 | | } |
32 | | |
33 | 0 | Ok(()) |
34 | 0 | } |
35 | | } |