1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/// A set of reusable buffers for MySQL protocol communication
///
/// `Conn` uses a single `BufferSet` for all its operations.
#[derive(Debug)]
pub struct BufferSet {
/// Bytes are valid during Conn.
pub initial_handshake: Vec<u8>,
/// General-purpose read buffer
/// Bytes are valid during an operation.
pub read_buffer: Vec<u8>,
/// General-purpose write buffer
/// It always has at least 4 bytes which is reserved for the first packet header.
/// It is followed by payload bytes without considering 16MB split.
/// Layout: [4-byte header space][payload that is possibly larger than 16MB]
/// Bytes are valid during an operation.
pub write_buffer: Vec<u8>,
/// ColumnDefinition packets in one buffer
/// Bytes are valid during an operation.
pub column_definition_buffer: Vec<u8>,
}
impl BufferSet {
/// Create a new empty buffer set
pub fn new() -> Self {
Self {
initial_handshake: Vec::new(),
read_buffer: Vec::new(),
write_buffer: vec![0; 4],
column_definition_buffer: Vec::new(),
}
}
/// Create a new buffer set with the initial handshake packet
pub fn with_initial_handshake(initial_handshake: Vec<u8>) -> Self {
Self {
initial_handshake,
read_buffer: Vec::new(),
write_buffer: vec![0; 4],
column_definition_buffer: Vec::new(),
}
}
/// Clear the write buffer, reserve 4 bytes for the header, and return mutable access.
#[inline]
pub fn new_write_buffer(&mut self) -> &mut Vec<u8> {
self.write_buffer.clear();
self.write_buffer.extend_from_slice(&[0_u8; 4]);
&mut self.write_buffer
}
/// Get mutable access to the write buffer.
#[inline]
pub fn write_buffer_mut(&mut self) -> &mut Vec<u8> {
&mut self.write_buffer
}
/// Get the write buffer for reading.
#[inline]
pub fn write_buffer(&self) -> &[u8] {
&self.write_buffer
}
/// Get the payload length (total buffer length minus 4-byte header).
#[inline]
pub fn payload_len(&self) -> usize {
self.write_buffer.len().saturating_sub(4)
}
}
impl Default for BufferSet {
fn default() -> Self {
Self::new()
}
}