Skip to content

Commit 11484c6

Browse files
authored
Rollup merge of #53257 - faern:ip-method-idiomatic-improvement, r=TimNN
Idiomatic improvements to IP method Since match ergonomics and slice patterns are stable this might be more idiomatic modern Rust implementations of these methods? Or well, slice patterns with `..` are not stabilized yet, so maybe we want to specify all fields but with `_`?
2 parents e77d995 + cbe80a9 commit 11484c6

File tree

1 file changed

+50
-47
lines changed

1 file changed

+50
-47
lines changed

src/libstd/net/ip.rs

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ impl IpAddr {
160160
/// ```
161161
#[stable(feature = "ip_shared", since = "1.12.0")]
162162
pub fn is_unspecified(&self) -> bool {
163-
match *self {
164-
IpAddr::V4(ref a) => a.is_unspecified(),
165-
IpAddr::V6(ref a) => a.is_unspecified(),
163+
match self {
164+
IpAddr::V4(ip) => ip.is_unspecified(),
165+
IpAddr::V6(ip) => ip.is_unspecified(),
166166
}
167167
}
168168

@@ -185,9 +185,9 @@ impl IpAddr {
185185
/// ```
186186
#[stable(feature = "ip_shared", since = "1.12.0")]
187187
pub fn is_loopback(&self) -> bool {
188-
match *self {
189-
IpAddr::V4(ref a) => a.is_loopback(),
190-
IpAddr::V6(ref a) => a.is_loopback(),
188+
match self {
189+
IpAddr::V4(ip) => ip.is_loopback(),
190+
IpAddr::V6(ip) => ip.is_loopback(),
191191
}
192192
}
193193

@@ -214,9 +214,9 @@ impl IpAddr {
214214
/// }
215215
/// ```
216216
pub fn is_global(&self) -> bool {
217-
match *self {
218-
IpAddr::V4(ref a) => a.is_global(),
219-
IpAddr::V6(ref a) => a.is_global(),
217+
match self {
218+
IpAddr::V4(ip) => ip.is_global(),
219+
IpAddr::V6(ip) => ip.is_global(),
220220
}
221221
}
222222

@@ -239,9 +239,9 @@ impl IpAddr {
239239
/// ```
240240
#[stable(feature = "ip_shared", since = "1.12.0")]
241241
pub fn is_multicast(&self) -> bool {
242-
match *self {
243-
IpAddr::V4(ref a) => a.is_multicast(),
244-
IpAddr::V6(ref a) => a.is_multicast(),
242+
match self {
243+
IpAddr::V4(ip) => ip.is_multicast(),
244+
IpAddr::V6(ip) => ip.is_multicast(),
245245
}
246246
}
247247

@@ -268,9 +268,9 @@ impl IpAddr {
268268
/// }
269269
/// ```
270270
pub fn is_documentation(&self) -> bool {
271-
match *self {
272-
IpAddr::V4(ref a) => a.is_documentation(),
273-
IpAddr::V6(ref a) => a.is_documentation(),
271+
match self {
272+
IpAddr::V4(ip) => ip.is_documentation(),
273+
IpAddr::V6(ip) => ip.is_documentation(),
274274
}
275275
}
276276

@@ -293,7 +293,7 @@ impl IpAddr {
293293
/// ```
294294
#[stable(feature = "ipaddr_checker", since = "1.16.0")]
295295
pub fn is_ipv4(&self) -> bool {
296-
match *self {
296+
match self {
297297
IpAddr::V4(_) => true,
298298
IpAddr::V6(_) => false,
299299
}
@@ -318,7 +318,7 @@ impl IpAddr {
318318
/// ```
319319
#[stable(feature = "ipaddr_checker", since = "1.16.0")]
320320
pub fn is_ipv6(&self) -> bool {
321-
match *self {
321+
match self {
322322
IpAddr::V4(_) => false,
323323
IpAddr::V6(_) => true,
324324
}
@@ -483,11 +483,11 @@ impl Ipv4Addr {
483483
/// ```
484484
#[stable(since = "1.7.0", feature = "ip_17")]
485485
pub fn is_private(&self) -> bool {
486-
match (self.octets()[0], self.octets()[1]) {
487-
(10, _) => true,
488-
(172, b) if b >= 16 && b <= 31 => true,
489-
(192, 168) => true,
490-
_ => false
486+
match self.octets() {
487+
[10, ..] => true,
488+
[172, b, ..] if b >= 16 && b <= 31 => true,
489+
[192, 168, ..] => true,
490+
_ => false,
491491
}
492492
}
493493

@@ -509,7 +509,10 @@ impl Ipv4Addr {
509509
/// ```
510510
#[stable(since = "1.7.0", feature = "ip_17")]
511511
pub fn is_link_local(&self) -> bool {
512-
self.octets()[0] == 169 && self.octets()[1] == 254
512+
match self.octets() {
513+
[169, 254, ..] => true,
514+
_ => false,
515+
}
513516
}
514517

515518
/// Returns [`true`] if the address appears to be globally routable.
@@ -612,11 +615,11 @@ impl Ipv4Addr {
612615
/// ```
613616
#[stable(since = "1.7.0", feature = "ip_17")]
614617
pub fn is_documentation(&self) -> bool {
615-
match(self.octets()[0], self.octets()[1], self.octets()[2], self.octets()[3]) {
616-
(192, 0, 2, _) => true,
617-
(198, 51, 100, _) => true,
618-
(203, 0, 113, _) => true,
619-
_ => false
618+
match self.octets() {
619+
[192, 0, 2, _] => true,
620+
[198, 51, 100, _] => true,
621+
[203, 0, 113, _] => true,
622+
_ => false,
620623
}
621624
}
622625

@@ -666,9 +669,9 @@ impl Ipv4Addr {
666669
#[stable(feature = "ip_addr", since = "1.7.0")]
667670
impl fmt::Display for IpAddr {
668671
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
669-
match *self {
670-
IpAddr::V4(ref a) => a.fmt(fmt),
671-
IpAddr::V6(ref a) => a.fmt(fmt),
672+
match self {
673+
IpAddr::V4(ip) => ip.fmt(fmt),
674+
IpAddr::V6(ip) => ip.fmt(fmt),
672675
}
673676
}
674677
}
@@ -717,8 +720,8 @@ impl PartialEq for Ipv4Addr {
717720
#[stable(feature = "ip_cmp", since = "1.16.0")]
718721
impl PartialEq<Ipv4Addr> for IpAddr {
719722
fn eq(&self, other: &Ipv4Addr) -> bool {
720-
match *self {
721-
IpAddr::V4(ref v4) => v4 == other,
723+
match self {
724+
IpAddr::V4(v4) => v4 == other,
722725
IpAddr::V6(_) => false,
723726
}
724727
}
@@ -727,8 +730,8 @@ impl PartialEq<Ipv4Addr> for IpAddr {
727730
#[stable(feature = "ip_cmp", since = "1.16.0")]
728731
impl PartialEq<IpAddr> for Ipv4Addr {
729732
fn eq(&self, other: &IpAddr) -> bool {
730-
match *other {
731-
IpAddr::V4(ref v4) => self == v4,
733+
match other {
734+
IpAddr::V4(v4) => self == v4,
732735
IpAddr::V6(_) => false,
733736
}
734737
}
@@ -755,8 +758,8 @@ impl PartialOrd for Ipv4Addr {
755758
#[stable(feature = "ip_cmp", since = "1.16.0")]
756759
impl PartialOrd<Ipv4Addr> for IpAddr {
757760
fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> {
758-
match *self {
759-
IpAddr::V4(ref v4) => v4.partial_cmp(other),
761+
match self {
762+
IpAddr::V4(v4) => v4.partial_cmp(other),
760763
IpAddr::V6(_) => Some(Ordering::Greater),
761764
}
762765
}
@@ -765,8 +768,8 @@ impl PartialOrd<Ipv4Addr> for IpAddr {
765768
#[stable(feature = "ip_cmp", since = "1.16.0")]
766769
impl PartialOrd<IpAddr> for Ipv4Addr {
767770
fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
768-
match *other {
769-
IpAddr::V4(ref v4) => self.partial_cmp(v4),
771+
match other {
772+
IpAddr::V4(v4) => self.partial_cmp(v4),
770773
IpAddr::V6(_) => Some(Ordering::Less),
771774
}
772775
}
@@ -1335,19 +1338,19 @@ impl PartialEq for Ipv6Addr {
13351338
#[stable(feature = "ip_cmp", since = "1.16.0")]
13361339
impl PartialEq<IpAddr> for Ipv6Addr {
13371340
fn eq(&self, other: &IpAddr) -> bool {
1338-
match *other {
1341+
match other {
13391342
IpAddr::V4(_) => false,
1340-
IpAddr::V6(ref v6) => self == v6,
1343+
IpAddr::V6(v6) => self == v6,
13411344
}
13421345
}
13431346
}
13441347

13451348
#[stable(feature = "ip_cmp", since = "1.16.0")]
13461349
impl PartialEq<Ipv6Addr> for IpAddr {
13471350
fn eq(&self, other: &Ipv6Addr) -> bool {
1348-
match *self {
1351+
match self {
13491352
IpAddr::V4(_) => false,
1350-
IpAddr::V6(ref v6) => v6 == other,
1353+
IpAddr::V6(v6) => v6 == other,
13511354
}
13521355
}
13531356
}
@@ -1372,19 +1375,19 @@ impl PartialOrd for Ipv6Addr {
13721375
#[stable(feature = "ip_cmp", since = "1.16.0")]
13731376
impl PartialOrd<Ipv6Addr> for IpAddr {
13741377
fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
1375-
match *self {
1378+
match self {
13761379
IpAddr::V4(_) => Some(Ordering::Less),
1377-
IpAddr::V6(ref v6) => v6.partial_cmp(other),
1380+
IpAddr::V6(v6) => v6.partial_cmp(other),
13781381
}
13791382
}
13801383
}
13811384

13821385
#[stable(feature = "ip_cmp", since = "1.16.0")]
13831386
impl PartialOrd<IpAddr> for Ipv6Addr {
13841387
fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
1385-
match *other {
1388+
match other {
13861389
IpAddr::V4(_) => Some(Ordering::Greater),
1387-
IpAddr::V6(ref v6) => self.partial_cmp(v6),
1390+
IpAddr::V6(v6) => self.partial_cmp(v6),
13881391
}
13891392
}
13901393
}

0 commit comments

Comments
 (0)