Skip to content

Commit fbb3673

Browse files
author
CDirkx
committed
Make more Ipv4Addr methods const
Constify the following methods of `std::net::Ipv4Addr`: - `octets` - `is_loopback` - `is_private` - `is_link_local` - `is_shared` - `is_ietf_protocol_assignment` - `is_benchmarking` - `is_multicast` - `is_documentation` Also insta-stabilizes these methods as const. Possible because of the stabilization of const integer arithmetic and control flow.
1 parent 36b0d7e commit fbb3673

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

library/std/src/net/ip.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,9 @@ impl Ipv4Addr {
361361
/// let addr = Ipv4Addr::new(127, 0, 0, 1);
362362
/// assert_eq!(addr.octets(), [127, 0, 0, 1]);
363363
/// ```
364+
#[rustc_const_stable(feature = "const_ipv4", since = "1.48.0")]
364365
#[stable(feature = "rust1", since = "1.0.0")]
365-
pub fn octets(&self) -> [u8; 4] {
366+
pub const fn octets(&self) -> [u8; 4] {
366367
// This returns the order we want because s_addr is stored in big-endian.
367368
self.inner.s_addr.to_ne_bytes()
368369
}
@@ -404,8 +405,9 @@ impl Ipv4Addr {
404405
/// assert_eq!(Ipv4Addr::new(127, 0, 0, 1).is_loopback(), true);
405406
/// assert_eq!(Ipv4Addr::new(45, 22, 13, 197).is_loopback(), false);
406407
/// ```
408+
#[rustc_const_stable(feature = "const_ipv4", since = "1.48.0")]
407409
#[stable(since = "1.7.0", feature = "ip_17")]
408-
pub fn is_loopback(&self) -> bool {
410+
pub const fn is_loopback(&self) -> bool {
409411
self.octets()[0] == 127
410412
}
411413

@@ -433,8 +435,9 @@ impl Ipv4Addr {
433435
/// assert_eq!(Ipv4Addr::new(192, 168, 0, 2).is_private(), true);
434436
/// assert_eq!(Ipv4Addr::new(192, 169, 0, 2).is_private(), false);
435437
/// ```
438+
#[rustc_const_stable(feature = "const_ipv4", since = "1.48.0")]
436439
#[stable(since = "1.7.0", feature = "ip_17")]
437-
pub fn is_private(&self) -> bool {
440+
pub const fn is_private(&self) -> bool {
438441
match self.octets() {
439442
[10, ..] => true,
440443
[172, b, ..] if b >= 16 && b <= 31 => true,
@@ -459,8 +462,9 @@ impl Ipv4Addr {
459462
/// assert_eq!(Ipv4Addr::new(169, 254, 10, 65).is_link_local(), true);
460463
/// assert_eq!(Ipv4Addr::new(16, 89, 10, 65).is_link_local(), false);
461464
/// ```
465+
#[rustc_const_stable(feature = "const_ipv4", since = "1.48.0")]
462466
#[stable(since = "1.7.0", feature = "ip_17")]
463-
pub fn is_link_local(&self) -> bool {
467+
pub const fn is_link_local(&self) -> bool {
464468
match self.octets() {
465469
[169, 254, ..] => true,
466470
_ => false,
@@ -573,7 +577,8 @@ impl Ipv4Addr {
573577
/// assert_eq!(Ipv4Addr::new(100, 127, 255, 255).is_shared(), true);
574578
/// assert_eq!(Ipv4Addr::new(100, 128, 0, 0).is_shared(), false);
575579
/// ```
576-
pub fn is_shared(&self) -> bool {
580+
#[rustc_const_stable(feature = "const_ipv4", since = "1.48.0")]
581+
pub const fn is_shared(&self) -> bool {
577582
self.octets()[0] == 100 && (self.octets()[1] & 0b1100_0000 == 0b0100_0000)
578583
}
579584

@@ -605,7 +610,8 @@ impl Ipv4Addr {
605610
/// assert_eq!(Ipv4Addr::new(192, 0, 1, 0).is_ietf_protocol_assignment(), false);
606611
/// assert_eq!(Ipv4Addr::new(191, 255, 255, 255).is_ietf_protocol_assignment(), false);
607612
/// ```
608-
pub fn is_ietf_protocol_assignment(&self) -> bool {
613+
#[rustc_const_stable(feature = "const_ipv4", since = "1.48.0")]
614+
pub const fn is_ietf_protocol_assignment(&self) -> bool {
609615
self.octets()[0] == 192 && self.octets()[1] == 0 && self.octets()[2] == 0
610616
}
611617

@@ -628,7 +634,8 @@ impl Ipv4Addr {
628634
/// assert_eq!(Ipv4Addr::new(198, 19, 255, 255).is_benchmarking(), true);
629635
/// assert_eq!(Ipv4Addr::new(198, 20, 0, 0).is_benchmarking(), false);
630636
/// ```
631-
pub fn is_benchmarking(&self) -> bool {
637+
#[rustc_const_stable(feature = "const_ipv4", since = "1.48.0")]
638+
pub const fn is_benchmarking(&self) -> bool {
632639
self.octets()[0] == 198 && (self.octets()[1] & 0xfe) == 18
633640
}
634641

@@ -681,8 +688,9 @@ impl Ipv4Addr {
681688
/// assert_eq!(Ipv4Addr::new(236, 168, 10, 65).is_multicast(), true);
682689
/// assert_eq!(Ipv4Addr::new(172, 16, 10, 65).is_multicast(), false);
683690
/// ```
691+
#[rustc_const_stable(feature = "const_ipv4", since = "1.48.0")]
684692
#[stable(since = "1.7.0", feature = "ip_17")]
685-
pub fn is_multicast(&self) -> bool {
693+
pub const fn is_multicast(&self) -> bool {
686694
self.octets()[0] >= 224 && self.octets()[0] <= 239
687695
}
688696

@@ -727,8 +735,9 @@ impl Ipv4Addr {
727735
/// assert_eq!(Ipv4Addr::new(203, 0, 113, 6).is_documentation(), true);
728736
/// assert_eq!(Ipv4Addr::new(193, 34, 17, 19).is_documentation(), false);
729737
/// ```
738+
#[rustc_const_stable(feature = "const_ipv4", since = "1.48.0")]
730739
#[stable(since = "1.7.0", feature = "ip_17")]
731-
pub fn is_documentation(&self) -> bool {
740+
pub const fn is_documentation(&self) -> bool {
732741
match self.octets() {
733742
[192, 0, 2, _] => true,
734743
[198, 51, 100, _] => true,

0 commit comments

Comments
 (0)