Skip to content

Commit e5f5500

Browse files
committed
s/bytes_read/bytes_written/
1 parent a39b30f commit e5f5500

File tree

1 file changed

+37
-36
lines changed

1 file changed

+37
-36
lines changed

src/server/encode.rs

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ pub(crate) struct Encoder {
2020
/// The state of the encoding process
2121
state: State,
2222
/// Track bytes read in a call to poll_read.
23-
bytes_read: usize,
23+
bytes_written: usize,
2424
/// The data we're writing as part of the head section.
2525
head: Vec<u8>,
2626
/// The amount of bytes read from the head section.
27-
head_bytes_read: usize,
27+
head_bytes_written: usize,
2828
/// The total length of the body.
2929
/// This is only used in the known-length body encoder.
3030
body_len: usize,
3131
/// The amount of bytes read from the body.
3232
/// This is only used in the known-length body encoder.
33-
body_bytes_read: usize,
33+
body_bytes_written: usize,
3434
/// An encoder for chunked encoding.
3535
chunked: ChunkedEncoder,
3636
}
@@ -51,11 +51,11 @@ impl Encoder {
5151
Self {
5252
res,
5353
state: State::Init,
54-
bytes_read: 0,
54+
bytes_written: 0,
5555
head: vec![],
56-
head_bytes_read: 0,
56+
head_bytes_written: 0,
5757
body_len: 0,
58-
body_bytes_read: 0,
58+
body_bytes_written: 0,
5959
chunked: ChunkedEncoder::new(),
6060
}
6161
}
@@ -65,16 +65,16 @@ impl Encoder {
6565
cx: &mut Context<'_>,
6666
buf: &mut [u8],
6767
) -> Poll<io::Result<usize>> {
68-
self.bytes_read = 0;
68+
self.bytes_written = 0;
6969
let res = match self.state {
7070
State::Init => self.init(cx, buf),
7171
State::ComputeHead => self.compute_head(cx, buf),
7272
State::EncodeHead => self.encode_head(cx, buf),
7373
State::FixedBody => self.encode_fixed_body(cx, buf),
7474
State::ChunkedBody => self.encode_chunked_body(cx, buf),
75-
State::End => Poll::Ready(Ok(self.bytes_read)),
75+
State::End => Poll::Ready(Ok(self.bytes_written)),
7676
};
77-
log::trace!("ServerEncoder {} bytes written", self.bytes_read);
77+
log::trace!("ServerEncoder {} bytes written", self.bytes_written);
7878
res
7979
}
8080

