mysql_connector/utils/
buf_mut_ext.rs

1use bytes::BufMut;
2
3pub trait BufMutExt: BufMut {
4    #[allow(dead_code)]
5    fn put_lenenc_int(&mut self, n: u64) {
6        if n < 251 {
7            self.put_u8(n as u8);
8        } else if n < 65_536 {
9            self.put_u8(0xFC);
10            self.put_uint_le(n, 2);
11        } else if n < 16_777_216 {
12            self.put_u8(0xFD);
13            self.put_uint_le(n, 3);
14        } else {
15            self.put_u8(0xFE);
16            self.put_uint_le(n, 8);
17        }
18    }
19
20    #[allow(dead_code)]
21    fn put_lenenc_slice(&mut self, s: &[u8]) {
22        self.put_lenenc_int(s.len() as u64);
23        self.put_slice(s);
24    }
25
26    /// Writes a string with u8 length prefix. Truncates, if the length is greater that `u8::MAX`.
27    #[allow(dead_code)]
28    fn put_u8_slice(&mut self, s: &[u8]) {
29        let len = std::cmp::min(s.len(), u8::MAX as usize);
30        self.put_u8(len as u8);
31        self.put_slice(&s[..len]);
32    }
33
34    /// Writes a string with u32 length prefix. Truncates, if the length is greater that `u32::MAX`.
35    #[allow(dead_code)]
36    fn put_u32_slice(&mut self, s: &[u8]) {
37        let len = std::cmp::min(s.len(), u32::MAX as usize);
38        self.put_u32_le(len as u32);
39        self.put_slice(&s[..len]);
40    }
41
42    #[allow(dead_code)]
43    fn put_null_slice(&mut self, s: &[u8]) {
44        self.put_slice(s);
45        self.put_u8(0);
46    }
47}
48
49impl<T: BufMut> BufMutExt for T {}