diff options
| author | Lzu Tao <taolzu@gmail.com> | 2020-08-11 16:07:39 +0000 |
|---|---|---|
| committer | Lzu Tao <taolzu@gmail.com> | 2020-08-11 16:07:39 +0000 |
| commit | e8ea6e59f088708a3450334319c251a24b2f9c23 (patch) | |
| tree | de35998b98b0acc7cc175170f0bb1059d08cc90f | |
| parent | 4b9ac5161781ca6a376daab3d3b2f2623d8f3789 (diff) | |
| download | rust-e8ea6e59f088708a3450334319c251a24b2f9c23.tar.gz rust-e8ea6e59f088708a3450334319c251a24b2f9c23.zip | |
prefer pattern matching over indexing
| -rw-r--r-- | library/std/src/net/ip.rs | 23 |
1 files changed, 10 insertions, 13 deletions
diff --git a/library/std/src/net/ip.rs b/library/std/src/net/ip.rs index 5d103e64037..ccaacbf1a58 100644 --- a/library/std/src/net/ip.rs +++ b/library/std/src/net/ip.rs @@ -767,10 +767,8 @@ impl Ipv4Addr { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn to_ipv6_compatible(&self) -> Ipv6Addr { - let octets = self.octets(); - Ipv6Addr::from([ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, octets[0], octets[1], octets[2], octets[3], - ]) + let [a, b, c, d] = self.octets(); + Ipv6Addr::from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, a, b, c, d]) } /// Converts this address to an IPv4-mapped [IPv6 address]. @@ -789,10 +787,8 @@ impl Ipv4Addr { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn to_ipv6_mapped(&self) -> Ipv6Addr { - let octets = self.octets(); - Ipv6Addr::from([ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, octets[0], octets[1], octets[2], octets[3], - ]) + let [a, b, c, d] = self.octets(); + Ipv6Addr::from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, a, b, c, d]) } } @@ -1498,11 +1494,12 @@ impl Ipv6Addr { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn to_ipv4(&self) -> Option<Ipv4Addr> { - match self.segments() { - [0, 0, 0, 0, 0, f, g, h] if f == 0 || f == 0xffff => { - Some(Ipv4Addr::new((g >> 8) as u8, g as u8, (h >> 8) as u8, h as u8)) - } - _ => None, + if let [0, 0, 0, 0, 0, 0 | 0xffff, ab, cd] = self.segments() { + let [a, b] = ab.to_be_bytes(); + let [c, d] = cd.to_be_bytes(); + Some(Ipv4Addr::new(a, b, c, d)) + } else { + None } } |
