@@ -7,13 +7,34 @@ use std::io::{self, Cursor, Read};
77/// Parses ITM packets.
88pub struct Decoder < R : Read > {
99 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+ }
1031}
1132
1233impl < R : Read > Decoder < R > {
1334 /// Construct a new `Decoder` that reads encoded packets from
1435 /// `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 }
1738 }
1839
1940 // TODO: If we need config for the Decoder, my plan is to:
@@ -53,7 +74,7 @@ impl<R: Read> Decoder<R> {
5374 {
5475 // Scope the mutable borrow on buf to satisfy borrow checker.
5576 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 ) {
5778 Err ( ref e) if e. kind ( ) == io:: ErrorKind :: UnexpectedEof => {
5879 return Err ( Error :: from ( ErrorKind :: EofDuringPacket ) )
5980 }
@@ -76,7 +97,7 @@ impl<R: Read> Decoder<R> {
7697
7798/// Decode a single packet from a slice of bytes.
7899pub 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 ) ;
80101 d. read_packet ( )
81102}
82103
0 commit comments