@@ -7,6 +7,12 @@ use crate::io::{self, BufRead, BufReader, Read, Write};
77use crate :: task:: { Context , Poll } ;
88use crate :: utils:: Context as _;
99
10+ // Note: There are two otherwise-identical implementations of this
11+ // function because unstable has removed the `?Sized` bound for the
12+ // reader and writer and accepts `R` and `W` instead of `&mut R` and
13+ // `&mut W`. If making a change to either of the implementations,
14+ // ensure that you copy it into the other.
15+
1016/// Copies the entire contents of a reader into a writer.
1117///
1218/// This function will continuously read data from `reader` and then
5763 #[ pin]
5864 writer: W ,
5965 amt: u64 ,
66+ reader_eof: bool
6067 }
6168 }
6269
@@ -69,13 +76,20 @@ where
6976
7077 fn poll ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
7178 let mut this = self . project ( ) ;
79+
7280 loop {
73- let buffer = futures_core:: ready!( this. reader. as_mut( ) . poll_fill_buf( cx) ) ?;
74- if buffer. is_empty ( ) {
81+ if * this. reader_eof {
7582 futures_core:: ready!( this. writer. as_mut( ) . poll_flush( cx) ) ?;
7683 return Poll :: Ready ( Ok ( * this. amt ) ) ;
7784 }
7885
86+ let buffer = futures_core:: ready!( this. reader. as_mut( ) . poll_fill_buf( cx) ) ?;
87+
88+ if buffer. is_empty ( ) {
89+ * this. reader_eof = true ;
90+ continue ;
91+ }
92+
7993 let i = futures_core:: ready!( this. writer. as_mut( ) . poll_write( cx, buffer) ) ?;
8094 if i == 0 {
8195 return Poll :: Ready ( Err ( io:: ErrorKind :: WriteZero . into ( ) ) ) ;
89103 let future = CopyFuture {
90104 reader : BufReader :: new ( reader) ,
91105 writer,
106+ reader_eof : false ,
92107 amt : 0 ,
93108 } ;
94109 future. await . context ( || String :: from ( "io::copy failed" ) )
@@ -144,6 +159,7 @@ where
144159 #[ pin]
145160 writer: W ,
146161 amt: u64 ,
162+ reader_eof: bool
147163 }
148164 }
149165
@@ -156,13 +172,20 @@ where
156172
157173 fn poll ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
158174 let mut this = self . project ( ) ;
175+
159176 loop {
160- let buffer = futures_core:: ready!( this. reader. as_mut( ) . poll_fill_buf( cx) ) ?;
161- if buffer. is_empty ( ) {
177+ if * this. reader_eof {
162178 futures_core:: ready!( this. writer. as_mut( ) . poll_flush( cx) ) ?;
163179 return Poll :: Ready ( Ok ( * this. amt ) ) ;
164180 }
165181
182+ let buffer = futures_core:: ready!( this. reader. as_mut( ) . poll_fill_buf( cx) ) ?;
183+
184+ if buffer. is_empty ( ) {
185+ * this. reader_eof = true ;
186+ continue ;
187+ }
188+
166189 let i = futures_core:: ready!( this. writer. as_mut( ) . poll_write( cx, buffer) ) ?;
167190 if i == 0 {
168191 return Poll :: Ready ( Err ( io:: ErrorKind :: WriteZero . into ( ) ) ) ;
@@ -176,6 +199,7 @@ where
176199 let future = CopyFuture {
177200 reader : BufReader :: new ( reader) ,
178201 writer,
202+ reader_eof : false ,
179203 amt : 0 ,
180204 } ;
181205 future. await . context ( || String :: from ( "io::copy failed" ) )
0 commit comments