@@ -144,15 +144,15 @@ impl Encoder {
144144
fn encode_head(&mut self, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<io::Result<usize>> {
145145
// Read from the serialized headers, url and methods.
146146
let head_len = self.head.len();
147-
let len = std::cmp::min(head_len - self.head_bytes_read, buf.len());
148-
let range = self.head_bytes_read..self.head_bytes_read + len;
147+
let len = std::cmp::min(head_len - self.head_bytes_written, buf.len());
148+
let range = self.head_bytes_written..self.head_bytes_written + len;
149149
buf[0..len].copy_from_slice(&self.head[range]);
150-
self.bytes_read += len;
151-
self.head_bytes_read += len;
150+
self.bytes_written += len;
151+
self.head_bytes_written += len;
152152

153153
// If we've read the total length of the head we're done
154154
// reading the head and can transition to reading the body
155-
if self.head_bytes_read == head_len {
155+
if self.head_bytes_written == head_len {
156156
// The response length lets us know if we are encoding
157157
// our body in chunks or not
158158
match self.res.len() {
@@ -171,7 +171,7 @@ impl Encoder {
171171
} else {
172172
// If we haven't read the entire header it means `buf` isn't
173173
// big enough. Break out of loop and return from `poll_read`
174-
return Poll::Ready(Ok(self.bytes_read));
174+
return Poll::Ready(Ok(self.bytes_written));
175175
}
176176
}
177177

@@ -183,47 +183,48 @@ impl Encoder {
183183
) -> Poll<io::Result<usize>> {
184184
// Double check that we didn't somehow read more bytes than
185185
// can fit in our buffer
186-
debug_assert!(self.bytes_read <= buf.len());
186+
debug_assert!(self.bytes_written <= buf.len());
187187

188188
// ensure we have at least room for 1 more byte in our buffer
189-
if self.bytes_read == buf.len() {
190-
return Poll::Ready(Ok(self.bytes_read));
189+
if self.bytes_written == buf.len() {
190+
return Poll::Ready(Ok(self.bytes_written));
191191
}
192192

193193
// Figure out how many bytes we can read.
194-
let upper_bound = (self.bytes_read + self.body_len - self.body_bytes_read).min(buf.len());
194+
let upper_bound =
195+
(self.bytes_written + self.body_len - self.body_bytes_written).min(buf.len());
195196
// Read bytes from body
196-
let range = self.bytes_read..upper_bound;
197+
let range = self.bytes_written..upper_bound;
197198
let inner_poll_result = Pin::new(&mut self.res).poll_read(cx, &mut buf[range]);
198-
let new_body_bytes_read = match inner_poll_result {
199+
let new_body_bytes_written = match inner_poll_result {
199200
Poll::Ready(Ok(n)) => n,
200201
Poll::Ready(Err(e)) => return Poll::Ready(Err(e)),
201-
Poll::Pending => match self.bytes_read {
202+
Poll::Pending => match self.bytes_written {
202203
0 => return Poll::Pending,
203204
n => return Poll::Ready(Ok(n)),
204205
},
205206
};
206-
self.body_bytes_read += new_body_bytes_read;
207-
self.bytes_read += new_body_bytes_read;
207+
self.body_bytes_written += new_body_bytes_written;
208+
self.bytes_written += new_body_bytes_written;
208209

209210
// Double check we did not read more body bytes than the total
210211
// length of the body
211212
debug_assert!(
212-
self.body_bytes_read <= self.body_len,
213+
self.body_bytes_written <= self.body_len,
213214
"Too many bytes read. Expected: {}, read: {}",
214215
self.body_len,
215-
self.body_bytes_read
216+
self.body_bytes_written
216217
);
217218

218-
if self.body_len == self.body_bytes_read {
219+
if self.body_len == self.body_bytes_written {
219220
// If we've read the `len` number of bytes, end
220221
self.set_state(State::End);
221-
return Poll::Ready(Ok(self.bytes_read));
222-
} else if new_body_bytes_read == 0 {
222+
return Poll::Ready(Ok(self.bytes_written));
223+
} else if new_body_bytes_written == 0 {
223224
// If we've reached unexpected EOF, end anyway
224225
// TODO: do something?
225226
self.set_state(State::End);
226-
return Poll::Ready(Ok(self.bytes_read));
227+
return Poll::Ready(Ok(self.bytes_written));
227228
} else {
228229
self.encode_fixed_body(cx, buf)
229230
}
@@ -236,19 +237,19 @@ impl Encoder {
236237
cx: &mut Context<'_>,
237238
buf: &mut [u8],
238239
) -> Poll<io::Result<usize>> {
239-
let buf = &mut buf[self.bytes_read..];
240+
let buf = &mut buf[self.bytes_written..];
240241
match self.chunked.encode(&mut self.res, cx, buf) {
241242
Poll::Ready(Ok(read)) => {
242-
self.bytes_read += read;
243-
if self.bytes_read == 0 {
243+
self.bytes_written += read;
244+
if self.bytes_written == 0 {
244245
self.set_state(State::End);
245246
}
246-
Poll::Ready(Ok(self.bytes_read))
247+
Poll::Ready(Ok(self.bytes_written))
247248
}
248249
Poll::Ready(Err(err)) => Poll::Ready(Err(err)),
249250
Poll::Pending => {
250-
if self.bytes_read > 0 {
251-
return Poll::Ready(Ok(self.bytes_read));
251+
if self.bytes_written > 0 {
252+
return Poll::Ready(Ok(self.bytes_written));
252253
}
253254
Poll::Pending
254255
}

0 commit comments

Comments
 (0)