Coverage Report

Created: 2025-10-28 06:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/http/src/byte_str.rs
Line
Count
Source
1
use bytes::Bytes;
2
3
use std::{ops, str};
4
5
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
6
pub(crate) struct ByteStr {
7
    // Invariant: bytes contains valid UTF-8
8
    bytes: Bytes,
9
}
10
11
impl ByteStr {
12
    #[inline]
13
7.29k
    pub fn new() -> ByteStr {
14
7.29k
        ByteStr {
15
7.29k
            // Invariant: the empty slice is trivially valid UTF-8.
16
7.29k
            bytes: Bytes::new(),
17
7.29k
        }
18
7.29k
    }
19
20
    #[inline]
21
5.94k
    pub const fn from_static(val: &'static str) -> ByteStr {
22
5.94k
        ByteStr {
23
5.94k
            // Invariant: val is a str so contains valid UTF-8.
24
5.94k
            bytes: Bytes::from_static(val.as_bytes()),
25
5.94k
        }
26
5.94k
    }
27
28
    #[inline]
29
    /// ## Panics
30
    /// In a debug build this will panic if `bytes` is not valid UTF-8.
31
    ///
32
    /// ## Safety
33
    /// `bytes` must contain valid UTF-8. In a release build it is undefined
34
    /// behavior to call this with `bytes` that is not valid UTF-8.
35
1.87k
    pub unsafe fn from_utf8_unchecked(bytes: Bytes) -> ByteStr {
36
1.87k
        if cfg!(debug_assertions) {
37
0
            match str::from_utf8(&bytes) {
38
0
                Ok(_) => (),
39
0
                Err(err) => panic!(
40
0
                    "ByteStr::from_utf8_unchecked() with invalid bytes; error = {}, bytes = {:?}",
41
                    err, bytes
42
                ),
43
            }
44
1.87k
        }
45
        // Invariant: assumed by the safety requirements of this function.
46
1.87k
        ByteStr { bytes }
47
1.87k
    }
48
49
64
    pub(crate) fn from_utf8(bytes: Bytes) -> Result<ByteStr, std::str::Utf8Error> {
50
64
        str::from_utf8(&bytes)?;
51
        // Invariant: just checked is utf8
52
2
        Ok(ByteStr { bytes })
53
64
    }
54
}
55
56
impl ops::Deref for ByteStr {
57
    type Target = str;
58
59
    #[inline]
60
410
    fn deref(&self) -> &str {
61
410
        let b: &[u8] = self.bytes.as_ref();
62
        // Safety: the invariant of `bytes` is that it contains valid UTF-8.
63
410
        unsafe { str::from_utf8_unchecked(b) }
64
410
    }
<http::byte_str::ByteStr as core::ops::deref::Deref>::deref
Line
Count
Source
60
410
    fn deref(&self) -> &str {
61
410
        let b: &[u8] = self.bytes.as_ref();
62
        // Safety: the invariant of `bytes` is that it contains valid UTF-8.
63
410
        unsafe { str::from_utf8_unchecked(b) }
64
410
    }
Unexecuted instantiation: <http::byte_str::ByteStr as core::ops::deref::Deref>::deref
65
}
66
67
impl From<String> for ByteStr {
68
    #[inline]
69
    fn from(src: String) -> ByteStr {
70
        ByteStr {
71
            // Invariant: src is a String so contains valid UTF-8.
72
            bytes: Bytes::from(src),
73
        }
74
    }
75
}
76
77
impl<'a> From<&'a str> for ByteStr {
78
    #[inline]
79
    fn from(src: &'a str) -> ByteStr {
80
        ByteStr {
81
            // Invariant: src is a str so contains valid UTF-8.
82
            bytes: Bytes::copy_from_slice(src.as_bytes()),
83
        }
84
    }
85
}
86
87
impl From<ByteStr> for Bytes {
88
0
    fn from(src: ByteStr) -> Self {
89
0
        src.bytes
90
0
    }
91
}