File tree 2 files changed +21
-1
lines changed
2 files changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -587,6 +587,7 @@ impl AsyncRead for File {
587
587
dst : & mut ReadBuf < ' _ > ,
588
588
) -> Poll < io:: Result < ( ) > > {
589
589
ready ! ( crate :: trace:: trace_leaf( cx) ) ;
590
+
590
591
let me = self . get_mut ( ) ;
591
592
let inner = me. inner . get_mut ( ) ;
592
593
@@ -595,7 +596,7 @@ impl AsyncRead for File {
595
596
State :: Idle ( ref mut buf_cell) => {
596
597
let mut buf = buf_cell. take ( ) . unwrap ( ) ;
597
598
598
- if !buf. is_empty ( ) {
599
+ if !buf. is_empty ( ) || dst . remaining ( ) == 0 {
599
600
buf. copy_to ( dst) ;
600
601
* buf_cell = Some ( buf) ;
601
602
return Poll :: Ready ( Ok ( ( ) ) ) ;
Original file line number Diff line number Diff line change 1
1
#![ warn( rust_2018_idioms) ]
2
2
#![ cfg( all( feature = "full" , not( target_os = "wasi" ) ) ) ] // WASI does not support all fs operations
3
3
4
+ use futures:: future:: FutureExt ;
4
5
use std:: io:: prelude:: * ;
5
6
use std:: io:: IoSlice ;
6
7
use tempfile:: NamedTempFile ;
@@ -176,6 +177,24 @@ async fn read_file_from_std() {
176
177
assert_eq ! ( & buf[ ..n] , HELLO ) ;
177
178
}
178
179
180
+ #[ tokio:: test]
181
+ async fn empty_read ( ) {
182
+ let mut tempfile = tempfile ( ) ;
183
+ tempfile. write_all ( HELLO ) . unwrap ( ) ;
184
+
185
+ let mut file = File :: open ( tempfile. path ( ) ) . await . unwrap ( ) ;
186
+
187
+ // Perform an empty read and get a length of zero.
188
+ assert ! ( matches!( file. read( & mut [ ] ) . now_or_never( ) , Some ( Ok ( 0 ) ) ) ) ;
189
+
190
+ // Check that we don't get EOF on the next read.
191
+ let mut buf = [ 0 ; 1024 ] ;
192
+ let n = file. read ( & mut buf) . await . unwrap ( ) ;
193
+
194
+ assert_eq ! ( n, HELLO . len( ) ) ;
195
+ assert_eq ! ( & buf[ ..n] , HELLO ) ;
196
+ }
197
+
179
198
fn tempfile ( ) -> NamedTempFile {
180
199
NamedTempFile :: new ( ) . unwrap ( )
181
200
}
You can’t perform that action at this time.
0 commit comments