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
#![warn(missing_docs)]
#[macro_use]
extern crate mac;
use std::slice;
pub use impls::arrayvec::ArrayVecBuffer;
pub use impls::buffer_ref::BufferRefBuffer;
pub use impls::cap_at::CapAt;
pub use impls::cap_at::CapAtBuffer;
pub use impls::slice::SliceBuffer;
pub use impls::slice_ref::SliceRefBuffer;
pub use impls::vec::VecBuffer;
pub use traits::ReadBuffer;
pub use traits::ReadBufferMarker;
pub use traits::ReadBufferRef;
pub use traits::read_buffer_ref;
mod impls;
mod traits;
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct CapacityError;
unsafe fn wildly_unsafe<'a, 'b>(slice: &'a mut [u8]) -> &'b mut [u8] {
slice::from_raw_parts_mut(slice.as_mut_ptr(), slice.len())
}
pub struct BufferRef<'data, 'size> {
buffer: &'data mut [u8],
initialized_: &'size mut usize,
}
impl<'d, 's> BufferRef<'d, 's> {
pub fn new(buffer: &'d mut [u8], initialized: &'s mut usize) -> BufferRef<'d, 's> {
debug_assert!(*initialized == 0);
BufferRef {
buffer: buffer,
initialized_: initialized,
}
}
pub unsafe fn advance(&mut self, num_bytes: usize) {
assert!(*self.initialized_ + num_bytes <= self.buffer.len());
*self.initialized_ += num_bytes;
}
pub fn extend<I>(&mut self, bytes: I) -> Result<(), CapacityError>
where I: Iterator<Item=u8>
{
let mut buf_iter = (&mut self.buffer[*self.initialized_..]).into_iter();
for b in bytes {
*unwrap_or_return!(buf_iter.next(), Err(CapacityError)) = b;
*self.initialized_ += 1;
}
Ok(())
}
pub fn write(&mut self, bytes: &[u8]) -> Result<(), CapacityError> {
self.extend(bytes.iter().cloned())
}
pub unsafe fn uninitialized_mut(&mut self) -> &mut [u8] {
&mut self.buffer[*self.initialized_..]
}
pub fn initialized(self) -> &'d [u8] {
&self.buffer[..*self.initialized_]
}
pub fn remaining(&self) -> usize {
self.buffer.len() - *self.initialized_
}
fn cap_at(self, index: usize) -> BufferRef<'d, 's> {
assert!(*self.initialized_ == 0);
BufferRef {
buffer: &mut self.buffer[..index],
initialized_: self.initialized_,
}
}
}
pub fn with_buffer<'a, T: Buffer<'a>, F, R>(buffer: T, f: F) -> R
where F: for<'b> FnOnce(BufferRef<'a, 'b>) -> R
{
let mut intermediate = buffer.to_to_buffer_ref();
f(intermediate.to_buffer_ref())
}
pub trait Buffer<'data> {
type Intermediate: ToBufferRef<'data>;
fn to_to_buffer_ref(self) -> Self::Intermediate;
fn cap_at(self, len: usize) -> CapAt<'data, Self> where Self: Sized {
self.cap_at_impl(len)
}
}
pub trait ToBufferRef<'data> {
fn to_buffer_ref<'size>(&'size mut self) -> BufferRef<'data, 'size>;
}
trait CapAtImpl<'data>: Buffer<'data> {
fn cap_at_impl(self, len: usize) -> CapAt<'data, Self> where Self: Sized;
}