@@ -7,13 +7,34 @@ use std::io::{self, Cursor, Read};
7
7
/// Parses ITM packets.
8
8
pub struct Decoder < R : Read > {
9
9
inner : R ,
10
+ follow : bool
11
+ }
12
+
13
+ // Copy&Paste from std::io::Read::read_exact
14
+ fn read_exact_gently < R : Read > ( reader : & mut R , mut buf : & mut [ u8 ] , keep_reading : bool ) -> :: std:: io:: Result < ( ) > {
15
+ use std:: io:: { ErrorKind , Error } ;
16
+ while !buf. is_empty ( ) {
17
+ match reader. read ( buf) {
18
+ Ok ( 0 ) if !keep_reading => break ,
19
+ Ok ( 0 ) if keep_reading => continue ,
20
+ Ok ( n) => { let tmp = buf; buf = & mut tmp[ n..] ; }
21
+ Err ( ref e) if e. kind ( ) == ErrorKind :: Interrupted => { }
22
+ Err ( e) => return Err ( e) ,
23
+ }
24
+ }
25
+ if !buf. is_empty ( ) {
26
+ Err ( Error :: new ( ErrorKind :: UnexpectedEof ,
27
+ "failed to fill whole buffer" ) )
28
+ } else {
29
+ Ok ( ( ) )
30
+ }
10
31
}
11
32
12
33
impl < R : Read > Decoder < R > {
13
34
/// Construct a new `Decoder` that reads encoded packets from
14
35
/// `inner`.
15
- pub fn new ( inner : R ) -> Decoder < R > {
16
- Decoder :: < R > { inner : inner }
36
+ pub fn new ( inner : R , follow : bool ) -> Decoder < R > {
37
+ Decoder :: < R > { inner : inner, follow : follow }
17
38
}
18
39
19
40
// TODO: If we need config for the Decoder, my plan is to:
@@ -53,7 +74,7 @@ impl<R: Read> Decoder<R> {
53
74
{
54
75
// Scope the mutable borrow on buf to satisfy borrow checker.
55
76
let buf = & mut ud. payload [ 0 ..payload_len] ;
56
- match self . inner . read_exact ( buf) {
77
+ match read_exact_gently ( & mut self . inner , buf, self . follow ) {
57
78
Err ( ref e) if e. kind ( ) == io:: ErrorKind :: UnexpectedEof => {
58
79
return Err ( Error :: from ( ErrorKind :: EofDuringPacket ) )
59
80
}
@@ -76,7 +97,7 @@ impl<R: Read> Decoder<R> {
76
97
77
98
/// Decode a single packet from a slice of bytes.
78
99
pub fn from_slice ( s : & [ u8 ] ) -> Result < Packet > {
79
- let mut d = Decoder :: new ( Cursor :: new ( Vec :: from ( s) ) ) ;
100
+ let mut d = Decoder :: new ( Cursor :: new ( Vec :: from ( s) ) , false ) ;
80
101
d. read_packet ( )
81
102
}
82
103
0 commit comments