Skip to content

Commit 4f4d058

Browse files
committed
Auto merge of #105893 - Ayush1325:remote-test-server-improve, r=Mark-Simulacrum
Use u32 methods instead of manual shifting Switch to `to_le_bytes()` and `from_le_bytes()` instead of manual shifting This was suggested [`here`](#105145 (comment)) Signed-off-by: Ayush Singh <[email protected]>
2 parents 6c0c6d6 + 51fe248 commit 4f4d058

File tree

1 file changed

+8
-12
lines changed
  • src/tools/remote-test-server/src

1 file changed

+8
-12
lines changed

src/tools/remote-test-server/src/main.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,7 @@ fn my_copy(src: &mut dyn Read, which: u8, dst: &Mutex<dyn Write>) {
365365
loop {
366366
let n = t!(src.read(&mut b));
367367
let mut dst = dst.lock().unwrap();
368-
t!(dst.write_all(&[
369-
which,
370-
(n >> 24) as u8,
371-
(n >> 16) as u8,
372-
(n >> 8) as u8,
373-
(n >> 0) as u8,
374-
]));
368+
t!(dst.write_all(&create_header(which, n as u32)));
375369
if n > 0 {
376370
t!(dst.write_all(&b[..n]));
377371
} else {
@@ -383,19 +377,21 @@ fn my_copy(src: &mut dyn Read, which: u8, dst: &Mutex<dyn Write>) {
383377
fn batch_copy(buf: &[u8], which: u8, dst: &Mutex<dyn Write>) {
384378
let n = buf.len();
385379
let mut dst = dst.lock().unwrap();
386-
t!(dst.write_all(&[which, (n >> 24) as u8, (n >> 16) as u8, (n >> 8) as u8, (n >> 0) as u8,]));
380+
t!(dst.write_all(&create_header(which, n as u32)));
387381
if n > 0 {
388382
t!(dst.write_all(buf));
389383
// Marking buf finished
390384
t!(dst.write_all(&[which, 0, 0, 0, 0,]));
391385
}
392386
}
393387

388+
const fn create_header(which: u8, n: u32) -> [u8; 5] {
389+
let bytes = n.to_be_bytes();
390+
[which, bytes[0], bytes[1], bytes[2], bytes[3]]
391+
}
392+
394393
fn read_u32(r: &mut dyn Read) -> u32 {
395394
let mut len = [0; 4];
396395
t!(r.read_exact(&mut len));
397-
((len[0] as u32) << 24)
398-
| ((len[1] as u32) << 16)
399-
| ((len[2] as u32) << 8)
400-
| ((len[3] as u32) << 0)
396+
u32::from_be_bytes(len)
401397
}

0 commit comments

Comments
 (0)