@@ -7,6 +7,12 @@ use crate::io::{self, BufRead, BufReader, Read, Write};
7
7
use crate :: task:: { Context , Poll } ;
8
8
use crate :: utils:: Context as _;
9
9
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
+
10
16
/// Copies the entire contents of a reader into a writer.
11
17
///
12
18
/// This function will continuously read data from `reader` and then
57
63
#[ pin]
58
64
writer: W ,
59
65
amt: u64 ,
66
+ reader_eof: bool
60
67
}
61
68
}
62
69
@@ -69,13 +76,20 @@ where
69
76
70
77
fn poll ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
71
78
let mut this = self . project ( ) ;
79
+
72
80
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 {
75
82
futures_core:: ready!( this. writer. as_mut( ) . poll_flush( cx) ) ?;
76
83
return Poll :: Ready ( Ok ( * this. amt ) ) ;
77
84
}
78
85
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
+
79
93
let i = futures_core:: ready!( this. writer. as_mut( ) . poll_write( cx, buffer) ) ?;
80
94
if i == 0 {
81
95
return Poll :: Ready ( Err ( io:: ErrorKind :: WriteZero . into ( ) ) ) ;
89
103
let future = CopyFuture {
90
104
reader : BufReader :: new ( reader) ,
91
105
writer,
106
+ reader_eof : false ,
92
107
amt : 0 ,
93
108
} ;
94
109
future. await . context ( || String :: from ( "io::copy failed" ) )
@@ -144,6 +159,7 @@ where
144
159
#[ pin]
145
160
writer: W ,
146
161
amt: u64 ,
162
+ reader_eof: bool
147
163
}
148
164
}
149
165
@@ -156,13 +172,20 @@ where
156
172
157
173
fn poll ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
158
174
let mut this = self . project ( ) ;
175
+
159
176
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 {
162
178
futures_core:: ready!( this. writer. as_mut( ) . poll_flush( cx) ) ?;
163
179
return Poll :: Ready ( Ok ( * this. amt ) ) ;
164
180
}
165
181
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
+
166
189
let i = futures_core:: ready!( this. writer. as_mut( ) . poll_write( cx, buffer) ) ?;
167
190
if i == 0 {
168
191
return Poll :: Ready ( Err ( io:: ErrorKind :: WriteZero . into ( ) ) ) ;
@@ -176,6 +199,7 @@ where
176
199
let future = CopyFuture {
177
200
reader : BufReader :: new ( reader) ,
178
201
writer,
202
+ reader_eof : false ,
179
203
amt : 0 ,
180
204
} ;
181
205
future. await . context ( || String :: from ( "io::copy failed" ) )
0 commit comments