about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLzu Tao <taolzu@gmail.com>2020-08-05 02:49:26 +0000
committerLzu Tao <taolzu@gmail.com>2020-08-05 02:49:26 +0000
commit30a1455c8da95ca3f4f8fb33edfa556678e3ac58 (patch)
treecd4ad577f40a324d5bc154f3584054f7696557ea
parent07f1fdecfed85fe4be14b293eb913560a6cd60ba (diff)
downloadrust-30a1455c8da95ca3f4f8fb33edfa556678e3ac58.tar.gz
rust-30a1455c8da95ca3f4f8fb33edfa556678e3ac58.zip
Use u32::from_ne_bytes to fix a FIXME
Co-authored-by: Weiyi Wang <wwylele@gmail.com>
Co-authored-by: Adam Reichold <adam.reichold@t-online.de>
Co-authored-by: Josh Stone <cuviper@gmail.com>
Co-authored-by: Scott McMurray <scottmcm@users.noreply.github.com>
Co-authored-by: tmiasko <tomasz.miasko@gmail.com>
-rw-r--r--library/std/src/net/ip.rs12
1 files changed, 3 insertions, 9 deletions
diff --git a/library/std/src/net/ip.rs b/library/std/src/net/ip.rs
index a64b43ca3ad..a0c0981ebf9 100644
--- a/library/std/src/net/ip.rs
+++ b/library/std/src/net/ip.rs
@@ -319,15 +319,9 @@ impl Ipv4Addr {
     #[stable(feature = "rust1", since = "1.0.0")]
     #[rustc_const_stable(feature = "const_ipv4", since = "1.32.0")]
     pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
-        // FIXME: should just be u32::from_be_bytes([a, b, c, d]),
-        // once that method is no longer rustc_const_unstable
-        Ipv4Addr {
-            inner: c::in_addr {
-                s_addr: u32::to_be(
-                    ((a as u32) << 24) | ((b as u32) << 16) | ((c as u32) << 8) | (d as u32),
-                ),
-            },
-        }
+        // `s_addr` is stored as BE on all machine and the array is in BE order.
+        // So the native endian conversion method is used so that it's never swapped.
+        Ipv4Addr { inner: c::in_addr { s_addr: u32::from_ne_bytes([a, b, c, d]) } }
     }
 
     /// An IPv4 address with the address pointing to localhost: 127.0.0.1.