Skip to content

Commit 96032e5

Browse files
committed
remove scope state from server encoder
This is a necessary step to enable splitting the main match statement into independent functions.
1 parent 68dccc6 commit 96032e5

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

src/server/encode.rs

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ pub(crate) struct Encoder {
2121
res: Response,
2222
/// The state of the encoding process
2323
state: EncoderState,
24+
/// Track bytes read in a call to poll_read.
25+
bytes_read: usize,
2426
}
2527

2628
#[derive(Debug)]
@@ -48,6 +50,7 @@ impl Encoder {
4850
Self {
4951
res,
5052
state: EncoderState::Start,
53+
bytes_read: 0,
5154
}
5255
}
5356

@@ -92,7 +95,7 @@ impl Read for Encoder {
9295
) -> Poll<io::Result<usize>> {
9396
// we must keep track how many bytes of the head and body we've read
9497
// in this call of `poll_read`
95-
let mut bytes_read = 0;
98+
self.bytes_read = 0;
9699
loop {
97100
match self.state {
98101
EncoderState::Start => {
@@ -112,7 +115,7 @@ impl Read for Encoder {
112115
let len = std::cmp::min(head_len - head_bytes_read, buf.len());
113116
let range = head_bytes_read..head_bytes_read + len;
114117
buf[0..len].copy_from_slice(&data[range]);
115-
bytes_read += len;
118+
self.bytes_read += len;
116119
head_bytes_read += len;
117120

118121
// If we've read the total length of the head we're done
@@ -139,31 +142,31 @@ impl Read for Encoder {
139142
} => {
140143
// Double check that we didn't somehow read more bytes than
141144
// can fit in our buffer
142-
debug_assert!(bytes_read <= buf.len());
145+
debug_assert!(self.bytes_read <= buf.len());
143146

144147
// ensure we have at least room for 1 more byte in our buffer
145-
if bytes_read == buf.len() {
148+
if self.bytes_read == buf.len() {
146149
break;
147150
}
148151

149152
// Figure out how many bytes we can read.
150-
let upper_bound = (bytes_read + body_len - body_bytes_read).min(buf.len());
153+
let upper_bound = (self.bytes_read + body_len - body_bytes_read).min(buf.len());
151154
// Read bytes from body
152-
let inner_poll_result =
153-
Pin::new(&mut self.res).poll_read(cx, &mut buf[bytes_read..upper_bound]);
155+
let range = self.bytes_read..upper_bound;
156+
let inner_poll_result = Pin::new(&mut self.res).poll_read(cx, &mut buf[range]);
154157
let new_body_bytes_read = match inner_poll_result {
155158
Poll::Ready(Ok(n)) => n,
156159
Poll::Ready(Err(e)) => return Poll::Ready(Err(e)),
157160
Poll::Pending => {
158-
if bytes_read == 0 {
161+
if self.bytes_read == 0 {
159162
return Poll::Pending;
160163
} else {
161164
break;
162165
}
163166
}
164167
};
165168
body_bytes_read += new_body_bytes_read;
166-
bytes_read += new_body_bytes_read;
169+
self.bytes_read += new_body_bytes_read;
167170

168171
// Double check we did not read more body bytes than the total
169172
// length of the body
@@ -192,7 +195,7 @@ impl Read for Encoder {
192195
EncoderState::UncomputedChunked => {
193196
// We can read a maximum of the buffer's total size
194197
// minus what we've already filled the buffer with
195-
let buffer_remaining = buf.len() - bytes_read;
198+
let buffer_remaining = buf.len() - self.bytes_read;
196199

197200
// ensure we have at least room for 1 byte in our buffer
198201
if buffer_remaining == 0 {
@@ -208,7 +211,7 @@ impl Read for Encoder {
208211
Poll::Ready(Ok(n)) => n,
209212
Poll::Ready(Err(e)) => return Poll::Ready(Err(e)),
210213
Poll::Pending => {
211-
if bytes_read == 0 {
214+
if self.bytes_read == 0 {
212215
return Poll::Pending;
213216
} else {
214217
break;
@@ -224,7 +227,7 @@ impl Read for Encoder {
224227

225228
// calculate the total size of the chunk including serialized
226229
// length and the CRLF padding
227-
let total_chunk_size = bytes_read
230+
let total_chunk_size = self.bytes_read
228231
+ chunk_length_bytes_len
229232
+ CRLF_LENGTH
230233
+ chunk_length
@@ -233,24 +236,24 @@ impl Read for Encoder {
233236
// See if we can write the chunk out in one go
234237
if total_chunk_size < buffer_remaining {
235238
// Write the chunk length into the buffer
236-
buf[bytes_read..bytes_read + chunk_length_bytes_len]
239+
buf[self.bytes_read..(self.bytes_read + chunk_length_bytes_len)]
237240
.copy_from_slice(chunk_length_bytes);
238-
bytes_read += chunk_length_bytes_len;
241+
self.bytes_read += chunk_length_bytes_len;
239242

240243
// follow chunk length with CRLF
241-
buf[bytes_read] = CR;
242-
buf[bytes_read + 1] = LF;
243-
bytes_read += 2;
244+
buf[self.bytes_read] = CR;
245+
buf[self.bytes_read + 1] = LF;
246+
self.bytes_read += 2;
244247

245248
// copy chunk into buf
246-
buf[bytes_read..bytes_read + chunk_length]
249+
buf[self.bytes_read..(self.bytes_read + chunk_length)]
247250
.copy_from_slice(&chunk_buf[..chunk_length]);
248-
bytes_read += chunk_length;
251+
self.bytes_read += chunk_length;
249252

250253
// follow chunk with CRLF
251-
buf[bytes_read] = CR;
252-
buf[bytes_read + 1] = LF;
253-
bytes_read += 2;
254+
buf[self.bytes_read] = CR;
255+
buf[self.bytes_read + 1] = LF;
256+
self.bytes_read += 2;
254257

255258
if chunk_length == 0 {
256259
self.state = EncoderState::Done;
@@ -276,7 +279,7 @@ impl Read for Encoder {
276279
// follow chunk with CRLF
277280
chunk[bytes_written] = CR;
278281
chunk[bytes_written + 1] = LF;
279-
bytes_read += 2;
282+
self.bytes_read += 2;
280283
self.state = EncoderState::ComputedChunked {
281284
chunk: io::Cursor::new(chunk),
282285
is_last: chunk_length == 0,
@@ -288,18 +291,18 @@ impl Read for Encoder {
288291
is_last,
289292
} => {
290293
let inner_poll_result = Pin::new(chunk).poll_read(cx, &mut buf);
291-
bytes_read += match inner_poll_result {
294+
self.bytes_read += match inner_poll_result {
292295
Poll::Ready(Ok(n)) => n,
293296
Poll::Ready(Err(e)) => return Poll::Ready(Err(e)),
294297
Poll::Pending => {
295-
if bytes_read == 0 {
298+
if self.bytes_read == 0 {
296299
return Poll::Pending;
297300
} else {
298301
break;
299302
}
300303
}
301304
};
302-
if bytes_read == 0 {
305+
if self.bytes_read == 0 {
303306
self.state = match is_last {
304307
true => EncoderState::Done,
305308
false => EncoderState::UncomputedChunked,
@@ -311,6 +314,6 @@ impl Read for Encoder {
311314
}
312315
}
313316

314-
Poll::Ready(Ok(bytes_read as usize))
317+
Poll::Ready(Ok(self.bytes_read as usize))
315318
}
316319
}

0 commit comments

Comments
 (0)