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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use crate::packet::{InputPacketBytes, InputPacketBytesError, InputPacketBytesResult, OutputPacketBytes, OutputPacketBytesResult};
#[derive(Debug, Default)]
pub struct OutputPacketBytesVec {
pub data: Vec<u8>,
}
#[derive(Debug)]
pub struct InputPacketBytesPrepared {
pub data: Box<[u8]>,
pub offset: usize,
}
impl OutputPacketBytesVec {
pub fn new() -> Self {
Self::default()
}
}
#[async_trait::async_trait]
impl OutputPacketBytes for OutputPacketBytesVec {
async fn write_byte(&mut self, byte: u8) -> OutputPacketBytesResult {
Ok(self.data.push(byte))
}
async fn write_bytes(&mut self, slice: &[u8]) -> OutputPacketBytesResult {
let mut index = self.data.len();
self.data.resize(self.data.len() + slice.len(), 0);
Ok(
slice.iter().for_each(|byte| {
self.data[index] = *byte;
index += 1;
})
)
}
}
impl From<Vec<u8>> for OutputPacketBytesVec {
fn from(data: Vec<u8>) -> Self {
Self { data }
}
}
impl From<OutputPacketBytesVec> for Vec<u8> {
fn from(data: OutputPacketBytesVec) -> Self {
data.data
}
}
impl From<OutputPacketBytesVec> for InputPacketBytesPrepared {
fn from(output: OutputPacketBytesVec) -> Self {
InputPacketBytesPrepared {
data: output.data.into_boxed_slice(),
offset: 0,
}
}
}
#[async_trait::async_trait]
impl InputPacketBytes for InputPacketBytesPrepared {
async fn take_byte(&mut self) -> InputPacketBytesResult<u8> {
match self.has_bytes(1) {
true => {
let byte = self.data[self.offset];
self.offset += 1;
Ok(byte)
},
false => Err(InputPacketBytesError::NoBytes(self.data.len())),
}
}
async fn take_slice(&mut self, slice: &mut [u8]) -> InputPacketBytesResult<()> {
match self.has_bytes(slice.len()) {
true => {
for index in 0..slice.len() {
slice[index] = self.data[self.offset];
self.offset += 1;
}
Ok(())
},
false => Err(InputPacketBytesError::NoBytes(self.data.len())),
}
}
async fn take_vec(&mut self, vec: &mut Vec<u8>, count: usize) -> InputPacketBytesResult<()> {
match self.has_bytes(count) {
true => {
vec.resize(count, 0);
for index in 0..count {
vec[index] = self.data[self.offset];
self.offset += 1;
}
Ok(())
},
false => Err(InputPacketBytesError::NoBytes(self.data.len()))
}
}
fn has_bytes(&self, count: usize) -> bool {
self.remaining_bytes() >= count
}
fn remaining_bytes(&self) -> usize {
match self.data.len() > self.offset {
true => self.data.len() - self.offset,
false => 0,
}
}
}
impl From<Vec<u8>> for InputPacketBytesPrepared {
fn from(data: Vec<u8>) -> Self {
InputPacketBytesPrepared {
data: data.into_boxed_slice(),
offset: 0
}
}
}