Coverage Report

Created: 2025-12-14 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/bytes-1.11.0/src/fmt/debug.rs
Line
Count
Source
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
29.8k
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
14
29.8k
        write!(f, "b\"")?;
15
28.1M
        for &b in self.0 {
16
            // https://doc.rust-lang.org/reference/tokens.html#byte-escapes
17
28.1M
            if b == b'\n' {
18
54.6k
                write!(f, "\\n")?;
19
28.1M
            } else if b == b'\r' {
20
19.6k
                write!(f, "\\r")?;
21
28.0M
            } else if b == b'\t' {
22
161k
                write!(f, "\\t")?;
23
27.9M
            } else if b == b'\\' || b == b'"' {
24
10.8k
                write!(f, "\\{}", b as char)?;
25
27.9M
            } else if b == b'\0' {
26
12.5M
                write!(f, "\\0")?;
27
            // ASCII printable
28
15.4M
            } else if (0x20..0x7f).contains(&b) {
29
3.95M
                write!(f, "{}", b as char)?;
30
            } else {
31
11.4M
                write!(f, "\\x{:02x}", b)?;
32
            }
33
        }
34
29.8k
        write!(f, "\"")?;
35
29.8k
        Ok(())
36
29.8k
    }
37
}
38
39
fmt_impl!(Debug, Bytes);
40
fmt_impl!(Debug, BytesMut);