Skip to content

Commit 1c9e8c1

Browse files
committed
don't poll the reader again after eof while waiting for the writer to flush
1 parent f09fa42 commit 1c9e8c1

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/io/copy.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ where
144144
#[pin]
145145
writer: W,
146146
amt: u64,
147+
reader_eof: bool
147148
}
148149
}
149150

@@ -156,13 +157,20 @@ where
156157

157158
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
158159
let mut this = self.project();
160+
159161
loop {
160-
let buffer = futures_core::ready!(this.reader.as_mut().poll_fill_buf(cx))?;
161-
if buffer.is_empty() {
162+
if *this.reader_eof {
162163
futures_core::ready!(this.writer.as_mut().poll_flush(cx))?;
163164
return Poll::Ready(Ok(*this.amt));
164165
}
165166

167+
let buffer = futures_core::ready!(this.reader.as_mut().poll_fill_buf(cx))?;
168+
169+
if buffer.is_empty() {
170+
*this.reader_eof = true;
171+
continue;
172+
}
173+
166174
let i = futures_core::ready!(this.writer.as_mut().poll_write(cx, buffer))?;
167175
if i == 0 {
168176
return Poll::Ready(Err(io::ErrorKind::WriteZero.into()));
@@ -176,6 +184,7 @@ where
176184
let future = CopyFuture {
177185
reader: BufReader::new(reader),
178186
writer,
187+
reader_eof: false,
179188
amt: 0,
180189
};
181190
future.await.context(|| String::from("io::copy failed"))

0 commit comments

Comments
 (0)