Debian Codegraph @

src/io/buf_mut.rs

raw symbols SCIP
1 use bytes::BufMut;
2
3 pub trait MySqlBufMutExt: BufMut {
4 fn put_uint_lenenc(&mut self, v: u64);
5
6 fn put_str_lenenc(&mut self, v: &str);
7
8 fn put_bytes_lenenc(&mut self, v: &[u8]);
9 }
10
11 impl MySqlBufMutExt for Vec<u8> {
12 fn put_uint_lenenc(&mut self, v: u64) {
13 // https://dev.mysql.com/doc/internals/en/integer.html
14 // https://mariadb.com/kb/en/library/protocol-data-types/#length-encoded-integers
15
16 let encoded_le = v.to_le_bytes();
17
18 match v {
19 0..=250 => self.push(encoded_le[0]),
20 251..=0xFF_FF => {
21 self.push(0xfc);
22 self.extend_from_slice(&encoded_le[..2]);
23 }
24 0x1_00_00..=0xFF_FF_FF => {
25 self.push(0xfd);
26 self.extend_from_slice(&encoded_le[..3]);
27 }
28 _ => {
29 self.push(0xfe);
30 self.extend_from_slice(&encoded_le);
31 }
32 }
33 }
34
35 fn put_str_lenenc(&mut self, v: &str) {
36 self.put_bytes_lenenc(v.as_bytes());
37 }
38
39 fn put_bytes_lenenc(&mut self, v: &[u8]) {
40 self.put_uint_lenenc(v.len() as u64);
41 self.extend(v);
42 }
43 }
44
45 #[test]
46 fn test_encodes_int_lenenc_u8() {
47 let mut buf = Vec::with_capacity(1024);
48 buf.put_uint_lenenc(0xFA as u64);
49
50 assert_eq!(&buf[..], b"\xFA");
51 }
52
53 #[test]
54 fn test_encodes_int_lenenc_u16() {
55 let mut buf = Vec::with_capacity(1024);
56 buf.put_uint_lenenc(std::u16::MAX as u64);
57
58 assert_eq!(&buf[..], b"\xFC\xFF\xFF");
59 }
60
61 #[test]
62 fn test_encodes_int_lenenc_u24() {
63 let mut buf = Vec::with_capacity(1024);
64 buf.put_uint_lenenc(0xFF_FF_FF as u64);
65
66 assert_eq!(&buf[..], b"\xFD\xFF\xFF\xFF");
67 }
68
69 #[test]
70 fn test_encodes_int_lenenc_u64() {
71 let mut buf = Vec::with_capacity(1024);
72 buf.put_uint_lenenc(std::u64::MAX);
73
74 assert_eq!(&buf[..], b"\xFE\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF");
75 }
76
77 #[test]
78 fn test_encodes_int_lenenc_fb() {
79 let mut buf = Vec::with_capacity(1024);
80 buf.put_uint_lenenc(0xFB as u64);
81
82 assert_eq!(&buf[..], b"\xFC\xFB\x00");
83 }
84
85 #[test]
86 fn test_encodes_int_lenenc_fc() {
87 let mut buf = Vec::with_capacity(1024);
88 buf.put_uint_lenenc(0xFC as u64);
89
90 assert_eq!(&buf[..], b"\xFC\xFC\x00");
91 }
92
93 #[test]
94 fn test_encodes_int_lenenc_fd() {
95 let mut buf = Vec::with_capacity(1024);
96 buf.put_uint_lenenc(0xFD as u64);
97
98 assert_eq!(&buf[..], b"\xFC\xFD\x00");
99 }
100
101 #[test]
102 fn test_encodes_int_lenenc_fe() {
103 let mut buf = Vec::with_capacity(1024);
104 buf.put_uint_lenenc(0xFE as u64);
105
106 assert_eq!(&buf[..], b"\xFC\xFE\x00");
107 }
108
109 #[test]
110 fn test_encodes_int_lenenc_ff() {
111 let mut buf = Vec::with_capacity(1024);
112 buf.put_uint_lenenc(0xFF as u64);
113
114 assert_eq!(&buf[..], b"\xFC\xFF\x00");
115 }
116
117 #[test]
118 fn test_encodes_string_lenenc() {
119 let mut buf = Vec::with_capacity(1024);
120 buf.put_str_lenenc("random_string");
121
122 assert_eq!(&buf[..], b"\x0Drandom_string");
123 }
124
125 #[test]
126 fn test_encodes_byte_lenenc() {
127 let mut buf = Vec::with_capacity(1024);
128 buf.put_bytes_lenenc(b"random_string");
129
130 assert_eq!(&buf[..], b"\x0Drandom_string");
131 }