Coverage Report

Created: 2024-12-17 06:15

/rust/registry/src/index.crates.io-6f17d22bba15001f/bytes-1.9.0/src/fmt/debug.rs
Line
Count
Source (jump to first uncovered line)
1
use core::fmt::{Debug, Formatter, Result};
2
3
use super::BytesRef;
4
use crate::{Bytes, BytesMut};
5
6
/// Alternative implementation of `std::fmt::Debug` for byte slice.
7
///
8
/// Standard `Debug` implementation for `[u8]` is comma separated
9
/// list of numbers. Since large amount of byte strings are in fact
10
/// ASCII strings or contain a lot of ASCII strings (e. g. HTTP),
11
/// it is convenient to print strings as ASCII when possible.
12
impl Debug for BytesRef<'_> {
13
0
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
14
0
        write!(f, "b\"")?;
15
0
        for &b in self.0 {
16
            // https://doc.rust-lang.org/reference/tokens.html#byte-escapes
17
0
            if b == b'\n' {
18
0
                write!(f, "\\n")?;
19
0
            } else if b == b'\r' {
20
0
                write!(f, "\\r")?;
21
0
            } else if b == b'\t' {
22
0
                write!(f, "\\t")?;
23
0
            } else if b == b'\\' || b == b'"' {
24
0
                write!(f, "\\{}", b as char)?;
25
0
            } else if b == b'\0' {
26
0
                write!(f, "\\0")?;
27
            // ASCII printable
28
0
            } else if (0x20..0x7f).contains(&b) {
29
0
                write!(f, "{}", b as char)?;
30
            } else {
31
0
                write!(f, "\\x{:02x}", b)?;
32
            }
33
        }
34
0
        write!(f, "\"")?;
35
0
        Ok(())
36
0
    }
37
}
38
39
fmt_impl!(Debug, Bytes);
40
fmt_impl!(Debug, BytesMut);