about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-12-24 04:54:39 +0000
committerbors <bors@rust-lang.org>2022-12-24 04:54:39 +0000
commit4f4d0586ad20c66a16d547581ca379beafece93a (patch)
tree164ced8f8b6b0a56c82728e903f31a281e2bbcc9
parent6c0c6d6eb3b38931f9dd4f5466185591e91d51c9 (diff)
parent51fe24873fbd255e80063a97ecd406726c1e6675 (diff)
downloadrust-4f4d0586ad20c66a16d547581ca379beafece93a.tar.gz
rust-4f4d0586ad20c66a16d547581ca379beafece93a.zip
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`](https://github.com/rust-lang/rust/pull/105145#discussion_r1051418964)

Signed-off-by: Ayush Singh <ayushsingh1325@gmail.com>
-rw-r--r--src/tools/remote-test-server/src/main.rs20
1 files changed, 8 insertions, 12 deletions
diff --git a/src/tools/remote-test-server/src/main.rs b/src/tools/remote-test-server/src/main.rs
index c1450aedc31..3d61a067559 100644
--- a/src/tools/remote-test-server/src/main.rs
+++ b/src/tools/remote-test-server/src/main.rs
@@ -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 {
@@ -383,7 +377,7 @@ 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
@@ -391,11 +385,13 @@ fn batch_copy(buf: &[u8], which: u8, dst: &Mutex<dyn Write>) {
     }
 }
 
+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)
 }