@@ -32,29 +32,8 @@ impl AsyncRead for UsbStream {
32
32
) -> Poll < io:: Result < ( ) > > {
33
33
let pin = self . get_mut ( ) ;
34
34
35
- let ( copy_from_buffer, remaining) = {
36
- let unfilled = buf. initialize_unfilled ( ) ;
37
-
38
- // check if possible to read more
39
- if unfilled. is_empty ( ) {
40
- return Poll :: Pending ;
41
- }
42
-
43
- // first copy from local buffer
44
- let copy_from_buffer = std:: cmp:: min ( unfilled. len ( ) , pin. read_buffer . len ( ) ) ;
45
-
46
- if copy_from_buffer > 0 {
47
- unfilled[ ..copy_from_buffer] . copy_from_slice ( & pin. read_buffer [ ..copy_from_buffer] ) ;
48
- pin. read_buffer . drain ( ..copy_from_buffer) ;
49
- }
50
-
51
- ( copy_from_buffer, unfilled. len ( ) - copy_from_buffer)
52
- } ;
53
-
54
- buf. advance ( copy_from_buffer) ;
55
-
56
- // check if can request from remote
57
- if remaining > 0 {
35
+ // either read from local buffer or request from remote
36
+ if pin. read_buffer . is_empty ( ) {
58
37
// make sure there's pending request
59
38
if pin. read_queue . pending ( ) == 0 {
60
39
pin. read_queue . submit ( RequestBuffer :: new ( MAX_PACKET_SIZE ) ) ;
@@ -69,8 +48,7 @@ impl AsyncRead for UsbStream {
69
48
// copy into poll buffer
70
49
let copy_from_buffer = {
71
50
let unfilled = buf. initialize_unfilled ( ) ;
72
-
73
- let copy_from_buffer = std:: cmp:: min ( remaining, res. data . len ( ) ) ;
51
+ let copy_from_buffer = std:: cmp:: min ( unfilled. len ( ) , res. data . len ( ) ) ;
74
52
unfilled[ ..copy_from_buffer] . copy_from_slice ( & res. data [ ..copy_from_buffer] ) ;
75
53
76
54
copy_from_buffer
@@ -87,6 +65,28 @@ impl AsyncRead for UsbStream {
87
65
Err ( e) => Poll :: Ready ( Err ( io:: Error :: new ( io:: ErrorKind :: Other , e) ) ) ,
88
66
}
89
67
} else {
68
+ let copy_from_buffer = {
69
+ let unfilled = buf. initialize_unfilled ( ) ;
70
+
71
+ // check if possible to read more
72
+ if unfilled. is_empty ( ) {
73
+ return Poll :: Pending ;
74
+ }
75
+
76
+ // first copy from local buffer
77
+ let copy_from_buffer = std:: cmp:: min ( unfilled. len ( ) , pin. read_buffer . len ( ) ) ;
78
+
79
+ if copy_from_buffer > 0 {
80
+ unfilled[ ..copy_from_buffer]
81
+ . copy_from_slice ( & pin. read_buffer [ ..copy_from_buffer] ) ;
82
+ pin. read_buffer . drain ( ..copy_from_buffer) ;
83
+ }
84
+
85
+ copy_from_buffer
86
+ } ;
87
+
88
+ buf. advance ( copy_from_buffer) ;
89
+
90
90
Poll :: Ready ( Ok ( ( ) ) )
91
91
}
92
92
}
0 commit comments