Skip to content

Commit 66b887d

Browse files
committed
move the Read impl higher, it's the most important thing
1 parent 95385df commit 66b887d

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

src/chunked/encoder.rs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,33 @@ impl<R: Read + Unpin> ChunkedEncoder<R> {
2222
}
2323
}
2424

25+
impl<R: Read + Unpin> Read for ChunkedEncoder<R> {
26+
fn poll_read(
27+
mut self: Pin<&mut Self>,
28+
cx: &mut Context<'_>,
29+
buf: &mut [u8],
30+
) -> Poll<io::Result<usize>> {
31+
if self.done {
32+
return Poll::Ready(Ok(0));
33+
}
34+
let reader = &mut self.reader;
35+
36+
let max_bytes_to_read = max_bytes_to_read(buf.len());
37+
38+
let bytes = ready!(Pin::new(reader).poll_read(cx, &mut buf[..max_bytes_to_read]))?;
39+
if bytes == 0 {
40+
self.done = true;
41+
}
42+
let start = format!("{:X}\r\n", bytes);
43+
let start_length = start.as_bytes().len();
44+
let total = bytes + start_length + 2;
45+
buf.copy_within(..bytes, start_length);
46+
buf[..start_length].copy_from_slice(start.as_bytes());
47+
buf[total - 2..total].copy_from_slice(b"\r\n");
48+
Poll::Ready(Ok(total))
49+
}
50+
}
51+
2552
fn max_bytes_to_read(buf_len: usize) -> usize {
2653
if buf_len < 6 {
2754
// the minimum read size is of 6 represents one byte of
@@ -89,30 +116,3 @@ mod test_bytes_to_read {
89116
}
90117
}
91118
}
92-
93-
impl<R: Read + Unpin> Read for ChunkedEncoder<R> {
94-
fn poll_read(
95-
mut self: Pin<&mut Self>,
96-
cx: &mut Context<'_>,
97-
buf: &mut [u8],
98-
) -> Poll<io::Result<usize>> {
99-
if self.done {
100-
return Poll::Ready(Ok(0));
101-
}
102-
let reader = &mut self.reader;
103-
104-
let max_bytes_to_read = max_bytes_to_read(buf.len());
105-
106-
let bytes = ready!(Pin::new(reader).poll_read(cx, &mut buf[..max_bytes_to_read]))?;
107-
if bytes == 0 {
108-
self.done = true;
109-
}
110-
let start = format!("{:X}\r\n", bytes);
111-
let start_length = start.as_bytes().len();
112-
let total = bytes + start_length + 2;
113-
buf.copy_within(..bytes, start_length);
114-
buf[..start_length].copy_from_slice(start.as_bytes());
115-
buf[total - 2..total].copy_from_slice(b"\r\n");
116-
Poll::Ready(Ok(total))
117-
}
118-
}

0 commit comments

Comments
 (0)