Coverage Report

Created: 2024-12-17 06:15

/rust/registry/src/index.crates.io-6f17d22bba15001f/hyper-0.14.32/src/common/buf.rs
Line
Count
Source (jump to first uncovered line)
1
use std::collections::VecDeque;
2
use std::io::IoSlice;
3
4
use bytes::{Buf, BufMut, Bytes, BytesMut};
5
6
pub(crate) struct BufList<T> {
7
    bufs: VecDeque<T>,
8
}
9
10
impl<T: Buf> BufList<T> {
11
541k
    pub(crate) fn new() -> BufList<T> {
12
541k
        BufList {
13
541k
            bufs: VecDeque::new(),
14
541k
        }
15
541k
    }
Unexecuted instantiation: <hyper::common::buf::BufList<_>>::new
<hyper::common::buf::BufList<hyper::proto::h1::encode::EncodedBuf<linkerd_http_box::body::Data>>>::new
Line
Count
Source
11
270k
    pub(crate) fn new() -> BufList<T> {
12
270k
        BufList {
13
270k
            bufs: VecDeque::new(),
14
270k
        }
15
270k
    }
<hyper::common::buf::BufList<hyper::proto::h1::encode::EncodedBuf<bytes::bytes::Bytes>>>::new
Line
Count
Source
11
270k
    pub(crate) fn new() -> BufList<T> {
12
270k
        BufList {
13
270k
            bufs: VecDeque::new(),
14
270k
        }
15
270k
    }
Unexecuted instantiation: <hyper::common::buf::BufList<_>>::new
Unexecuted instantiation: <hyper::common::buf::BufList<_>>::new
16
17
    #[inline]
18
1.07M
    pub(crate) fn push(&mut self, buf: T) {
19
1.07M
        debug_assert!(buf.has_remaining());
20
1.07M
        self.bufs.push_back(buf);
21
1.07M
    }
Unexecuted instantiation: <hyper::common::buf::BufList<_>>::push
<hyper::common::buf::BufList<hyper::proto::h1::encode::EncodedBuf<linkerd_http_box::body::Data>>>::push
Line
Count
Source
18
538k
    pub(crate) fn push(&mut self, buf: T) {
19
538k
        debug_assert!(buf.has_remaining());
20
538k
        self.bufs.push_back(buf);
21
538k
    }
<hyper::common::buf::BufList<hyper::proto::h1::encode::EncodedBuf<bytes::bytes::Bytes>>>::push
Line
Count
Source
18
538k
    pub(crate) fn push(&mut self, buf: T) {
19
538k
        debug_assert!(buf.has_remaining());
20
538k
        self.bufs.push_back(buf);
21
538k
    }
Unexecuted instantiation: <hyper::common::buf::BufList<_>>::push
Unexecuted instantiation: <hyper::common::buf::BufList<_>>::push
22
23
    #[inline]
24
    #[cfg(feature = "http1")]
25
2.75M
    pub(crate) fn bufs_cnt(&self) -> usize {
26
2.75M
        self.bufs.len()
27
2.75M
    }
Unexecuted instantiation: <hyper::common::buf::BufList<_>>::bufs_cnt
<hyper::common::buf::BufList<hyper::proto::h1::encode::EncodedBuf<linkerd_http_box::body::Data>>>::bufs_cnt
Line
Count
Source
25
1.78M
    pub(crate) fn bufs_cnt(&self) -> usize {
26
1.78M
        self.bufs.len()
27
1.78M
    }
<hyper::common::buf::BufList<hyper::proto::h1::encode::EncodedBuf<bytes::bytes::Bytes>>>::bufs_cnt
Line
Count
Source
25
964k
    pub(crate) fn bufs_cnt(&self) -> usize {
26
964k
        self.bufs.len()
27
964k
    }
Unexecuted instantiation: <hyper::common::buf::BufList<_>>::bufs_cnt
Unexecuted instantiation: <hyper::common::buf::BufList<_>>::bufs_cnt
28
}
29
30
impl<T: Buf> Buf for BufList<T> {
31
    #[inline]
32
20.2M
    fn remaining(&self) -> usize {
33
20.2M
        self.bufs.iter().map(|buf| buf.remaining()).sum()
Unexecuted instantiation: <hyper::common::buf::BufList<_> as bytes::buf::buf_impl::Buf>::remaining::{closure#0}
<hyper::common::buf::BufList<hyper::proto::h1::encode::EncodedBuf<linkerd_http_box::body::Data>> as bytes::buf::buf_impl::Buf>::remaining::{closure#0}
Line
Count
Source
33
1.07M
        self.bufs.iter().map(|buf| buf.remaining()).sum()
<hyper::common::buf::BufList<hyper::proto::h1::encode::EncodedBuf<bytes::bytes::Bytes>> as bytes::buf::buf_impl::Buf>::remaining::{closure#0}
Line
Count
Source
33
538k
        self.bufs.iter().map(|buf| buf.remaining()).sum()
Unexecuted instantiation: <hyper::common::buf::BufList<_> as bytes::buf::buf_impl::Buf>::remaining::{closure#0}
Unexecuted instantiation: <hyper::common::buf::BufList<_> as bytes::buf::buf_impl::Buf>::remaining::{closure#0}
34
20.2M
    }
Unexecuted instantiation: <hyper::common::buf::BufList<_> as bytes::buf::buf_impl::Buf>::remaining
<hyper::common::buf::BufList<hyper::proto::h1::encode::EncodedBuf<linkerd_http_box::body::Data>> as bytes::buf::buf_impl::Buf>::remaining
Line
Count
Source
32
11.9M
    fn remaining(&self) -> usize {
33
11.9M
        self.bufs.iter().map(|buf| buf.remaining()).sum()
34
11.9M
    }
<hyper::common::buf::BufList<hyper::proto::h1::encode::EncodedBuf<bytes::bytes::Bytes>> as bytes::buf::buf_impl::Buf>::remaining
Line
Count
Source
32
8.22M
    fn remaining(&self) -> usize {
33
8.22M
        self.bufs.iter().map(|buf| buf.remaining()).sum()
34
8.22M
    }
Unexecuted instantiation: <hyper::common::buf::BufList<_> as bytes::buf::buf_impl::Buf>::remaining
Unexecuted instantiation: <hyper::common::buf::BufList<_> as bytes::buf::buf_impl::Buf>::remaining
35
36
    #[inline]
37
0
    fn chunk(&self) -> &[u8] {
38
0
        self.bufs.front().map(Buf::chunk).unwrap_or_default()
39
0
    }
Unexecuted instantiation: <hyper::common::buf::BufList<_> as bytes::buf::buf_impl::Buf>::chunk
Unexecuted instantiation: <hyper::common::buf::BufList<_> as bytes::buf::buf_impl::Buf>::chunk
Unexecuted instantiation: <hyper::common::buf::BufList<_> as bytes::buf::buf_impl::Buf>::chunk
40
41
    #[inline]
42
1.07M
    fn advance(&mut self, mut cnt: usize) {
43
2.15M
        while cnt > 0 {
44
            {
45
1.07M
                let front = &mut self.bufs[0];
46
1.07M
                let rem = front.remaining();
47
1.07M
                if rem > cnt {
48
0
                    front.advance(cnt);
49
0
                    return;
50
1.07M
                } else {
51
1.07M
                    front.advance(rem);
52
1.07M
                    cnt -= rem;
53
1.07M
                }
54
1.07M
            }
55
1.07M
            self.bufs.pop_front();
56
        }
57
1.07M
    }
Unexecuted instantiation: <hyper::common::buf::BufList<_> as bytes::buf::buf_impl::Buf>::advance
<hyper::common::buf::BufList<hyper::proto::h1::encode::EncodedBuf<linkerd_http_box::body::Data>> as bytes::buf::buf_impl::Buf>::advance
Line
Count
Source
42
538k
    fn advance(&mut self, mut cnt: usize) {
43
1.07M
        while cnt > 0 {
44
            {
45
538k
                let front = &mut self.bufs[0];
46
538k
                let rem = front.remaining();
47
538k
                if rem > cnt {
48
0
                    front.advance(cnt);
49
0
                    return;
50
538k
                } else {
51
538k
                    front.advance(rem);
52
538k
                    cnt -= rem;
53
538k
                }
54
538k
            }
55
538k
            self.bufs.pop_front();
56
        }
57
538k
    }
<hyper::common::buf::BufList<hyper::proto::h1::encode::EncodedBuf<bytes::bytes::Bytes>> as bytes::buf::buf_impl::Buf>::advance
Line
Count
Source
42
538k
    fn advance(&mut self, mut cnt: usize) {
43
1.07M
        while cnt > 0 {
44
            {
45
538k
                let front = &mut self.bufs[0];
46
538k
                let rem = front.remaining();
47
538k
                if rem > cnt {
48
0
                    front.advance(cnt);
49
0
                    return;
50
538k
                } else {
51
538k
                    front.advance(rem);
52
538k
                    cnt -= rem;
53
538k
                }
54
538k
            }
55
538k
            self.bufs.pop_front();
56
        }
57
538k
    }
Unexecuted instantiation: <hyper::common::buf::BufList<_> as bytes::buf::buf_impl::Buf>::advance
Unexecuted instantiation: <hyper::common::buf::BufList<_> as bytes::buf::buf_impl::Buf>::advance
58
59
    #[inline]
60
2.48M
    fn chunks_vectored<'t>(&'t self, dst: &mut [IoSlice<'t>]) -> usize {
61
2.48M
        if dst.is_empty() {
62
0
            return 0;
63
2.48M
        }
64
2.48M
        let mut vecs = 0;
65
3.55M
        for buf in &self.bufs {
66
1.07M
            vecs += buf.chunks_vectored(&mut dst[vecs..]);
67
1.07M
            if vecs == dst.len() {
68
0
                break;
69
1.07M
            }
70
        }
71
2.48M
        vecs
72
2.48M
    }
Unexecuted instantiation: <hyper::common::buf::BufList<_> as bytes::buf::buf_impl::Buf>::chunks_vectored
<hyper::common::buf::BufList<hyper::proto::h1::encode::EncodedBuf<linkerd_http_box::body::Data>> as bytes::buf::buf_impl::Buf>::chunks_vectored
Line
Count
Source
60
1.23M
    fn chunks_vectored<'t>(&'t self, dst: &mut [IoSlice<'t>]) -> usize {
61
1.23M
        if dst.is_empty() {
62
0
            return 0;
63
1.23M
        }
64
1.23M
        let mut vecs = 0;
65
1.77M
        for buf in &self.bufs {
66
538k
            vecs += buf.chunks_vectored(&mut dst[vecs..]);
67
538k
            if vecs == dst.len() {
68
0
                break;
69
538k
            }
70
        }
71
1.23M
        vecs
72
1.23M
    }
<hyper::common::buf::BufList<hyper::proto::h1::encode::EncodedBuf<bytes::bytes::Bytes>> as bytes::buf::buf_impl::Buf>::chunks_vectored
Line
Count
Source
60
1.24M
    fn chunks_vectored<'t>(&'t self, dst: &mut [IoSlice<'t>]) -> usize {
61
1.24M
        if dst.is_empty() {
62
0
            return 0;
63
1.24M
        }
64
1.24M
        let mut vecs = 0;
65
1.78M
        for buf in &self.bufs {
66
538k
            vecs += buf.chunks_vectored(&mut dst[vecs..]);
67
538k
            if vecs == dst.len() {
68
0
                break;
69
538k
            }
70
        }
71
1.24M
        vecs
72
1.24M
    }
Unexecuted instantiation: <hyper::common::buf::BufList<_> as bytes::buf::buf_impl::Buf>::chunks_vectored
Unexecuted instantiation: <hyper::common::buf::BufList<_> as bytes::buf::buf_impl::Buf>::chunks_vectored
73
74
    #[inline]
75
0
    fn copy_to_bytes(&mut self, len: usize) -> Bytes {
76
0
        // Our inner buffer may have an optimized version of copy_to_bytes, and if the whole
77
0
        // request can be fulfilled by the front buffer, we can take advantage.
78
0
        match self.bufs.front_mut() {
79
0
            Some(front) if front.remaining() == len => {
80
0
                let b = front.copy_to_bytes(len);
81
0
                self.bufs.pop_front();
82
0
                b
83
            }
84
0
            Some(front) if front.remaining() > len => front.copy_to_bytes(len),
85
            _ => {
86
0
                assert!(len <= self.remaining(), "`len` greater than remaining");
87
0
                let mut bm = BytesMut::with_capacity(len);
88
0
                bm.put(self.take(len));
89
0
                bm.freeze()
90
            }
91
        }
92
0
    }
Unexecuted instantiation: <hyper::common::buf::BufList<_> as bytes::buf::buf_impl::Buf>::copy_to_bytes
Unexecuted instantiation: <hyper::common::buf::BufList<_> as bytes::buf::buf_impl::Buf>::copy_to_bytes
Unexecuted instantiation: <hyper::common::buf::BufList<_> as bytes::buf::buf_impl::Buf>::copy_to_bytes
93
}
94
95
#[cfg(test)]
96
mod tests {
97
    use std::ptr;
98
99
    use super::*;
100
101
    fn hello_world_buf() -> BufList<Bytes> {
102
        BufList {
103
            bufs: vec![Bytes::from("Hello"), Bytes::from(" "), Bytes::from("World")].into(),
104
        }
105
    }
106
107
    #[test]
108
    fn to_bytes_shorter() {
109
        let mut bufs = hello_world_buf();
110
        let old_ptr = bufs.chunk().as_ptr();
111
        let start = bufs.copy_to_bytes(4);
112
        assert_eq!(start, "Hell");
113
        assert!(ptr::eq(old_ptr, start.as_ptr()));
114
        assert_eq!(bufs.chunk(), b"o");
115
        assert!(ptr::eq(old_ptr.wrapping_add(4), bufs.chunk().as_ptr()));
116
        assert_eq!(bufs.remaining(), 7);
117
    }
118
119
    #[test]
120
    fn to_bytes_eq() {
121
        let mut bufs = hello_world_buf();
122
        let old_ptr = bufs.chunk().as_ptr();
123
        let start = bufs.copy_to_bytes(5);
124
        assert_eq!(start, "Hello");
125
        assert!(ptr::eq(old_ptr, start.as_ptr()));
126
        assert_eq!(bufs.chunk(), b" ");
127
        assert_eq!(bufs.remaining(), 6);
128
    }
129
130
    #[test]
131
    fn to_bytes_longer() {
132
        let mut bufs = hello_world_buf();
133
        let start = bufs.copy_to_bytes(7);
134
        assert_eq!(start, "Hello W");
135
        assert_eq!(bufs.remaining(), 4);
136
    }
137
138
    #[test]
139
    fn one_long_buf_to_bytes() {
140
        let mut buf = BufList::new();
141
        buf.push(b"Hello World" as &[_]);
142
        assert_eq!(buf.copy_to_bytes(5), "Hello");
143
        assert_eq!(buf.chunk(), b" World");
144
    }
145
146
    #[test]
147
    #[should_panic(expected = "`len` greater than remaining")]
148
    fn buf_to_bytes_too_many() {
149
        hello_world_buf().copy_to_bytes(42);
150
    }
151
}