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
use crate::{DrainChunks, IntoChunks};
use bytes::{Buf, BufMut, Bytes, BytesMut};
use std::collections::VecDeque;
use std::io::IoSlice;
use std::mem::MaybeUninit;
const DEFAULT_CHUNK_SIZE: usize = 4096;
#[derive(Debug)]
pub(crate) struct Inner {
staging: BytesMut,
chunks: VecDeque<Bytes>,
chunk_size: usize,
}
impl Default for Inner {
#[inline]
fn default() -> Self {
Inner {
staging: BytesMut::new(),
chunks: VecDeque::new(),
chunk_size: DEFAULT_CHUNK_SIZE,
}
}
}
pub(crate) enum AdvanceStopped {
InChunk,
InStaging(usize),
}
impl Inner {
#[inline]
pub fn with_chunk_size(chunk_size: usize) -> Self {
Inner {
chunk_size,
..Default::default()
}
}
#[inline]
pub fn with_profile(chunk_size: usize, chunking_capacity: usize) -> Self {
Inner {
staging: BytesMut::new(),
chunks: VecDeque::with_capacity(chunking_capacity),
chunk_size,
}
}
#[inline]
pub fn chunk_size(&self) -> usize {
self.chunk_size
}
#[inline]
pub fn is_empty(&self) -> bool {
self.chunks.is_empty() && self.staging.is_empty()
}
#[inline]
pub fn staging_len(&self) -> usize {
self.staging.len()
}
#[inline]
pub fn staging_capacity(&self) -> usize {
self.staging.capacity()
}
#[inline]
pub fn push_chunk(&mut self, chunk: Bytes) {
debug_assert!(!chunk.is_empty());
self.chunks.push_back(chunk)
}
#[inline]
pub fn flush(&mut self) {
if !self.staging.is_empty() {
let bytes = self.staging.split().freeze();
self.push_chunk(bytes)
}
}
#[inline]
pub fn drain_chunks(&mut self) -> DrainChunks<'_> {
DrainChunks::new(self.chunks.drain(..))
}
#[inline]
pub fn into_chunks(mut self) -> IntoChunks {
if !self.staging.is_empty() {
self.chunks.push_back(self.staging.freeze());
}
IntoChunks::new(self.chunks.into_iter())
}
pub fn reserve_staging(&mut self) -> usize {
let cap = self.staging.capacity();
let cutoff = cap.saturating_add(cap / 2);
let additional = if cutoff > self.chunk_size {
self.flush();
self.chunk_size
} else {
self.chunk_size - cap
};
self.staging.reserve(additional);
self.staging.capacity()
}
#[inline]
pub fn remaining_mut(&self) -> usize {
self.staging.remaining_mut()
}
#[inline]
pub unsafe fn advance_mut(&mut self, cnt: usize) {
self.staging.advance_mut(cnt);
}
#[inline]
pub fn bytes_mut(&mut self) -> &mut [MaybeUninit<u8>] {
self.staging.bytes_mut()
}
pub fn remaining(&self) -> usize {
self.chunks
.iter()
.fold(self.staging.len(), |sum, chunk| sum + chunk.len())
}
#[inline]
pub fn bytes(&self) -> &[u8] {
if let Some(chunk) = self.chunks.front() {
chunk
} else {
&self.staging
}
}
pub fn advance(&mut self, mut cnt: usize) -> AdvanceStopped {
loop {
match self.chunks.front_mut() {
None => {
self.staging.advance(cnt);
return AdvanceStopped::InStaging(cnt);
}
Some(chunk) => {
let len = chunk.len();
if cnt < len {
chunk.advance(cnt);
return AdvanceStopped::InChunk;
} else {
cnt -= len;
self.chunks.pop_front();
}
}
}
}
}
pub fn bytes_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize {
let n = {
let zipped = dst.iter_mut().zip(self.chunks.iter());
let len = zipped.len();
for (io_slice, chunk) in zipped {
*io_slice = IoSlice::new(chunk);
}
len
};
if n < dst.len() && !self.staging.is_empty() {
dst[n] = IoSlice::new(&self.staging);
n + 1
} else {
n
}
}
pub fn take_bytes(&mut self) -> Bytes {
match self.chunks.pop_front() {
None => self.staging.split().freeze(),
Some(chunk) => {
if self.is_empty() {
return chunk;
}
let cap = chunk.len() + self.remaining();
let mut buf = BytesMut::with_capacity(cap);
buf.put(chunk);
while let Some(chunk) = self.chunks.pop_front() {
buf.put(chunk);
}
buf.put(self.staging.split());
buf.freeze()
}
}
}
}