]> git.lizzy.rs Git - rust.git/commitdiff
Change implementation of `Ipv6Addr::is_unspecified` and `is_loopback` from `matches...
authorCDirkx <christiaan@dirkx.com>
Tue, 1 Sep 2020 19:05:26 +0000 (21:05 +0200)
committerCDirkx <christiaan@dirkx.com>
Tue, 1 Sep 2020 19:05:26 +0000 (21:05 +0200)
Done because `matches!` doesn't optimize well with array comparisons

library/std/src/net/ip.rs

index 8e3dbf8fcb48b3095942b18f8bd7ad4e78177aeb..341a112df71b3c51775af40a39e8620b73e10f1a 100644 (file)
@@ -1139,7 +1139,7 @@ pub const fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16, h: u16)
     #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
     #[stable(since = "1.7.0", feature = "ip_17")]
     pub const fn is_unspecified(&self) -> bool {
-        matches!(self.segments(), [0, 0, 0, 0, 0, 0, 0, 0])
+        u128::from_be_bytes(self.octets()) == u128::from_be_bytes(Ipv6Addr::UNSPECIFIED.octets())
     }
 
     /// Returns [`true`] if this is a loopback address (::1).
@@ -1160,7 +1160,7 @@ pub const fn is_unspecified(&self) -> bool {
     #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
     #[stable(since = "1.7.0", feature = "ip_17")]
     pub const fn is_loopback(&self) -> bool {
-        matches!(self.segments(), [0, 0, 0, 0, 0, 0, 0, 1])
+        u128::from_be_bytes(self.octets()) == u128::from_be_bytes(Ipv6Addr::LOCALHOST.octets())
     }
 
     /// Returns [`true`] if the address appears to be globally routable.