cubic_protocol/
packet_bytes.rs

1use crate::packet::{InputPacketBytes, InputPacketBytesError, InputPacketBytesResult, OutputPacketBytes, OutputPacketBytesResult};
2
3#[derive(Debug, Default)]
4pub struct OutputPacketBytesVec {
5    pub data: Vec<u8>,
6}
7
8#[derive(Debug)]
9pub struct InputPacketBytesPrepared {
10    pub data: Box<[u8]>,
11    pub offset: usize,
12}
13
14impl OutputPacketBytesVec {
15    pub fn new() -> Self {
16        Self::default()
17    }
18}
19
20#[async_trait::async_trait]
21impl OutputPacketBytes for OutputPacketBytesVec {
22    async fn write_byte(&mut self, byte: u8) -> OutputPacketBytesResult {
23        Ok(self.data.push(byte))
24    }
25
26    async fn write_bytes(&mut self, slice: &[u8]) -> OutputPacketBytesResult {
27        let mut index = self.data.len();
28        self.data.resize(self.data.len() + slice.len(), 0);
29        Ok(
30            slice.iter().for_each(|byte| {
31                self.data[index] = *byte;
32                index += 1;
33            })
34        )
35    }
36}
37
38impl From<Vec<u8>> for OutputPacketBytesVec {
39    fn from(data: Vec<u8>) -> Self {
40        Self { data }
41    }
42}
43
44impl From<OutputPacketBytesVec> for Vec<u8> {
45    fn from(data: OutputPacketBytesVec) -> Self {
46        data.data
47    }
48}
49
50impl From<OutputPacketBytesVec> for InputPacketBytesPrepared {
51    fn from(output: OutputPacketBytesVec) -> Self {
52        InputPacketBytesPrepared {
53            data: output.data.into_boxed_slice(),
54            offset: 0,
55        }
56    }
57}
58
59#[async_trait::async_trait]
60impl InputPacketBytes for InputPacketBytesPrepared {
61    async fn take_byte(&mut self) -> InputPacketBytesResult<u8> {
62        match self.has_bytes(1) {
63            true => {
64                let byte = self.data[self.offset];
65                self.offset += 1;
66                Ok(byte)
67            },
68            false => Err(InputPacketBytesError::NoBytes(self.data.len())),
69        }
70    }
71
72    async fn take_slice(&mut self, slice: &mut [u8]) -> InputPacketBytesResult<()> {
73        match self.has_bytes(slice.len()) {
74            true => {
75                for index in 0..slice.len() {
76                    slice[index] = self.data[self.offset];
77                    self.offset += 1;
78                }
79                Ok(())
80            },
81            false => Err(InputPacketBytesError::NoBytes(self.data.len())),
82        }
83    }
84
85    async fn take_vec(&mut self, vec: &mut Vec<u8>, count: usize) -> InputPacketBytesResult<()> {
86        match self.has_bytes(count) {
87            true => {
88                vec.resize(count, 0);
89                for index in 0..count {
90                    vec[index] = self.data[self.offset];
91                    self.offset += 1;
92                }
93                Ok(())
94            },
95            false => Err(InputPacketBytesError::NoBytes(self.data.len()))
96        }
97    }
98
99    fn has_bytes(&self, count: usize) -> bool {
100        self.remaining_bytes() >= count
101    }
102
103    fn remaining_bytes(&self) -> usize {
104        match self.data.len() > self.offset {
105            true => self.data.len() - self.offset,
106            false => 0,
107        }
108    }
109}
110
111impl From<Vec<u8>> for InputPacketBytesPrepared {
112    fn from(data: Vec<u8>) -> Self {
113        InputPacketBytesPrepared {
114            data: data.into_boxed_slice(),
115            offset: 0
116        }
117    }
118}