Skip to content

Commit a43dd4f

Browse files
author
CDirkx
committed
Change implementation of Ipv6Addr::is_unspecified and is_loopback from matches! to u128 comparison
Done because `matches!` doesn't optimize well with array comparisons
1 parent cd08def commit a43dd4f

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

library/std/src/net/ip.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,7 @@ impl Ipv6Addr {
11391139
#[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
11401140
#[stable(since = "1.7.0", feature = "ip_17")]
11411141
pub const fn is_unspecified(&self) -> bool {
1142-
matches!(self.segments(), [0, 0, 0, 0, 0, 0, 0, 0])
1142+
u128::from_be_bytes(self.octets()) == u128::from_be_bytes(Ipv6Addr::UNSPECIFIED.octets())
11431143
}
11441144

11451145
/// Returns [`true`] if this is a loopback address (::1).
@@ -1160,7 +1160,7 @@ impl Ipv6Addr {
11601160
#[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
11611161
#[stable(since = "1.7.0", feature = "ip_17")]
11621162
pub const fn is_loopback(&self) -> bool {
1163-
matches!(self.segments(), [0, 0, 0, 0, 0, 0, 0, 1])
1163+
u128::from_be_bytes(self.octets()) == u128::from_be_bytes(Ipv6Addr::LOCALHOST.octets())
11641164
}
11651165

11661166
/// Returns [`true`] if the address appears to be globally routable.

0 commit comments

Comments
 (0)