1
1
use std:: {
2
- collections:: VecDeque ,
3
2
io:: { BufWriter , Write } ,
4
3
thread:: sleep,
5
4
time:: Duration ,
@@ -29,7 +28,6 @@ pub struct CommandResponse {
29
28
pub struct Connection {
30
29
serial : Box < dyn SerialPort > ,
31
30
decoder : SlipDecoder ,
32
- buffer : VecDeque < u8 > ,
33
31
}
34
32
35
33
#[ derive( Zeroable , Pod , Copy , Clone , Debug ) ]
@@ -46,7 +44,6 @@ impl Connection {
46
44
Connection {
47
45
serial,
48
46
decoder : SlipDecoder :: new ( ) ,
49
- buffer : VecDeque :: with_capacity ( 128 ) ,
50
47
}
51
48
}
52
49
@@ -119,7 +116,6 @@ impl Connection {
119
116
}
120
117
121
118
pub fn write_command ( & mut self , command : Command ) -> Result < ( ) , Error > {
122
- self . buffer . clear ( ) ;
123
119
self . serial . clear ( serialport:: ClearBuffer :: Input ) ?;
124
120
let mut writer = BufWriter :: new ( & mut self . serial ) ;
125
121
let mut encoder = SlipEncoder :: new ( & mut writer) ?;
@@ -172,23 +168,16 @@ impl Connection {
172
168
}
173
169
174
170
fn read ( & mut self , len : usize ) -> Result < Option < Vec < u8 > > , Error > {
175
- let mut output = Vec :: with_capacity ( 1024 ) ;
176
- self . decoder . decode ( & mut self . serial , & mut output ) ? ;
177
-
178
- self . buffer . extend ( & output ) ;
179
- if self . buffer . len ( ) < len {
180
- return Ok ( None ) ;
171
+ let mut tmp = Vec :: with_capacity ( 1024 ) ;
172
+ loop {
173
+ self . decoder . decode ( & mut self . serial , & mut tmp ) ? ;
174
+ if tmp . len ( ) >= len {
175
+ return Ok ( Some ( tmp ) ) ;
176
+ }
181
177
}
182
-
183
- // reuse allocation
184
- output. clear ( ) ;
185
- output. extend ( self . buffer . drain ( ..) ) ;
186
-
187
- Ok ( Some ( output) )
188
178
}
189
179
190
180
pub fn flush ( & mut self ) -> Result < ( ) , Error > {
191
- self . buffer . clear ( ) ;
192
181
self . serial . flush ( ) ?;
193
182
Ok ( ( ) )
194
183
}
0 commit comments