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
use std::ops::{Deref, DerefMut};
pub const MAX_PACKET_LEN: usize = 32768;
pub trait Packet: Deref<Target = [u8]> + DerefMut {
fn capacity(&self) -> usize;
fn resize(&mut self, len: usize, val: u8);
fn extend(&mut self, other: &[u8]) {
let cur_len = self.len();
let new_len = cur_len + other.len();
self.resize(new_len, 0);
self[cur_len..new_len].copy_from_slice(other);
}
fn truncate(&mut self, len: usize) {
let len = len.min(self.len());
self.resize(len, 0);
}
fn clear(&mut self) {
self.resize(0, 0);
}
fn as_slice(&self) -> &[u8] {
self.deref()
}
fn as_mut_slice(&mut self) -> &mut [u8] {
self.deref_mut()
}
}
pub trait PacketPool {
type Packet: Packet;
fn acquire(&self) -> Self::Packet;
}