Skip to content

Commit c15bae5

Browse files
committed
Auto merge of #75086 - lzutao:u32const, r=oli-obk
Use u32::from_ne_bytes to fix a FIXME and add comment about that `u32::from_ne_bytes` has been const stable since 1.44.
2 parents 4b0882c + 30a1455 commit c15bae5

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

library/std/src/net/ip.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -319,15 +319,9 @@ impl Ipv4Addr {
319319
#[stable(feature = "rust1", since = "1.0.0")]
320320
#[rustc_const_stable(feature = "const_ipv4", since = "1.32.0")]
321321
pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
322-
// FIXME: should just be u32::from_be_bytes([a, b, c, d]),
323-
// once that method is no longer rustc_const_unstable
324-
Ipv4Addr {
325-
inner: c::in_addr {
326-
s_addr: u32::to_be(
327-
((a as u32) << 24) | ((b as u32) << 16) | ((c as u32) << 8) | (d as u32),
328-
),
329-
},
330-
}
322+
// `s_addr` is stored as BE on all machine and the array is in BE order.
323+
// So the native endian conversion method is used so that it's never swapped.
324+
Ipv4Addr { inner: c::in_addr { s_addr: u32::from_ne_bytes([a, b, c, d]) } }
331325
}
332326

333327
/// An IPv4 address with the address pointing to localhost: 127.0.0.1.

0 commit comments

Comments
 (0)