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
use {Buf, BufMut, Bytes, BytesMut};
use std::{cmp, fmt};
pub struct ByteBuf {
mem: BytesMut,
rd: usize,
}
impl ByteBuf {
pub fn new() -> ByteBuf {
ByteBuf::with_capacity(8 * 1024)
}
pub fn with_capacity(cap: usize) -> ByteBuf {
ByteBuf {
mem: BytesMut::with_capacity(cap),
rd: 0,
}
}
pub fn from_bytes(bytes: BytesMut) -> ByteBuf {
ByteBuf {
mem: bytes,
rd: 0,
}
}
pub fn from_slice<T: AsRef<[u8]>>(bytes: T) -> ByteBuf {
let mut buf = ByteBuf::with_capacity(bytes.as_ref().len());
buf.copy_from_slice(bytes.as_ref());
buf
}
pub fn capacity(&self) -> usize {
self.mem.capacity()
}
pub fn position(&self) -> usize {
self.rd
}
pub fn set_position(&mut self, position: usize) {
assert!(position <= self.mem.len(), "position out of bounds");
self.rd = position
}
pub fn len(&self) -> usize {
self.mem.len()
}
pub fn is_empty(&self) -> bool {
self.mem.is_empty()
}
pub fn clear(&mut self) {
self.rd = 0;
unsafe { self.mem.set_len(0); }
}
pub fn drain_read(&mut self) -> BytesMut {
let drained = self.mem.drain_to(self.rd);
self.rd = 0;
drained
}
pub fn drain_to(&mut self, at: usize) -> BytesMut {
let drained = self.mem.drain_to(at);
if at >= self.rd {
self.rd = 0;
} else {
self.rd -= at;
}
drained
}
pub fn reserve(&mut self, additional: usize) {
if self.remaining_mut() < additional {
let cap = cmp::max(self.capacity() * 2, self.len() + additional);
let cap = cap.next_power_of_two();
let mut new = ByteBuf::with_capacity(cap);
new.copy_from_slice(self.mem.as_ref());
new.rd = self.rd;
*self = new;
}
}
pub fn reserve_exact(&mut self, additional: usize) {
if self.remaining_mut() < additional {
let cap = self.len() + additional;
let mut new = ByteBuf::with_capacity(cap);
new.copy_from_slice(self.mem.as_ref());
new.rd = self.rd;
*self = new;
}
}
pub fn get_ref(&self) -> &BytesMut {
&self.mem
}
pub fn into_inner(self) -> BytesMut {
self.mem
}
}
impl Buf for ByteBuf {
fn remaining(&self) -> usize {
self.len() - self.rd
}
fn bytes(&self) -> &[u8] {
&self.mem[self.rd..]
}
fn advance(&mut self, cnt: usize) {
assert!(cnt <= self.remaining(), "buffer overflow");
self.rd += cnt;
}
fn copy_to_slice(&mut self, dst: &mut [u8]) {
assert!(self.remaining() >= dst.len());
let len = dst.len();
dst.copy_from_slice(&self.bytes()[..len]);
self.rd += len;
}
}
impl BufMut for ByteBuf {
fn remaining_mut(&self) -> usize {
self.capacity() - self.len()
}
unsafe fn advance_mut(&mut self, cnt: usize) {
let new_len = self.len() + cnt;
self.mem.set_len(new_len);
}
unsafe fn bytes_mut(&mut self) -> &mut [u8] {
let len = self.len();
&mut self.mem.as_raw()[len..]
}
fn copy_from_slice(&mut self, src: &[u8]) {
assert!(self.remaining_mut() >= src.len());
let len = src.len();
unsafe {
self.bytes_mut()[..len].copy_from_slice(src);
self.advance_mut(len);
}
}
}
impl From<ByteBuf> for Bytes {
fn from(src: ByteBuf) -> Bytes {
let bytes = BytesMut::from(src);
bytes.freeze()
}
}
impl From<ByteBuf> for BytesMut {
fn from(src: ByteBuf) -> BytesMut {
src.mem
}
}
impl fmt::Debug for ByteBuf {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
self.bytes().fmt(fmt)
}
}
impl fmt::Write for ByteBuf {
fn write_str(&mut self, s: &str) -> fmt::Result {
BufMut::put_str(self, s);
Ok(())
}
fn write_fmt(&mut self, args: fmt::Arguments) -> fmt::Result {
fmt::write(self, args)
}
}