@@ -30,31 +30,32 @@ impl<'a> FillBytes for ByteSliceDriver<'a> {
30
30
31
31
#[ inline]
32
32
fn peek_bytes ( & mut self , offset : usize , bytes : & mut [ u8 ] ) -> Option < ( ) > {
33
- match self . mode {
34
- DriverMode :: Direct => {
35
- if ( offset + bytes. len ( ) ) > self . input . len ( ) {
36
- None
37
- } else {
38
- bytes. copy_from_slice ( & self . input [ offset..( offset + bytes. len ( ) ) ] ) ;
39
- Some ( ( ) )
40
- }
33
+ match self . input . len ( ) . checked_sub ( offset) {
34
+ None | Some ( 0 ) => {
35
+ // no bytes left so fill in zeros
36
+ bytes. fill ( 0 ) ;
41
37
}
42
- DriverMode :: Forced => {
43
- if offset < self . input . len ( ) {
44
- let copy_len = core:: cmp:: min ( bytes. len ( ) , self . input . len ( ) - offset) ;
45
- bytes[ ..copy_len] . copy_from_slice ( & self . input [ offset..( offset + copy_len) ] ) ;
46
- bytes[ copy_len..] . fill ( 0 ) ;
47
- } else {
48
- bytes. fill ( 0 ) ;
49
- }
50
- Some ( ( ) )
38
+ Some ( remaining_len) if remaining_len >= bytes. len ( ) => {
39
+ let input = & self . input [ offset..] ;
40
+ let input = & input[ ..bytes. len ( ) ] ;
41
+ bytes. copy_from_slice ( input) ;
42
+ }
43
+ Some ( remaining_len) => {
44
+ let input = & self . input [ offset..] ;
45
+ // we don't have enough bytes to fill the whole output
46
+ let ( head, tail) = bytes. split_at_mut ( remaining_len) ;
47
+ head. copy_from_slice ( input) ;
48
+ tail. fill ( 0 ) ;
51
49
}
52
50
}
51
+
52
+ Some ( ( ) )
53
53
}
54
54
55
55
#[ inline]
56
56
fn consume_bytes ( & mut self , consumed : usize ) {
57
- self . input = & self . input [ core:: cmp:: min ( consumed, self . input . len ( ) ) ..] ;
57
+ let consumed = consumed. min ( self . input . len ( ) ) ;
58
+ self . input = & self . input [ consumed..] ;
58
59
}
59
60
}
60
61
0 commit comments