Skip to content

Commit 5d80584

Browse files
committed
Use less explicit shifting in std::net::ip
Now that we have {to|from}_be_bytes the code can be simpler. (Inspired by PR #57740)
1 parent 16ca0b9 commit 5d80584

File tree

1 file changed

+58
-25
lines changed

1 file changed

+58
-25
lines changed

src/libstd/net/ip.rs

+58-25
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,8 @@ impl Ipv4Addr {
329329
/// ```
330330
#[stable(feature = "rust1", since = "1.0.0")]
331331
pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
332+
// FIXME: should just be u32::from_be_bytes([a, b, c, d]),
333+
// once that method is no longer rustc_const_unstable
332334
Ipv4Addr {
333335
inner: c::in_addr {
334336
s_addr: u32::to_be(
@@ -392,6 +394,7 @@ impl Ipv4Addr {
392394
/// ```
393395
#[stable(feature = "rust1", since = "1.0.0")]
394396
pub fn octets(&self) -> [u8; 4] {
397+
// This returns the order we want because s_addr is stored in big-endian.
395398
self.inner.s_addr.to_ne_bytes()
396399
}
397400

@@ -618,9 +621,13 @@ impl Ipv4Addr {
618621
/// ```
619622
#[stable(feature = "rust1", since = "1.0.0")]
620623
pub fn to_ipv6_compatible(&self) -> Ipv6Addr {
621-
Ipv6Addr::new(0, 0, 0, 0, 0, 0,
622-
((self.octets()[0] as u16) << 8) | self.octets()[1] as u16,
623-
((self.octets()[2] as u16) << 8) | self.octets()[3] as u16)
624+
let octets = self.octets();
625+
Ipv6Addr::from([
626+
0, 0, 0, 0,
627+
0, 0, 0, 0,
628+
0, 0, 0, 0,
629+
octets[0], octets[1], octets[2], octets[3],
630+
])
624631
}
625632

626633
/// Converts this address to an IPv4-mapped [IPv6 address].
@@ -639,9 +646,13 @@ impl Ipv4Addr {
639646
/// ```
640647
#[stable(feature = "rust1", since = "1.0.0")]
641648
pub fn to_ipv6_mapped(&self) -> Ipv6Addr {
642-
Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff,
643-
((self.octets()[0] as u16) << 8) | self.octets()[1] as u16,
644-
((self.octets()[2] as u16) << 8) | self.octets()[3] as u16)
649+
let octets = self.octets();
650+
Ipv6Addr::from([
651+
0, 0, 0, 0,
652+
0, 0, 0, 0,
653+
0, 0, 0xFF, 0xFF,
654+
octets[0], octets[1], octets[2], octets[3],
655+
])
645656
}
646657
}
647658

@@ -784,7 +795,7 @@ impl From<Ipv4Addr> for u32 {
784795
/// ```
785796
fn from(ip: Ipv4Addr) -> u32 {
786797
let ip = ip.octets();
787-
((ip[0] as u32) << 24) + ((ip[1] as u32) << 16) + ((ip[2] as u32) << 8) + (ip[3] as u32)
798+
u32::from_be_bytes(ip)
788799
}
789800
}
790801

@@ -801,7 +812,7 @@ impl From<u32> for Ipv4Addr {
801812
/// assert_eq!(Ipv4Addr::new(13, 12, 11, 10), addr);
802813
/// ```
803814
fn from(ip: u32) -> Ipv4Addr {
804-
Ipv4Addr::new((ip >> 24) as u8, (ip >> 16) as u8, (ip >> 8) as u8, ip as u8)
815+
Ipv4Addr::from(ip.to_be_bytes())
805816
}
806817
}
807818

@@ -909,14 +920,14 @@ impl Ipv6Addr {
909920
pub fn segments(&self) -> [u16; 8] {
910921
let arr = &self.inner.s6_addr;
911922
[
912-
(arr[0] as u16) << 8 | (arr[1] as u16),
913-
(arr[2] as u16) << 8 | (arr[3] as u16),
914-
(arr[4] as u16) << 8 | (arr[5] as u16),
915-
(arr[6] as u16) << 8 | (arr[7] as u16),
916-
(arr[8] as u16) << 8 | (arr[9] as u16),
917-
(arr[10] as u16) << 8 | (arr[11] as u16),
918-
(arr[12] as u16) << 8 | (arr[13] as u16),
919-
(arr[14] as u16) << 8 | (arr[15] as u16),
923+
u16::from_be_bytes([arr[0], arr[1]]),
924+
u16::from_be_bytes([arr[2], arr[3]]),
925+
u16::from_be_bytes([arr[4], arr[5]]),
926+
u16::from_be_bytes([arr[6], arr[7]]),
927+
u16::from_be_bytes([arr[8], arr[9]]),
928+
u16::from_be_bytes([arr[10], arr[11]]),
929+
u16::from_be_bytes([arr[12], arr[13]]),
930+
u16::from_be_bytes([arr[14], arr[15]]),
920931
]
921932
}
922933

@@ -1382,21 +1393,43 @@ impl FromInner<c::in6_addr> for Ipv6Addr {
13821393

13831394
#[stable(feature = "i128", since = "1.26.0")]
13841395
impl From<Ipv6Addr> for u128 {
1396+
/// Convert an `Ipv6Addr` into a host byte order `u128`.
1397+
///
1398+
/// # Examples
1399+
///
1400+
/// ```
1401+
/// use std::net::Ipv6Addr;
1402+
///
1403+
/// let addr = Ipv6Addr::new(
1404+
/// 0x1020, 0x3040, 0x5060, 0x7080,
1405+
/// 0x90A0, 0xB0C0, 0xD0E0, 0xF00D,
1406+
/// );
1407+
/// assert_eq!(0x102030405060708090A0B0C0D0E0F00D_u128, u128::from(addr));
1408+
/// ```
13851409
fn from(ip: Ipv6Addr) -> u128 {
1386-
let ip = ip.segments();
1387-
((ip[0] as u128) << 112) + ((ip[1] as u128) << 96) + ((ip[2] as u128) << 80) +
1388-
((ip[3] as u128) << 64) + ((ip[4] as u128) << 48) + ((ip[5] as u128) << 32) +
1389-
((ip[6] as u128) << 16) + (ip[7] as u128)
1410+
let ip = ip.octets();
1411+
u128::from_be_bytes(ip)
13901412
}
13911413
}
13921414
#[stable(feature = "i128", since = "1.26.0")]
13931415
impl From<u128> for Ipv6Addr {
1416+
/// Convert a host byte order `u128` into an `Ipv6Addr`.
1417+
///
1418+
/// # Examples
1419+
///
1420+
/// ```
1421+
/// use std::net::Ipv6Addr;
1422+
///
1423+
/// let addr = Ipv6Addr::from(0x102030405060708090A0B0C0D0E0F00D_u128);
1424+
/// assert_eq!(
1425+
/// Ipv6Addr::new(
1426+
/// 0x1020, 0x3040, 0x5060, 0x7080,
1427+
/// 0x90A0, 0xB0C0, 0xD0E0, 0xF00D,
1428+
/// ),
1429+
/// addr);
1430+
/// ```
13941431
fn from(ip: u128) -> Ipv6Addr {
1395-
Ipv6Addr::new(
1396-
(ip >> 112) as u16, (ip >> 96) as u16, (ip >> 80) as u16,
1397-
(ip >> 64) as u16, (ip >> 48) as u16, (ip >> 32) as u16,
1398-
(ip >> 16) as u16, ip as u16,
1399-
)
1432+
Ipv6Addr::from(ip.to_be_bytes())
14001433
}
14011434
}
14021435

0 commit comments

Comments
 (0)