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
46
|
// A `fmt` body may write repeatedly, so a formatter write appends rather than
// assigns. `UpperHex` here writes once per element.
use core::fmt;
struct Bytes<'a>(&'a [u8]);
impl fmt::UpperHex for Bytes<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for &b in self.0 {
write!(f, "{:02X}", b)?;
}
Ok(())
}
}
impl fmt::LowerHex for Bytes<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for &b in self.0 {
write!(f, "{:02x}", b)?;
}
Ok(())
}
}
impl fmt::Display for Bytes<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("<")?;
write!(f, "{self:x}")?;
f.write_str(">")
}
}
fn main() {
let raw: Vec<u8> = vec![0xab, 0xcd, 0x01, 0x00];
println!("{:X}", Bytes(&raw));
println!("{:x}", Bytes(&raw));
println!("{}", Bytes(&raw));
println!("[{}]", Bytes(&[]));
// `debug_assert*` fires in debug builds, which is the profile modelled.
assert!(raw.len() == 4);
assert_eq!(raw.len(), 4);
assert_ne!(raw.len(), 5);
debug_assert_eq!(raw[0], 0xab);
println!("asserts passed");
}
|