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
use std::collections::VecDeque;
#[derive(Clone)]
pub struct Buffer {
pub data: VecDeque<u8>,
pub size: usize,
}
impl Default for Buffer {
fn default() -> Self {
Self::new()
}
}
impl Buffer {
pub fn new() -> Self {
Self {
data: VecDeque::<u8>::new(),
size: 0,
}
}
pub fn len(&self) -> usize {
self.size
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn add(&mut self, data: Vec<u8>) {
self.size += data.len();
self.data.extend(data);
}
pub fn get(&mut self, numb: usize) -> Vec<u8> {
if numb > self.size || numb == 0 {
self.data.drain(..).collect()
} else if numb <= self.size {
self.data.drain(0..numb).collect()
} else {
self.data.drain(..).collect()
}
}
pub fn unget(&mut self, data: Vec<u8>) {
self.size += data.len();
for item in data.iter().rev() {
self.data.push_back(*item);
}
}
}