Skip to content

Commit 484032f

Browse files
authored
Rollup merge of rust-lang#86434 - CDirkx:ipv6-benchmarking, r=joshtriplett
Add `Ipv6Addr::is_benchmarking` This PR adds the unstable method `Ipv6Addr::is_benchmarking`. This method is added for parity with `Ipv4Addr::is_benchmarking`, and I intend to use it in a future rework of `Ipv6Addr::is_global` (edit: rust-lang#86634) to more accurately follow the [IANA Special Address Registry](https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml) (like is done in `Ipv4Addr::is_global`). With `Ipv6Addr::is_benchmarking` and `Ipv4Addr::is_benchmarking` now both existing, `IpAddr::is_benchmarking` is also added.
2 parents e737694 + cbaccc1 commit 484032f

File tree

2 files changed

+103
-32
lines changed

2 files changed

+103
-32
lines changed

library/std/src/net/ip.rs

+46
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,30 @@ impl IpAddr {
340340
}
341341
}
342342

343+
/// Returns [`true`] if this address is in a range designated for benchmarking.
344+
///
345+
/// See the documentation for [`Ipv4Addr::is_benchmarking()`] and
346+
/// [`Ipv6Addr::is_benchmarking()`] for more details.
347+
///
348+
/// # Examples
349+
///
350+
/// ```
351+
/// #![feature(ip)]
352+
///
353+
/// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
354+
///
355+
/// assert_eq!(IpAddr::V4(Ipv4Addr::new(198, 19, 255, 255)).is_benchmarking(), true);
356+
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0x2, 0, 0, 0, 0, 0, 0)).is_benchmarking(), true);
357+
/// ```
358+
#[unstable(feature = "ip", issue = "27709")]
359+
#[inline]
360+
pub const fn is_benchmarking(&self) -> bool {
361+
match self {
362+
IpAddr::V4(ip) => ip.is_benchmarking(),
363+
IpAddr::V6(ip) => ip.is_benchmarking(),
364+
}
365+
}
366+
343367
/// Returns [`true`] if this address is an [`IPv4` address], and [`false`]
344368
/// otherwise.
345369
///
@@ -1449,6 +1473,28 @@ impl Ipv6Addr {
14491473
(self.segments()[0] == 0x2001) && (self.segments()[1] == 0xdb8)
14501474
}
14511475

1476+
/// Returns [`true`] if this is an address reserved for benchmarking (`2001:2::/48`).
1477+
///
1478+
/// This property is defined in [IETF RFC 5180], where it is mistakenly specified as covering the range `2001:0200::/48`.
1479+
/// This is corrected in [IETF RFC Errata 1752] to `2001:0002::/48`.
1480+
///
1481+
/// [IETF RFC 5180]: https://tools.ietf.org/html/rfc5180
1482+
/// [IETF RFC Errata 1752]: https://www.rfc-editor.org/errata_search.php?eid=1752
1483+
///
1484+
/// ```
1485+
/// #![feature(ip)]
1486+
///
1487+
/// use std::net::Ipv6Addr;
1488+
///
1489+
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc613, 0x0).is_benchmarking(), false);
1490+
/// assert_eq!(Ipv6Addr::new(0x2001, 0x2, 0, 0, 0, 0, 0, 0).is_benchmarking(), true);
1491+
/// ```
1492+
#[unstable(feature = "ip", issue = "27709")]
1493+
#[inline]
1494+
pub const fn is_benchmarking(&self) -> bool {
1495+
(self.segments()[0] == 0x2001) && (self.segments()[1] == 0x2) && (self.segments()[2] == 0)
1496+
}
1497+
14521498
/// Returns [`true`] if the address is a globally routable unicast address.
14531499
///
14541500
/// The following return false:

library/std/src/net/ip/tests.rs

+57-32
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ fn ip_properties() {
224224
let global: u8 = 1 << 2;
225225
let multicast: u8 = 1 << 3;
226226
let doc: u8 = 1 << 4;
227+
let benchmarking: u8 = 1 << 5;
227228

228229
if ($mask & unspec) == unspec {
229230
assert!(ip!($s).is_unspecified());
@@ -254,6 +255,12 @@ fn ip_properties() {
254255
} else {
255256
assert!(!ip!($s).is_documentation());
256257
}
258+
259+
if ($mask & benchmarking) == benchmarking {
260+
assert!(ip!($s).is_benchmarking());
261+
} else {
262+
assert!(!ip!($s).is_benchmarking());
263+
}
257264
}};
258265
}
259266

