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
use std::{collections::VecDeque, io};
#[derive(Debug)]
pub(crate) struct Chunks {
seq: VecDeque<Chunk>
}
impl Chunks {
pub(crate) fn new() -> Self {
Chunks { seq: VecDeque::new() }
}
pub(crate) fn len(&self) -> Option<usize> {
self.seq.iter().fold(Some(0), |total, x| {
total.and_then(|n| n.checked_add(x.len()))
})
}
pub(crate) fn push(&mut self, x: Vec<u8>) {
if !x.is_empty() {
self.seq.push_back(Chunk { cursor: io::Cursor::new(x) })
}
}
pub(crate) fn pop(&mut self) -> Option<Chunk> {
self.seq.pop_front()
}
pub(crate) fn front_mut(&mut self) -> Option<&mut Chunk> {
self.seq.front_mut()
}
}
#[derive(Debug)]
pub(crate) struct Chunk {
cursor: io::Cursor<Vec<u8>>
}
impl Chunk {
pub(crate) fn is_empty(&self) -> bool {
self.len() == 0
}
pub(crate) fn len(&self) -> usize {
self.cursor.get_ref().len() - self.offset()
}
pub(crate) fn offset(&self) -> usize {
self.cursor.position() as usize
}
pub(crate) fn advance(&mut self, amount: usize) {
assert!({
let pos = self.offset().checked_add(amount);
let max = self.cursor.get_ref().len();
pos.is_some() && pos <= Some(max)
});
self.cursor.set_position(self.cursor.position() + amount as u64);
}
pub(crate) fn into_vec(self) -> Vec<u8> {
self.cursor.into_inner()
}
}
impl AsRef<[u8]> for Chunk {
fn as_ref(&self) -> &[u8] {
&self.cursor.get_ref()[self.offset() ..]
}
}