14
14
// XXX: Iteration should probably be considered separately
15
15
16
16
use vec;
17
- use rt:: io:: Reader ;
17
+ use rt:: io:: { Reader , read_error , standard_error , EndOfFile } ;
18
18
use option:: { Option , Some , None } ;
19
19
use unstable:: finally:: Finally ;
20
20
use util;
@@ -36,16 +36,19 @@ pub trait ReaderUtil {
36
36
///
37
37
/// # Failure
38
38
///
39
- /// Raises the same conditions as `read`. Returns `false` if
40
- /// the condition is handled.
41
- fn push_bytes ( & mut self , buf : & mut ~[ u8 ] , len : uint ) -> bool ;
39
+ /// Raises the same conditions as `read`. Additionally raises `read_error`
40
+ /// on EOF. If `read_error` is handled then `push_bytes` returns without
41
+ /// pushing any bytes onto `buf` - that is, `buf` has the same length
42
+ /// upon exit as it did on entry.
43
+ fn push_bytes ( & mut self , buf : & mut ~[ u8 ] , len : uint ) ;
42
44
43
- /// Reads `len` bytes and gives you back a new vector
45
+ /// Reads `len` bytes and gives you back a new vector of length `len`
44
46
///
45
47
/// # Failure
46
48
///
47
- /// Raises the same conditions as the `read` method. May return
48
- /// less than the requested number of bytes on error or EOF.
49
+ /// Raises the same conditions as `read`. Additionally raises `read_error`
50
+ /// on EOF. If `read_error` is handled then the returned vector has
51
+ /// length 0.
49
52
fn read_bytes ( & mut self , len : uint ) -> ~[ u8 ] ;
50
53
51
54
/// Reads all remaining bytes from the stream.
@@ -71,11 +74,10 @@ impl<T: Reader> ReaderUtil for T {
71
74
}
72
75
}
73
76
74
- fn push_bytes ( & mut self , buf : & mut ~[ u8 ] , len : uint ) -> bool {
77
+ fn push_bytes ( & mut self , buf : & mut ~[ u8 ] , len : uint ) {
75
78
unsafe {
76
79
let start_len = buf. len ( ) ;
77
80
let mut total_read = 0 ;
78
- let mut eof = false ;
79
81
80
82
vec:: reserve_at_least ( buf, start_len + len) ;
81
83
vec:: raw:: set_len ( buf, start_len + len) ;
@@ -88,16 +90,16 @@ impl<T: Reader> ReaderUtil for T {
88
90
total_read += nread;
89
91
}
90
92
None => {
91
- eof = true ;
93
+ read_error:: cond. raise ( standard_error ( EndOfFile ) ) ;
94
+ // Reset the vector length as though we didn't read anything
95
+ total_read = 0 ;
92
96
break ;
93
97
}
94
98
}
95
99
}
96
100
} ) . finally {
97
101
vec:: raw:: set_len ( buf, start_len + total_read) ;
98
102
}
99
-
100
- return !eof;
101
103
}
102
104
}
103
105
@@ -407,11 +409,20 @@ mod test {
407
409
assert ! ( bytes == ~[ 10 , 11 , 12 , 13 ] ) ;
408
410
}
409
411
412
+ #[ test]
413
+ fn read_bytes_eof ( ) {
414
+ let mut reader = MemReader :: new ( ~[ 10 , 11 ] ) ;
415
+ do read_error:: cond. trap ( |_| {
416
+ } ) . in {
417
+ assert ! ( reader. read_bytes( 4 ) == ~[ ] ) ;
418
+ }
419
+ }
420
+
410
421
#[ test]
411
422
fn push_bytes ( ) {
412
423
let mut reader = MemReader :: new ( ~[ 10 , 11 , 12 , 13 ] ) ;
413
424
let mut buf = ~[ 8 , 9 ] ;
414
- assert ! ( reader. push_bytes( & mut buf, 4 ) ) ;
425
+ reader. push_bytes ( & mut buf, 4 ) ;
415
426
assert ! ( buf == ~[ 8 , 9 , 10 , 11 , 12 , 13 ] ) ;
416
427
}
417
428
@@ -434,16 +445,19 @@ mod test {
434
445
}
435
446
} ;
436
447
let mut buf = ~[ 8 , 9 ] ;
437
- assert ! ( reader. push_bytes( & mut buf, 4 ) ) ;
448
+ reader. push_bytes ( & mut buf, 4 ) ;
438
449
assert ! ( buf == ~[ 8 , 9 , 10 , 11 , 12 , 13 ] ) ;
439
450
}
440
451
441
452
#[ test]
442
453
fn push_bytes_eof ( ) {
443
454
let mut reader = MemReader :: new ( ~[ 10 , 11 ] ) ;
444
455
let mut buf = ~[ 8 , 9 ] ;
445
- assert ! ( !reader. push_bytes( & mut buf, 4 ) ) ;
446
- assert ! ( buf == ~[ 8 , 9 , 10 , 11 ] ) ;
456
+ do read_error:: cond. trap ( |_| {
457
+ } ) . in {
458
+ reader. push_bytes ( & mut buf, 4 ) ;
459
+ assert ! ( buf == ~[ 8 , 9 ] ) ;
460
+ }
447
461
}
448
462
449
463
#[ test]
@@ -464,9 +478,9 @@ mod test {
464
478
} ;
465
479
let mut buf = ~[ 8 , 9 ] ;
466
480
do read_error:: cond. trap ( |_| { } ) . in {
467
- assert ! ( ! reader. push_bytes( & mut buf, 4 ) ) ;
481
+ reader. push_bytes ( & mut buf, 4 ) ;
468
482
}
469
- assert ! ( buf == ~[ 8 , 9 , 10 ] ) ;
483
+ assert ! ( buf == ~[ 8 , 9 ] ) ;
470
484
}
471
485
472
486
#[ test]
0 commit comments