-
-
Notifications
You must be signed in to change notification settings - Fork 993
Expand file tree
/
Copy pathbuffer.rs
More file actions
631 lines (531 loc) · 17.2 KB
/
buffer.rs
File metadata and controls
631 lines (531 loc) · 17.2 KB
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
//! Shared-ownership buffer types
use gc_arena::Collect;
use std::cmp::min;
use std::fmt::{Debug, Formatter, LowerHex, UpperHex};
use std::io::{Error as IoError, Read, Result as IoResult};
use std::ops::{Bound, Deref, RangeBounds};
use std::sync::{Arc, RwLock, RwLockReadGuard};
use thiserror::Error;
/// A shared data buffer.
///
/// `Buffer` is intended to mirror the API of a `Vec<u8>`, but with shared
/// ownership. Mutability is partially supported: you may append data to the
/// end of the buffer, but not change or remove bytes already added to the
/// buffer.
///
/// Buffer data may also be sliced to yield references to the underlying data.
/// See `Slice` for more info.
#[derive(Clone, Debug, Collect)]
#[collect(require_static)]
pub struct Buffer(Arc<RwLock<Vec<u8>>>);
impl Buffer {
pub fn new() -> Self {
Buffer(Arc::new(RwLock::new(Vec::new())))
}
pub fn with_capacity(cap: usize) -> Self {
Buffer(Arc::new(RwLock::new(Vec::with_capacity(cap))))
}
pub fn capacity(&self) -> usize {
self.0.read().expect("unlock read").capacity()
}
pub fn len(&self) -> usize {
self.0.read().expect("unlock read").len()
}
pub fn is_empty(&self) -> bool {
self.0.read().expect("unlock read").is_empty()
}
pub fn reserve(&mut self, additional: usize) {
self.0.write().expect("unlock write").reserve(additional)
}
pub fn reserve_exact(&mut self, additional: usize) {
self.0
.write()
.expect("unlock write")
.reserve_exact(additional)
}
pub fn as_slice(&self) -> Slice {
let end = self.0.read().expect("unlock read").len();
Slice {
buf: self.clone(),
start: 0,
end,
}
}
pub fn append(&mut self, other: &mut Vec<u8>) {
self.0.write().expect("unlock write").append(other)
}
pub fn extend_from_slice(&mut self, other: &[u8]) {
self.0
.write()
.expect("unlock write")
.extend_from_slice(other)
}
pub fn get<T: RangeBounds<usize>>(&self, range: T) -> Option<Slice> {
let s = self.0.read().expect("unlock read");
let start = match range.start_bound() {
Bound::Included(u) => *u,
Bound::Excluded(u) => *u + 1,
Bound::Unbounded => 0,
};
let end = match range.end_bound() {
Bound::Included(u) => *u + 1,
Bound::Excluded(u) => *u,
Bound::Unbounded => s.len(),
};
if start <= s.len() && end <= s.len() && start <= end {
Some(Slice {
buf: Self(self.0.clone()),
start,
end,
})
} else {
None
}
}
pub fn to_full_slice(&self) -> Slice {
self.get(0..).expect("full slices are always valid")
}
pub fn to_empty_slice(&self) -> Slice {
self.get(0..0).expect("empty slices are always valid")
}
}
impl Default for Buffer {
fn default() -> Self {
Buffer::new()
}
}
impl From<Vec<u8>> for Buffer {
fn from(val: Vec<u8>) -> Self {
Self(Arc::new(RwLock::new(val)))
}
}
impl PartialEq for Buffer {
fn eq(&self, other: &Buffer) -> bool {
Arc::ptr_eq(&self.0, &other.0)
}
}
/// A reference into a shared data buffer.
///
/// `Slice` is intended to mirror the API of a `&[u8]`, but without retaining
/// a borrow. Ownership of a `Slice` keeps the underlying buffer data alive,
/// and you can read the buffer data at any time.
///
/// Slice bounds are interpreted as a [start..end] pair, i.e. inclusive on the
/// start bound and exclusive on the end.
#[derive(Clone, Debug)]
pub struct Slice {
buf: Buffer,
start: usize,
end: usize,
}
impl Slice {
/// Create a subslice of this buffer slice.
///
/// The parameter `slice` must be derived from the same buffer this slice
/// was, and must also be within bounds of this slice. If not, then the
/// returned slice will be empty.
pub fn to_subslice(&self, slice: &[u8]) -> Self {
let self_guard = self.buf.0.read().expect("unlock read");
let self_pval = self_guard.as_ptr() as usize;
let slice_pval = slice.as_ptr() as usize;
if (self_pval + self.start) <= slice_pval && slice_pval < (self_pval + self.end) {
Self {
buf: self.buf.clone(),
start: slice_pval - self_pval,
end: (slice_pval - self_pval) + slice.len(),
}
} else {
self.buf.to_empty_slice()
}
}
/// Get a subslice of this slice.
///
/// Normal subslicing bounds rules will be respected.
pub fn get<T: RangeBounds<usize>>(&self, range: T) -> Option<Slice> {
let s = self.buf.0.read().expect("unlock read");
let start = match range.start_bound() {
Bound::Included(u) => *u,
Bound::Excluded(u) => *u + 1,
Bound::Unbounded => 0,
};
let end = match range.end_bound() {
Bound::Included(u) => *u + 1,
Bound::Excluded(u) => *u,
Bound::Unbounded => s.len(),
};
if start <= s.len() && end <= s.len() && start <= end {
Some(Slice {
buf: self.buf.clone(),
start: self.start + start,
end: self.start + end,
})
} else {
None
}
}
/// Checks if this slice is empty
pub fn is_empty(&self) -> bool {
self.end == self.start
}
/// Get the length of the Slice.
pub fn len(&self) -> usize {
self.end - self.start
}
pub fn start(&self) -> usize {
self.start
}
pub fn end(&self) -> usize {
self.end
}
pub fn data(&self) -> SliceRef<'_> {
SliceRef {
guard: self.buf.0.read().expect("unlock read"),
start: self.start,
end: self.end,
}
}
pub fn buffer(&self) -> &Buffer {
&self.buf
}
}
impl Read for Slice {
fn read(&mut self, data: &mut [u8]) -> IoResult<usize> {
let len = <&[u8] as Read>::read(&mut self.data().as_ref(), data)?;
self.start += len;
Ok(len)
}
}
impl LowerHex for Slice {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
LowerHex::fmt(&self.data(), f)
}
}
impl UpperHex for Slice {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
UpperHex::fmt(&self.data(), f)
}
}
#[derive(Debug, Error)]
pub enum SubstreamError {
#[error("Attempted to add substream chunk from a foreign buffer")]
ForeignBuffer,
}
/// A list of multiple slices of the same buffer.
///
/// `Substream` represents a substream of the underlying `Buffer`. `Slice`s can
/// be appended to the chunks list in order to extend the substream, in the
/// same way that the underlying `Buffer` can be extended. All `Slice`s must be
/// backed by the same `Buffer` in order to be part of the same `Substream`.
///
/// Clones of a `Substream` share a single chunk list, and appending chunks
/// will extend all clones of the `Substream`.
#[derive(Clone, Debug)]
pub struct Substream {
buf: Buffer,
/// Shared list of chunks. Chunks are stored as (start, end) pairs.
chunks: Arc<RwLock<Vec<(usize, usize)>>>,
}
impl Substream {
pub fn new(buf: Buffer) -> Self {
Self {
buf,
chunks: Arc::new(RwLock::new(vec![])),
}
}
/// Append another `Slice` onto the end of the `Substream`.
///
/// Appended chunks will be present in all clones of the `Substream`.
pub fn append(&mut self, slice: Slice) -> Result<(), SubstreamError> {
let mut chunks = self.chunks.write().unwrap();
if self.buf == slice.buf {
chunks.push((slice.start, slice.end));
Ok(())
} else {
Err(SubstreamError::ForeignBuffer)
}
}
/// Calculate the number of chunks in the `Substream`.
pub fn num_chunks(&self) -> usize {
self.chunks.read().unwrap().len()
}
/// Create a readable cursor into the `Substream`.
///
/// The returned cursor clones the `Substream` and thus shares a chunk list
/// with it.
pub fn as_cursor(&self) -> SubstreamCursor {
SubstreamCursor {
substream: self.clone(),
chunk_pos: 0,
bytes_pos: 0,
}
}
/// Calculate the number of bytes in the `Substream`.
pub fn len(&self) -> usize {
let mut tally = 0;
let chunks = self.chunks.read().unwrap();
for (chunk_start, chunk_end) in chunks.iter() {
tally += chunk_end - chunk_start;
}
tally
}
/// Determine if the `Substream` is empty.
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Create a chunk iterator into the `Substream`.
///
/// The returned iterator clones the `Substream` and thus shares a chunk
/// list with it.
pub fn iter_chunks(&self) -> SubstreamChunksIter {
SubstreamChunksIter {
substream: self.clone(),
next_buf: 0,
}
}
pub fn buffer(&self) -> &Buffer {
&self.buf
}
pub fn first_chunk(&self) -> Option<Slice> {
if let Some((start, end)) = self.chunks.read().unwrap().first() {
Some(Slice {
buf: self.buf.clone(),
start: *start,
end: *end,
})
} else {
None
}
}
pub fn last_chunk(&self) -> Option<Slice> {
if let Some((start, end)) = self.chunks.read().unwrap().last() {
Some(Slice {
buf: self.buf.clone(),
start: *start,
end: *end,
})
} else {
None
}
}
}
impl From<Buffer> for Substream {
fn from(buf: Buffer) -> Self {
Self {
buf,
chunks: Arc::new(RwLock::new(vec![])),
}
}
}
impl From<Slice> for Substream {
fn from(slice: Slice) -> Self {
Self {
buf: slice.buf,
chunks: Arc::new(RwLock::new(vec![(slice.start, slice.end)])),
}
}
}
/// A readable cursor into a buffer substream.
///
/// Reads from the cursor (via `Read` etc) will be filled as if the substream
/// referred to in the chunks list is a single contiguous stream of bytes with
/// no other data in between. Code using a `SubstreamCursor` can thus work with
/// both multiplexed and unmultiplexed data in the same way.
pub struct SubstreamCursor {
substream: Substream,
chunk_pos: usize,
bytes_pos: usize,
}
impl Read for SubstreamCursor {
fn read(&mut self, data: &mut [u8]) -> IoResult<usize> {
let mut out_count = 0;
let buf_owned = self.substream.buf.clone();
let buf = buf_owned.0.read().map_err(|_| {
IoError::other("the underlying substream is locked by a panicked process")
})?;
let chunks = self.substream.chunks.read().unwrap();
while out_count < data.len() {
let cur_chunk = chunks.get(self.chunk_pos);
if cur_chunk.is_none() {
//out of chunks to read
return Ok(out_count);
}
let cur_chunk = cur_chunk.expect("cur_chunk should never be None");
let chunk_len = cur_chunk.1 - cur_chunk.0;
let copy_count = min(data.len() - out_count, chunk_len - self.bytes_pos);
data[out_count..out_count + copy_count].copy_from_slice(
buf.get(cur_chunk.0 + self.bytes_pos..cur_chunk.0 + self.bytes_pos + copy_count)
.expect("Slice offsets are always valid"),
);
self.bytes_pos += copy_count;
out_count += copy_count;
if self.bytes_pos < chunk_len {
//`data` is full
break;
}
//`data` not full, move onto next chunk
self.chunk_pos += 1;
self.bytes_pos = 0;
}
Ok(out_count)
}
}
/// Iterator for substream chunks
pub struct SubstreamChunksIter {
substream: Substream,
next_buf: usize,
}
impl Iterator for SubstreamChunksIter {
type Item = Slice;
fn next(&mut self) -> Option<Slice> {
if let Some((start, end)) = self.substream.chunks.read().unwrap().get(self.next_buf) {
self.next_buf += 1;
return Some(Slice {
buf: self.substream.buf.clone(),
start: *start,
end: *end,
});
}
None
}
}
#[derive(Debug)]
pub struct SliceRef<'a> {
guard: RwLockReadGuard<'a, Vec<u8>>,
start: usize,
end: usize,
}
impl Deref for SliceRef<'_> {
type Target = [u8];
fn deref(&self) -> &Self::Target {
&self.guard[self.start..self.end]
}
}
impl PartialEq for SliceRef<'_> {
fn eq(&self, other: &SliceRef<'_>) -> bool {
std::ptr::eq(self.guard.as_ptr(), other.guard.as_ptr())
&& self.start == other.start
&& self.end == other.end
}
}
impl LowerHex for SliceRef<'_> {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "[")?;
for (index, byte) in self[..].iter().enumerate() {
if index > 0 {
write!(f, " ")?;
}
LowerHex::fmt(byte, f)?;
}
write!(f, "]")?;
Ok(())
}
}
impl UpperHex for SliceRef<'_> {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "[")?;
for (index, byte) in self[..].iter().enumerate() {
if index > 0 {
write!(f, " ")?;
}
UpperHex::fmt(byte, f)?;
}
write!(f, "]")?;
Ok(())
}
}
#[cfg(test)]
mod test {
use crate::buffer::{Buffer, Substream};
use std::io::Read;
#[test]
fn buf_slice() {
let buf = Buffer::from(vec![0, 1, 2, 3, 4, 5]);
let slice = buf.get(2..4).expect("valid slice");
assert_eq!(&*slice.data(), &[2, 3]);
}
#[test]
fn buf_slice_append() {
let mut buf = Buffer::from(vec![0, 1, 2, 3, 4, 5]);
let slice = buf.get(2..4).expect("valid slice");
assert_eq!(&*slice.data(), &[2, 3]);
buf.append(&mut vec![6, 7, 8, 9]);
assert_eq!(&*slice.data(), &[2, 3]);
}
#[test]
fn slice_read() {
let buf = Buffer::from(vec![
38, 26, 99, 1, 1, 1, 1, 38, 12, 14, 1, 1, 93, 86, 1, 88,
]);
let slice = buf.to_full_slice();
let mut cursor = slice.clone();
let refdata = slice.data();
let mut data = vec![0; 1];
for byte in &refdata[..] {
let result = cursor.read(&mut data);
assert_eq!(result.unwrap(), 1);
assert_eq!(data[0], *byte);
}
}
#[test]
fn slice_read_all() {
let buf = Buffer::from(vec![
38, 26, 99, 1, 1, 1, 1, 38, 12, 14, 1, 1, 93, 86, 1, 88,
]);
let slice = buf.to_full_slice();
let mut cursor = slice.clone();
let refdata = slice.data();
let mut data = vec![0; slice.len() + 32];
let result = cursor.read(&mut data);
assert_eq!(result.unwrap(), slice.len());
assert_eq!(&data[..slice.len()], &refdata[..]);
}
#[test]
fn substream_cursor_read_inside() {
let buf = Buffer::from(vec![
38, 26, 99, 1, 1, 1, 1, 38, 12, 14, 1, 1, 93, 86, 1, 88,
]);
let mut substream = Substream::new(buf.clone());
substream.append(buf.get(3..7).unwrap()).unwrap();
substream.append(buf.get(10..12).unwrap()).unwrap();
substream.append(buf.get(14..15).unwrap()).unwrap();
let mut cursor = substream.as_cursor();
let mut data = vec![0; 7];
let result = cursor.read(&mut data);
assert_eq!(result.unwrap(), 7);
assert_eq!(data, vec![1; 7]);
}
#[test]
fn substream_cursor_read_outside() {
let buf = Buffer::from(vec![
38, 26, 99, 1, 1, 1, 1, 38, 12, 14, 1, 1, 93, 86, 1, 88,
]);
let mut substream = Substream::new(buf.clone());
substream.append(buf.get(0..3).unwrap()).unwrap();
substream.append(buf.get(7..10).unwrap()).unwrap();
substream.append(buf.get(12..14).unwrap()).unwrap();
substream.append(buf.get(15..).unwrap()).unwrap();
let mut cursor = substream.as_cursor();
let mut data = vec![0; 7];
let result = cursor.read(&mut data);
assert_eq!(result.unwrap(), 7);
assert_eq!(data, vec![38, 26, 99, 38, 12, 14, 93]);
let mut data = vec![0; 2];
let result = cursor.read(&mut data);
assert_eq!(result.unwrap(), 2);
assert_eq!(data, vec![86, 88]);
let mut cursor = substream.as_cursor();
let mut data = vec![0; 8];
let result = cursor.read(&mut data);
assert_eq!(result.unwrap(), 8);
assert_eq!(data, vec![38, 26, 99, 38, 12, 14, 93, 86]);
let mut data = vec![0; 1];
let result = cursor.read(&mut data);
assert_eq!(result.unwrap(), 1);
assert_eq!(data, vec![88]);
let mut cursor = substream.as_cursor();
let mut data = vec![0; 9];
let result = cursor.read(&mut data);
assert_eq!(result.unwrap(), 9);
assert_eq!(data, vec![38, 26, 99, 38, 12, 14, 93, 86, 88]);
}
}