Skip to content

Commit e2d6334

Browse files
committed
Add Ipv6Addr::is_benchmarking
1 parent d192c80 commit e2d6334

File tree

2 files changed

+67
-29
lines changed

2 files changed

+67
-29
lines changed

library/std/src/net/ip.rs

+22
Original file line numberDiff line numberDiff line change
@@ -1370,6 +1370,28 @@ impl Ipv6Addr {
13701370
(self.segments()[0] == 0x2001) && (self.segments()[1] == 0xdb8)
13711371
}
13721372

1373+
/// Returns [`true`] if this is an address reserved for benchmarking (`2001:2::/48`).
1374+
///
1375+
/// This property is defined in [IETF RFC 5180], where it is mistakenly specified as covering the range `2001:0200::/48`.
1376+
/// This is corrected in [IETF RFC Errata 1752] to `2001:0002::/48`.
1377+
///
1378+
/// [IETF RFC 5180]: https://tools.ietf.org/html/rfc5180
1379+
/// [IETF RFC Errata 1752]: https://www.rfc-editor.org/errata_search.php?eid=1752
1380+
///
1381+
/// ```
1382+
/// #![feature(ip)]
1383+
///
1384+
/// use std::net::Ipv6Addr;
1385+
///
1386+
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc613, 0x0).is_benchmarking(), false);
1387+
/// assert_eq!(Ipv6Addr::new(0x2001, 0x2, 0, 0, 0, 0, 0, 0).is_benchmarking(), true);
1388+
/// ```
1389+
#[unstable(feature = "ip", issue = "27709")]
1390+
#[inline]
1391+
pub const fn is_benchmarking(&self) -> bool {
1392+
(self.segments()[0] == 0x2001) && (self.segments()[1] == 0x2) && (self.segments()[2] == 0)
1393+
}
1394+
13731395
/// Returns [`true`] if the address is a globally routable unicast address.
13741396
///
13751397
/// The following return false:

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

+45-29
Original file line numberDiff line numberDiff line change
@@ -475,21 +475,22 @@ fn ipv6_properties() {
475475
assert_eq!(&ip!($s).octets(), octets);
476476
assert_eq!(Ipv6Addr::from(*octets), ip!($s));
477477

478-
let unspecified: u16 = 1 << 0;
479-
let loopback: u16 = 1 << 1;
480-
let unique_local: u16 = 1 << 2;
481-
let global: u16 = 1 << 3;
482-
let unicast_link_local: u16 = 1 << 4;
483-
let unicast_global: u16 = 1 << 7;
484-
let documentation: u16 = 1 << 8;
485-
let multicast_interface_local: u16 = 1 << 9;
486-
let multicast_link_local: u16 = 1 << 10;
487-
let multicast_realm_local: u16 = 1 << 11;
488-
let multicast_admin_local: u16 = 1 << 12;
489-
let multicast_site_local: u16 = 1 << 13;
490-
let multicast_organization_local: u16 = 1 << 14;
491-
let multicast_global: u16 = 1 << 15;
492-
let multicast: u16 = multicast_interface_local
478+
let unspecified: u32 = 1 << 0;
479+
let loopback: u32 = 1 << 1;
480+
let unique_local: u32 = 1 << 2;
481+
let global: u32 = 1 << 3;
482+
let unicast_link_local: u32 = 1 << 4;
483+
let unicast_global: u32 = 1 << 7;
484+
let documentation: u32 = 1 << 8;
485+
let benchmarking: u32 = 1 << 16;
486+
let multicast_interface_local: u32 = 1 << 9;
487+
let multicast_link_local: u32 = 1 << 10;
488+
let multicast_realm_local: u32 = 1 << 11;
489+
let multicast_admin_local: u32 = 1 << 12;
490+
let multicast_site_local: u32 = 1 << 13;
491+
let multicast_organization_local: u32 = 1 << 14;
492+
let multicast_global: u32 = 1 << 15;
493+
let multicast: u32 = multicast_interface_local
493494
| multicast_admin_local
494495
| multicast_global
495496
| multicast_link_local
@@ -532,6 +533,11 @@ fn ipv6_properties() {
532533
} else {
533534
assert!(!ip!($s).is_documentation());
534535
}
536+
if ($mask & benchmarking) == benchmarking {
537+
assert!(ip!($s).is_benchmarking());
538+
} else {
539+
assert!(!ip!($s).is_benchmarking());
540+
}
535541
if ($mask & multicast) != 0 {
536542
assert!(ip!($s).multicast_scope().is_some());
537543
assert!(ip!($s).is_multicast());
@@ -570,20 +576,21 @@ fn ipv6_properties() {
570576
}
571577
}
572578

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

588595
check!("::", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unspecified);
589596

@@ -679,6 +686,12 @@ fn ipv6_properties() {
679686
documentation
680687
);
681688

689+
check!(
690+
"2001:2::ac32:23ff:21",
691+
&[0x20, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0xac, 0x32, 0x23, 0xff, 0, 0x21],
692+
global | unicast_global | benchmarking
693+
);
694+
682695
check!(
683696
"102:304:506:708:90a:b0c:d0e:f10",
684697
&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
@@ -885,6 +898,9 @@ fn ipv6_const() {
885898
const IS_DOCUMENTATION: bool = IP_ADDRESS.is_documentation();
886899
assert!(!IS_DOCUMENTATION);
887900

901+
const IS_BENCHMARKING: bool = IP_ADDRESS.is_benchmarking();
902+
assert!(!IS_BENCHMARKING);
903+
888904
const IS_UNICAST_GLOBAL: bool = IP_ADDRESS.is_unicast_global();
889905
assert!(!IS_UNICAST_GLOBAL);
890906

0 commit comments

Comments
 (0)