Skip to content

Commit dcccb47

Browse files
authored
Rollup merge of #75036 - lzutao:pat, r=cuviper
Prefer pattern matching over indexing Quite a bit nicer IMO. r? @cuviper
2 parents cbe7c5c + e8ea6e5 commit dcccb47

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

library/std/src/net/ip.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -767,10 +767,8 @@ impl Ipv4Addr {
767767
/// ```
768768
#[stable(feature = "rust1", since = "1.0.0")]
769769
pub fn to_ipv6_compatible(&self) -> Ipv6Addr {
770-
let octets = self.octets();
771-
Ipv6Addr::from([
772-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, octets[0], octets[1], octets[2], octets[3],
773-
])
770+
let [a, b, c, d] = self.octets();
771+
Ipv6Addr::from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, a, b, c, d])
774772
}
775773

776774
/// Converts this address to an IPv4-mapped [IPv6 address].
@@ -789,10 +787,8 @@ impl Ipv4Addr {
789787
/// ```
790788
#[stable(feature = "rust1", since = "1.0.0")]
791789
pub fn to_ipv6_mapped(&self) -> Ipv6Addr {
792-
let octets = self.octets();
793-
Ipv6Addr::from([
794-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, octets[0], octets[1], octets[2], octets[3],
795-
])
790+
let [a, b, c, d] = self.octets();
791+
Ipv6Addr::from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, a, b, c, d])
796792
}
797793
}
798794

@@ -1498,11 +1494,12 @@ impl Ipv6Addr {
14981494
/// ```
14991495
#[stable(feature = "rust1", since = "1.0.0")]
15001496
pub fn to_ipv4(&self) -> Option<Ipv4Addr> {
1501-
match self.segments() {
1502-
[0, 0, 0, 0, 0, f, g, h] if f == 0 || f == 0xffff => {
1503-
Some(Ipv4Addr::new((g >> 8) as u8, g as u8, (h >> 8) as u8, h as u8))
1504-
}
1505-
_ => None,
1497+
if let [0, 0, 0, 0, 0, 0 | 0xffff, ab, cd] = self.segments() {
1498+
let [a, b] = ab.to_be_bytes();
1499+
let [c, d] = cd.to_be_bytes();
1500+
Some(Ipv4Addr::new(a, b, c, d))
1501+
} else {
1502+
None
15061503
}
15071504
}
15081505

0 commit comments

Comments
 (0)