Coverage Report

Created: 2025-10-10 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/bytes-1.10.1/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.5k
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
14
29.5k
        write!(f, "b\"")?;
15
24.9M
        for &b in self.0 {
16
            // https://doc.rust-lang.org/reference/tokens.html#byte-escapes
17
24.8M
            if b == b'\n' {
18
59.9k
                write!(f, "\\n")?;
19
24.8M
            } else if b == b'\r' {
20
32.9k
                write!(f, "\\r")?;
21
24.7M
            } else if b == b'\t' {
22
71.4k
                write!(f, "\\t")?;
23
24.7M
            } else if b == b'\\' || b == b'"' {
24
7.53k
                write!(f, "\\{}", b as char)?;
25
24.7M
            } else if b == b'\0' {
26
9.86M
                write!(f, "\\0")?;
27
            // ASCII printable
28
14.8M
            } else if (0x20..0x7f).contains(&b) {
29
4.20M
                write!(f, "{}", b as char)?;
30
            } else {
31
10.6M
                write!(f, "\\x{:02x}", b)?;
32
            }
33
        }
34
29.5k
        write!(f, "\"")?;
35
29.5k
        Ok(())
36
29.5k
    }
37
}
38
39
fmt_impl!(Debug, Bytes);
40
fmt_impl!(Debug, BytesMut);