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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
use core::cmp;
use core::fmt;
use core::ops::{Deref, DerefMut};
use crate::{AsPixel, Pixel};
use crate::buf::{Buffer, buf};
use zerocopy::{AsBytes, FromBytes};
pub struct Rec<P: AsBytes + FromBytes> {
inner: Buffer,
length: usize,
pixel: Pixel<P>,
}
pub struct ReuseError {
requested: Option<usize>,
capacity: usize,
}
impl<P: AsBytes + FromBytes> Rec<P> {
pub fn new(count: usize) -> Self where P: AsPixel {
Self::new_for_pixel(P::pixel(), count)
}
pub fn new_for_pixel(pixel: Pixel<P>, count: usize) -> Self {
Self::bytes_for_pixel(pixel, mem_size(pixel, count))
}
pub fn bytes_for_pixel(pixel: Pixel<P>, mem_size: usize) -> Self {
Rec {
inner: Buffer::new(mem_size),
length: mem_size,
pixel,
}
}
pub fn resize(&mut self, count: usize) {
self.resize_bytes(mem_size(self.pixel, count))
}
pub fn resize_bytes(&mut self, bytes: usize) {
self.inner.grow_to(bytes);
self.length = bytes;
}
pub fn reuse(&mut self, count: usize) -> Result<(), ReuseError> {
let bytes = count
.checked_mul(self.pixel.size())
.ok_or_else(|| ReuseError {
requested: None,
capacity: self.byte_capacity(),
})?;
self.reuse_bytes(bytes)
}
pub fn reuse_bytes(&mut self, bytes: usize) -> Result<(), ReuseError> {
if bytes > self.byte_capacity() {
return Err(ReuseError {
requested: Some(bytes),
capacity: self.capacity(),
})
}
Ok(self.resize_bytes(bytes))
}
pub fn shrink_to_fit(&mut self) {
let exact_size = mem_size(self.pixel, self.len());
self.inner.resize_to(exact_size);
self.length = exact_size;
}
pub fn as_slice(&self) -> &[P] {
self.buf().as_pixels(self.pixel)
}
pub fn as_mut_slice(&mut self) -> &mut [P] {
let pixel = self.pixel;
self.buf_mut().as_mut_pixels(pixel)
}
pub fn len(&self) -> usize {
self.as_slice().len()
}
pub fn capacity(&self) -> usize {
self.inner.capacity() / self.pixel.size()
}
pub fn as_bytes(&self) -> &[u8] {
self.buf().as_bytes()
}
pub fn as_bytes_mut(&mut self) -> &mut [u8] {
self.buf_mut().as_bytes_mut()
}
pub fn byte_len(&self) -> usize {
self.as_bytes().len()
}
pub fn byte_capacity(&self) -> usize {
self.inner.capacity()
}
pub fn reinterpret<Q>(self) -> Rec<Q>
where Q: AsPixel + AsBytes + FromBytes
{
self.reinterpret_to(Q::pixel())
}
pub fn reinterpret_to<Q>(self, pixel: Pixel<Q>) -> Rec<Q>
where Q: AsBytes + FromBytes
{
Rec {
inner: self.inner,
length: self.length,
pixel,
}
}
fn buf(&self) -> &buf {
&self.inner[..self.length]
}
fn buf_mut(&mut self) -> &mut buf {
&mut self.inner[..self.length]
}
}
fn mem_size<P>(pixel: Pixel<P>, count: usize) -> usize {
pixel.size().checked_mul(count).unwrap_or_else(
|| panic!("Requested count overflows memory size"))
}
impl<P: AsBytes + FromBytes> Deref for Rec<P> {
type Target = [P];
fn deref(&self) -> &[P] {
self.as_slice()
}
}
impl<P: AsBytes + FromBytes> DerefMut for Rec<P> {
fn deref_mut(&mut self) -> &mut [P] {
self.as_mut_slice()
}
}
impl<P: AsBytes + FromBytes> Clone for Rec<P> {
fn clone(&self) -> Self {
Rec {
inner: self.inner.clone(),
.. *self
}
}
}
impl<P: AsBytes + FromBytes + AsPixel> Default for Rec<P> {
fn default() -> Self {
Rec {
inner: Buffer::default(),
length: 0,
pixel: P::pixel(),
}
}
}
impl<P: AsBytes + FromBytes + cmp::PartialEq> cmp::PartialEq for Rec<P> {
fn eq(&self, other: &Self) -> bool {
self.as_slice().eq(other.as_slice())
}
}
impl<P: AsBytes + FromBytes + cmp::Eq> cmp::Eq for Rec<P> { }
impl<P: AsBytes + FromBytes + cmp::PartialOrd> cmp::PartialOrd for Rec<P> {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
self.as_slice().partial_cmp(other.as_slice())
}
}
impl<P: AsBytes + FromBytes + cmp::Ord> cmp::Ord for Rec<P> {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.as_slice().cmp(other.as_slice())
}
}
impl<P: AsBytes + FromBytes + fmt::Debug> fmt::Debug for Rec<P> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list()
.entries(self.as_slice().iter())
.finish()
}
}
impl fmt::Debug for ReuseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.requested {
None => {
write!(f, "Buffer reuse failed: Bytes count can not be expressed")
},
Some(requested) => {
write!(f, "Buffer reuse failed: {} bytes requested, only {} available",
requested, self.capacity)
},
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn resize() {
let mut buffer: Rec<u8> = Rec::new(0);
assert_eq!(buffer.capacity(), 0);
assert_eq!(buffer.len(), 0);
buffer.resize(4);
assert!(buffer.capacity() >= 4);
assert_eq!(buffer.len(), 4);
buffer.resize(2);
assert!(buffer.capacity() >= 2);
assert_eq!(buffer.len(), 2);
buffer.resize(0);
buffer.shrink_to_fit();
assert_eq!(buffer.capacity(), 0);
assert_eq!(buffer.len(), 0);
}
}