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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
use std::io;
use super::*;
/// A `Reserve` allows a reader to read everything
/// except for the last N bytes (the reserve) from the underlying
/// `BufferedReader`.
///
/// Note: because the `Reserve` doesn't generally know
/// how much data can be read from the underlying `BufferedReader`,
/// it causes at least N bytes to by buffered.
#[derive(Debug)]
pub struct Reserve<T: BufferedReader<C>, C: fmt::Debug> {
reader: T,
reserve: usize,
cookie: C,
}
impl<T: BufferedReader<C>, C: fmt::Debug> fmt::Display for Reserve<T, C> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Reserve")
.field("reserve", &self.reserve)
.finish()
}
}
impl<T: BufferedReader<()>> Reserve<T, ()> {
/// Instantiates a new `Reserve`.
///
/// `reader` is the source to wrap. `reserve` is the number of
/// bytes that will not be returned to the reader.
pub fn new(reader: T, reserve: usize) -> Self {
Self::with_cookie(reader, reserve, ())
}
}
impl<T: BufferedReader<C>, C: fmt::Debug> Reserve<T, C> {
/// Like `new()`, but sets a cookie.
///
/// The cookie can be retrieved using the `cookie_ref` and
/// `cookie_mut` methods, and set using the `cookie_set` method.
pub fn with_cookie(reader: T, reserve: usize, cookie: C)
-> Reserve<T, C> {
Reserve {
reader,
reserve,
cookie,
}
}
}
impl<T: BufferedReader<C>, C: fmt::Debug> io::Read for Reserve<T, C> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
let to_read = {
let data = self.reader.data(buf.len() + self.reserve)?;
if data.len() > self.reserve {
data.len() - self.reserve
} else {
return Ok(0);
}
};
let to_read = cmp::min(buf.len(), to_read);
return self.reader.read(&mut buf[..to_read]);
}
}
impl<T: BufferedReader<C>, C: fmt::Debug> BufferedReader<C> for Reserve<T, C> {
fn buffer(&self) -> &[u8] {
let buf = self.reader.buffer();
if buf.len() > self.reserve {
&buf[..buf.len() - self.reserve]
} else {
b""
}
}
/// Return the buffer. Ensure that it contains at least `amount`
/// bytes.
fn data(&mut self, amount: usize) -> Result<&[u8], io::Error> {
let data = self.reader.data(amount + self.reserve)?;
if data.len() <= self.reserve {
// EOF.
Ok(b"")
} else {
// More than enough.
Ok(&data[..data.len() - self.reserve])
}
}
fn consume(&mut self, amount: usize) -> &[u8] {
assert!(amount <= self.buffer().len());
// consume may return more than amount. If it does, make sure
// it doesn't return any of the reserve.
let data = self.reader.consume(amount);
assert!(data.len() >= amount);
if data.len() > amount {
// We got more than `amount`. We need to be careful to
// not return data from the reserve. But, we also know
// that `amount` does not include data from the reserve.
if data.len() > amount + self.reserve {
return &data[..data.len() - self.reserve];
}
}
&data[..amount]
}
fn data_consume(&mut self, amount: usize) -> Result<&[u8], io::Error> {
let amount = cmp::min(amount, self.data(amount)?.len());
Ok(self.consume(amount))
}
fn data_consume_hard(&mut self, amount: usize) -> Result<&[u8], io::Error> {
self.data_hard(amount)?;
Ok(self.consume(amount))
}
fn get_mut(&mut self) -> Option<&mut dyn BufferedReader<C>> {
Some(&mut self.reader)
}
fn get_ref(&self) -> Option<&dyn BufferedReader<C>> {
Some(&self.reader)
}
fn into_inner<'b>(self: Box<Self>) -> Option<Box<dyn BufferedReader<C> + 'b>>
where Self: 'b {
Some(self.reader.as_boxed())
}
fn cookie_set(&mut self, cookie: C) -> C {
use std::mem;
mem::replace(&mut self.cookie, cookie)
}
fn cookie_ref(&self) -> &C {
&self.cookie
}
fn cookie_mut(&mut self) -> &mut C {
&mut self.cookie
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn data() {
use crate::Memory;
// orig is the original buffer
//
// cursor is the Reserve's position in orig.
//
// to_read is how much to read.
//
// total is the total to_read that be read from orig.
//
// cursor / reserve \
// orig: [ | to_read | | ]
// \ total /
//
fn read_chunk<'a, R: BufferedReader<C>, C: fmt::Debug>(
orig: &[u8], r: &mut R, to_read: usize, cursor: usize, total: usize,
mode: usize)
{
// Use data.
let data_len = {
let data = r.data(to_read).unwrap();
assert_eq!(data, &orig[cursor..cursor + data.len()]);
data.len()
};
assert!(data_len <= total - cursor);
assert_eq!(r.buffer().len(), data_len);
// Use data_ha
|