Skip to content

Commit 07d0f67

Browse files
committed
improve USB stream code
1 parent e13c017 commit 07d0f67

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

RustApp/src/usb/frame.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,8 @@ impl AsyncRead for UsbStream {
3232
) -> Poll<io::Result<()>> {
3333
let pin = self.get_mut();
3434

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() {
5837
// make sure there's pending request
5938
if pin.read_queue.pending() == 0 {
6039
pin.read_queue.submit(RequestBuffer::new(MAX_PACKET_SIZE));
@@ -69,8 +48,7 @@ impl AsyncRead for UsbStream {
6948
// copy into poll buffer
7049
let copy_from_buffer = {
7150
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());
7452
unfilled[..copy_from_buffer].copy_from_slice(&res.data[..copy_from_buffer]);
7553

7654
copy_from_buffer
@@ -87,6 +65,28 @@ impl AsyncRead for UsbStream {
8765
Err(e) => Poll::Ready(Err(io::Error::new(io::ErrorKind::Other, e))),
8866
}
8967
} 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+
9090
Poll::Ready(Ok(()))
9191
}
9292
}

0 commit comments

Comments
 (0)