@@ -148,6 +148,9 @@ pub trait Reader {
148
148
/**
149
149
* Returns a boolean value: are we currently at EOF?
150
150
*
151
+ * Note that stream position may be already at the end-of-file point,
152
+ * but `eof` returns false until an attempt to read at that position.
153
+ *
151
154
* `eof` is conceptually similar to C's `feof` function.
152
155
*
153
156
* # Examples
@@ -724,15 +727,21 @@ impl<T:Reader> ReaderUtil for T {
724
727
}
725
728
726
729
fn each_byte ( & self , it : & fn ( int ) -> bool ) -> bool {
727
- while !self . eof ( ) {
728
- if !it ( self . read_byte ( ) ) { return false ; }
730
+ loop {
731
+ match self . read_byte ( ) {
732
+ -1 => break ,
733
+ ch => if !it ( ch) { return false ; }
734
+ }
729
735
}
730
736
return true ;
731
737
}
732
738
733
739
fn each_char ( & self , it : & fn ( char ) -> bool ) -> bool {
734
- while !self . eof ( ) {
735
- if !it ( self . read_char ( ) ) { return false ; }
740
+ loop {
741
+ match self . read_char ( ) {
742
+ eof if eof == ( -1 as char ) => break ,
743
+ ch => if !it ( ch) { return false ; }
744
+ }
736
745
}
737
746
return true ;
738
747
}
@@ -1858,6 +1867,31 @@ mod tests {
1858
1867
assert_eq ! ( frood, frood2) ;
1859
1868
}
1860
1869
1870
+ #[ test]
1871
+ fn test_each_byte_each_char_file ( ) {
1872
+ // Issue #5056 -- shouldn't include trailing EOF.
1873
+ let path = Path ( "tmp/lib-io-test-each-byte-each-char-file.tmp" ) ;
1874
+
1875
+ {
1876
+ // create empty, enough to reproduce a problem
1877
+ io:: file_writer ( & path, [ io:: Create ] ) . unwrap ( ) ;
1878
+ }
1879
+
1880
+ {
1881
+ let file = io:: file_reader ( & path) . unwrap ( ) ;
1882
+ for file. each_byte( ) |_| {
1883
+ fail ! ( "must be empty" ) ;
1884
+ }
1885
+ }
1886
+
1887
+ {
1888
+ let file = io:: file_reader ( & path) . unwrap ( ) ;
1889
+ for file. each_char( ) |_| {
1890
+ fail ! ( "must be empty" ) ;
1891
+ }
1892
+ }
1893
+ }
1894
+
1861
1895
#[ test]
1862
1896
fn test_readchars_empty ( ) {
1863
1897
do io:: with_str_reader ( "" ) |inp| {
0 commit comments