Coverage Report

Created: 2026-02-14 06:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/tonic-0.14.2/src/codec/buffer.rs
Line
Count
Source
1
use bytes::buf::UninitSlice;
2
use bytes::{Buf, BufMut, Bytes, BytesMut};
3
4
/// A specialized buffer to decode gRPC messages from.
5
#[derive(Debug)]
6
pub struct DecodeBuf<'a> {
7
    buf: &'a mut BytesMut,
8
    len: usize,
9
}
10
11
/// A specialized buffer to encode gRPC messages into.
12
#[derive(Debug)]
13
pub struct EncodeBuf<'a> {
14
    buf: &'a mut BytesMut,
15
}
16
17
impl<'a> DecodeBuf<'a> {
18
0
    pub(crate) fn new(buf: &'a mut BytesMut, len: usize) -> Self {
19
0
        DecodeBuf { buf, len }
20
0
    }
21
}
22
23
impl Buf for DecodeBuf<'_> {
24
    #[inline]
25
0
    fn remaining(&self) -> usize {
26
0
        self.len
27
0
    }
Unexecuted instantiation: <tonic::codec::buffer::DecodeBuf as bytes::buf::buf_impl::Buf>::remaining
Unexecuted instantiation: <tonic::codec::buffer::DecodeBuf as bytes::buf::buf_impl::Buf>::remaining
28
29
    #[inline]
30
0
    fn chunk(&self) -> &[u8] {
31
0
        let ret = self.buf.chunk();
32
33
0
        if ret.len() > self.len {
34
0
            &ret[..self.len]
35
        } else {
36
0
            ret
37
        }
38
0
    }
Unexecuted instantiation: <tonic::codec::buffer::DecodeBuf as bytes::buf::buf_impl::Buf>::chunk
Unexecuted instantiation: <tonic::codec::buffer::DecodeBuf as bytes::buf::buf_impl::Buf>::chunk
39
40
    #[inline]
41
0
    fn advance(&mut self, cnt: usize) {
42
0
        assert!(cnt <= self.len);
43
0
        self.buf.advance(cnt);
44
0
        self.len -= cnt;
45
0
    }
Unexecuted instantiation: <tonic::codec::buffer::DecodeBuf as bytes::buf::buf_impl::Buf>::advance
Unexecuted instantiation: <tonic::codec::buffer::DecodeBuf as bytes::buf::buf_impl::Buf>::advance
46
47
    #[inline]
48
0
    fn copy_to_bytes(&mut self, len: usize) -> Bytes {
49
0
        assert!(len <= self.len);
50
0
        self.len -= len;
51
0
        self.buf.copy_to_bytes(len)
52
0
    }
Unexecuted instantiation: <tonic::codec::buffer::DecodeBuf as bytes::buf::buf_impl::Buf>::copy_to_bytes
Unexecuted instantiation: <tonic::codec::buffer::DecodeBuf as bytes::buf::buf_impl::Buf>::copy_to_bytes
53
}
54
55
impl<'a> EncodeBuf<'a> {
56
0
    pub(crate) fn new(buf: &'a mut BytesMut) -> Self {
57
0
        EncodeBuf { buf }
58
0
    }
59
}
60
61
impl EncodeBuf<'_> {
62
    /// Reserves capacity for at least `additional` more bytes to be inserted
63
    /// into the buffer.
64
    ///
65
    /// More than `additional` bytes may be reserved in order to avoid frequent
66
    /// reallocations. A call to `reserve` may result in an allocation.
67
    #[inline]
68
0
    pub fn reserve(&mut self, additional: usize) {
69
0
        self.buf.reserve(additional);
70
0
    }
71
}
72
73
unsafe impl BufMut for EncodeBuf<'_> {
74
    #[inline]
75
0
    fn remaining_mut(&self) -> usize {
76
0
        self.buf.remaining_mut()
77
0
    }
Unexecuted instantiation: <tonic::codec::buffer::EncodeBuf as bytes::buf::buf_mut::BufMut>::remaining_mut
Unexecuted instantiation: <tonic::codec::buffer::EncodeBuf as bytes::buf::buf_mut::BufMut>::remaining_mut
78
79
    #[inline]
80
0
    unsafe fn advance_mut(&mut self, cnt: usize) {
81
0
        self.buf.advance_mut(cnt)
82
0
    }
83
84
    #[inline]
85
0
    fn chunk_mut(&mut self) -> &mut UninitSlice {
86
0
        self.buf.chunk_mut()
87
0
    }
88
89
    #[inline]
90
0
    fn put<T: Buf>(&mut self, src: T)
91
0
    where
92
0
        Self: Sized,
93
    {
94
0
        self.buf.put(src)
95
0
    }
Unexecuted instantiation: <tonic::codec::buffer::EncodeBuf as bytes::buf::buf_mut::BufMut>::put::<&[u8]>
Unexecuted instantiation: <tonic::codec::buffer::EncodeBuf as bytes::buf::buf_mut::BufMut>::put::<_>
96
97
    #[inline]
98
0
    fn put_slice(&mut self, src: &[u8]) {
99
0
        self.buf.put_slice(src)
100
0
    }
Unexecuted instantiation: <tonic::codec::buffer::EncodeBuf as bytes::buf::buf_mut::BufMut>::put_slice
Unexecuted instantiation: <tonic::codec::buffer::EncodeBuf as bytes::buf::buf_mut::BufMut>::put_slice
101
102
    #[inline]
103
0
    fn put_bytes(&mut self, val: u8, cnt: usize) {
104
0
        self.buf.put_bytes(val, cnt);
105
0
    }
106
}
107
108
#[cfg(test)]
109
mod tests {
110
    use super::*;
111
112
    #[test]
113
    fn decode_buf() {
114
        let mut payload = BytesMut::with_capacity(100);
115
        payload.put(&vec![0u8; 50][..]);
116
        let mut buf = DecodeBuf::new(&mut payload, 20);
117
118
        assert_eq!(buf.len, 20);
119
        assert_eq!(buf.remaining(), 20);
120
        assert_eq!(buf.chunk().len(), 20);
121
122
        buf.advance(10);
123
        assert_eq!(buf.remaining(), 10);
124
125
        let mut out = [0; 5];
126
        buf.copy_to_slice(&mut out);
127
        assert_eq!(buf.remaining(), 5);
128
        assert_eq!(buf.chunk().len(), 5);
129
130
        assert_eq!(buf.copy_to_bytes(5).len(), 5);
131
        assert!(!buf.has_remaining());
132
    }
133
134
    #[test]
135
    fn encode_buf() {
136
        let mut bytes = BytesMut::with_capacity(100);
137
        let mut buf = EncodeBuf::new(&mut bytes);
138
139
        let initial = buf.remaining_mut();
140
        unsafe { buf.advance_mut(20) };
141
        assert_eq!(buf.remaining_mut(), initial - 20);
142
143
        buf.put_u8(b'a');
144
        assert_eq!(buf.remaining_mut(), initial - 20 - 1);
145
    }
146
}