@@ -262,6 +269,7 @@ fn ip_properties() {
262269
let global: u8 = 1 << 2;
263270
let multicast: u8 = 1 << 3;
264271
let doc: u8 = 1 << 4;
272+
let benchmarking: u8 = 1 << 5;
265273

266274
check!("0.0.0.0", unspec);
267275
check!("0.0.0.1");
@@ -280,9 +288,9 @@ fn ip_properties() {
280288
check!("239.255.255.255", global | multicast);
281289
check!("255.255.255.255");
282290
// make sure benchmarking addresses are not global
283-
check!("198.18.0.0");
284-
check!("198.18.54.2");
285-
check!("198.19.255.255");
291+
check!("198.18.0.0", benchmarking);
292+
check!("198.18.54.2", benchmarking);
293+
check!("198.19.255.255", benchmarking);
286294
// make sure addresses reserved for protocol assignment are not global
287295
check!("192.0.0.0");
288296
check!("192.0.0.255");
@@ -313,6 +321,7 @@ fn ip_properties() {
313321
check!("ff08::", multicast);
314322
check!("ff0e::", global | multicast);
315323
check!("2001:db8:85a3::8a2e:370:7334", doc);
324+
check!("2001:2::ac32:23ff:21", global | benchmarking);
316325
check!("102:304:506:708:90a:b0c:d0e:f10", global);
317326
}
318327

@@ -467,21 +476,22 @@ fn ipv6_properties() {
467476
assert_eq!(&ip!($s).octets(), octets);
468477
assert_eq!(Ipv6Addr::from(*octets), ip!($s));
469478

470-
let unspecified: u16 = 1 << 0;
471-
let loopback: u16 = 1 << 1;
472-
let unique_local: u16 = 1 << 2;
473-
let global: u16 = 1 << 3;
474-
let unicast_link_local: u16 = 1 << 4;
475-
let unicast_global: u16 = 1 << 7;
476-
let documentation: u16 = 1 << 8;
477-
let multicast_interface_local: u16 = 1 << 9;
478-
let multicast_link_local: u16 = 1 << 10;
479-
let multicast_realm_local: u16 = 1 << 11;
480-
let multicast_admin_local: u16 = 1 << 12;
481-
let multicast_site_local: u16 = 1 << 13;
482-
let multicast_organization_local: u16 = 1 << 14;
483-
let multicast_global: u16 = 1 << 15;
484-
let multicast: u16 = multicast_interface_local
479+
let unspecified: u32 = 1 << 0;
480+
let loopback: u32 = 1 << 1;
481+
let unique_local: u32 = 1 << 2;
482+
let global: u32 = 1 << 3;
483+
let unicast_link_local: u32 = 1 << 4;
484+
let unicast_global: u32 = 1 << 7;
485+
let documentation: u32 = 1 << 8;
486+
let benchmarking: u32 = 1 << 16;
487+
let multicast_interface_local: u32 = 1 << 9;
488+
let multicast_link_local: u32 = 1 << 10;
489+
let multicast_realm_local: u32 = 1 << 11;
490+
let multicast_admin_local: u32 = 1 << 12;
491+
let multicast_site_local: u32 = 1 << 13;
492+
let multicast_organization_local: u32 = 1 << 14;
493+
let multicast_global: u32 = 1 << 15;
494+
let multicast: u32 = multicast_interface_local
485495
| multicast_admin_local
486496
| multicast_global
487497
| multicast_link_local
@@ -524,6 +534,11 @@ fn ipv6_properties() {
524534
} else {
525535
assert!(!ip!($s).is_documentation());
526536
}
537+
if ($mask & benchmarking) == benchmarking {
538+
assert!(ip!($s).is_benchmarking());
539+
} else {
540+
assert!(!ip!($s).is_benchmarking());
541+
}
527542
if ($mask & multicast) != 0 {
528543
assert!(ip!($s).multicast_scope().is_some());
529544
assert!(ip!($s).is_multicast());
@@ -562,20 +577,21 @@ fn ipv6_properties() {
562577
}
563578
}
564579

565-
let unspecified: u16 = 1 << 0;
566-
let loopback: u16 = 1 << 1;
567-
let unique_local: u16 = 1 << 2;
568-
let global: u16 = 1 << 3;
569-
let unicast_link_local: u16 = 1 << 4;
570-
let unicast_global: u16 = 1 << 7;
571-
let documentation: u16 = 1 << 8;
572-
let multicast_interface_local: u16 = 1 << 9;
573-
let multicast_link_local: u16 = 1 << 10;
574-
let multicast_realm_local: u16 = 1 << 11;
575-
let multicast_admin_local: u16 = 1 << 12;
576-
let multicast_site_local: u16 = 1 << 13;
577-
let multicast_organization_local: u16 = 1 << 14;
578-
let multicast_global: u16 = 1 << 15;
580+
let unspecified: u32 = 1 << 0;
581+
let loopback: u32 = 1 << 1;
582+
let unique_local: u32 = 1 << 2;
583+
let global: u32 = 1 << 3;
584+
let unicast_link_local: u32 = 1 << 4;
585+
let unicast_global: u32 = 1 << 7;
586+
let documentation: u32 = 1 << 8;
587+
let benchmarking: u32 = 1 << 16;
588+
let multicast_interface_local: u32 = 1 << 9;
589+
let multicast_link_local: u32 = 1 << 10;
590+
let multicast_realm_local: u32 = 1 << 11;
591+
let multicast_admin_local: u32 = 1 << 12;
592+
let multicast_site_local: u32 = 1 << 13;
593+
let multicast_organization_local: u32 = 1 << 14;
594+
let multicast_global: u32 = 1 << 15;
579595

580596
check!("::", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unspecified);
581597

@@ -671,6 +687,12 @@ fn ipv6_properties() {
671687
documentation
672688
);
673689

690+
check!(
691+
"2001:2::ac32:23ff:21",
692+
&[0x20, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0xac, 0x32, 0x23, 0xff, 0, 0x21],
693+
global | unicast_global | benchmarking
694+
);
695+
674696
check!(
675697
"102:304:506:708:90a:b0c:d0e:f10",
676698
&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
@@ -874,6 +896,9 @@ fn ipv6_const() {
874896
const IS_DOCUMENTATION: bool = IP_ADDRESS.is_documentation();
875897
assert!(!IS_DOCUMENTATION);
876898

899+
const IS_BENCHMARKING: bool = IP_ADDRESS.is_benchmarking();
900+
assert!(!IS_BENCHMARKING);
901+
877902
const IS_UNICAST_GLOBAL: bool = IP_ADDRESS.is_unicast_global();
878903
assert!(!IS_UNICAST_GLOBAL);
879904

0 commit comments

Comments
 (0)