Skip to content

Commit

Permalink
Auto merge of rust-lang#105893 - Ayush1325:remote-test-server-improve…
Browse files Browse the repository at this point in the history
…, 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`](rust-lang#105145 (comment))

Signed-off-by: Ayush Singh <[email protected]>
  • Loading branch information
bors committed Dec 24, 2022
2 parents 6c0c6d6 + 51fe248 commit 4f4d058
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/tools/remote-test-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,13 +365,7 @@ fn my_copy(src: &mut dyn Read, which: u8, dst: &Mutex<dyn Write>) {
loop {
let n = t!(src.read(&mut b));
let mut dst = dst.lock().unwrap();
t!(dst.write_all(&[
which,
(n >> 24) as u8,
(n >> 16) as u8,
(n >> 8) as u8,
(n >> 0) as u8,
]));
t!(dst.write_all(&create_header(which, n as u32)));
if n > 0 {
t!(dst.write_all(&b[..n]));
} else {
Expand All @@ -383,19 +377,21 @@ fn my_copy(src: &mut dyn Read, which: u8, dst: &Mutex<dyn Write>) {
fn batch_copy(buf: &[u8], which: u8, dst: &Mutex<dyn Write>) {
let n = buf.len();
let mut dst = dst.lock().unwrap();
t!(dst.write_all(&[which, (n >> 24) as u8, (n >> 16) as u8, (n >> 8) as u8, (n >> 0) as u8,]));
t!(dst.write_all(&create_header(which, n as u32)));
if n > 0 {
t!(dst.write_all(buf));
// Marking buf finished
t!(dst.write_all(&[which, 0, 0, 0, 0,]));
}
}

const fn create_header(which: u8, n: u32) -> [u8; 5] {
let bytes = n.to_be_bytes();
[which, bytes[0], bytes[1], bytes[2], bytes[3]]
}

fn read_u32(r: &mut dyn Read) -> u32 {
let mut len = [0; 4];
t!(r.read_exact(&mut len));
((len[0] as u32) << 24)
| ((len[1] as u32) << 16)
| ((len[2] as u32) << 8)
| ((len[3] as u32) << 0)
u32::from_be_bytes(len)
}

0 comments on commit 4f4d058

Please sign in to comment.