Skip to content

Commit 0c77257

Browse files
committed
Make all remaining methods of std::net::Ipv4Addr const
Makes the following methods of `std::net::Ipv4Addr` unstable const under the `const_ipv4` feature: - `is_global` - `is_reserved` - `is_broadcast` - `to_ipv6_compatible` - `to_ipv6_mapped` This results in all methods of `Ipv4Addr` being const. Also adds tests for these methods in a const context.
1 parent fb64e6d commit 0c77257

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

library/std/src/net/ip.rs

+20-9
Original file line numberDiff line numberDiff line change
@@ -542,10 +542,13 @@ impl Ipv4Addr {
542542
/// assert_eq!(Ipv4Addr::new(1, 1, 1, 1).is_global(), true);
543543
/// assert_eq!(Ipv4Addr::new(80, 9, 12, 3).is_global(), true);
544544
/// ```
545-
pub fn is_global(&self) -> bool {
545+
#[rustc_const_unstable(feature = "const_ipv4", issue = "76205")]
546+
pub const fn is_global(&self) -> bool {
546547
// check if this address is 192.0.0.9 or 192.0.0.10. These addresses are the only two
547548
// globally routable addresses in the 192.0.0.0/24 range.
548-
if u32::from(*self) == 0xc0000009 || u32::from(*self) == 0xc000000a {
549+
if u32::from_be_bytes(self.octets()) == 0xc0000009
550+
|| u32::from_be_bytes(self.octets()) == 0xc000000a
551+
{
549552
return true;
550553
}
551554
!self.is_private()
@@ -667,7 +670,8 @@ impl Ipv4Addr {
667670
/// // The broadcast address is not considered as reserved for future use by this implementation
668671
/// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_reserved(), false);
669672
/// ```
670-
pub fn is_reserved(&self) -> bool {
673+
#[rustc_const_unstable(feature = "const_ipv4", issue = "76205")]
674+
pub const fn is_reserved(&self) -> bool {
671675
self.octets()[0] & 240 == 240 && !self.is_broadcast()
672676
}
673677

@@ -709,9 +713,10 @@ impl Ipv4Addr {
709713
/// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_broadcast(), true);
710714
/// assert_eq!(Ipv4Addr::new(236, 168, 10, 65).is_broadcast(), false);
711715
/// ```
716+
#[rustc_const_unstable(feature = "const_ipv4", issue = "76205")]
712717
#[stable(since = "1.7.0", feature = "ip_17")]
713-
pub fn is_broadcast(&self) -> bool {
714-
self == &Self::BROADCAST
718+
pub const fn is_broadcast(&self) -> bool {
719+
u32::from_be_bytes(self.octets()) == u32::from_be_bytes(Self::BROADCAST.octets())
715720
}
716721

717722
/// Returns [`true`] if this address is in a range designated for documentation.
@@ -762,10 +767,13 @@ impl Ipv4Addr {
762767
/// Ipv6Addr::new(0, 0, 0, 0, 0, 0, 49152, 767)
763768
/// );
764769
/// ```
770+
#[rustc_const_unstable(feature = "const_ipv4", issue = "76205")]
765771
#[stable(feature = "rust1", since = "1.0.0")]
766-
pub fn to_ipv6_compatible(&self) -> Ipv6Addr {
772+
pub const fn to_ipv6_compatible(&self) -> Ipv6Addr {
767773
let [a, b, c, d] = self.octets();
768-
Ipv6Addr::from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, a, b, c, d])
774+
Ipv6Addr {
775+
inner: c::in6_addr { s6_addr: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, a, b, c, d] },
776+
}
769777
}
770778

771779
/// Converts this address to an IPv4-mapped [`IPv6` address].
@@ -782,10 +790,13 @@ impl Ipv4Addr {
782790
/// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).to_ipv6_mapped(),
783791
/// Ipv6Addr::new(0, 0, 0, 0, 0, 65535, 49152, 767));
784792
/// ```
793+
#[rustc_const_unstable(feature = "const_ipv4", issue = "76205")]
785794
#[stable(feature = "rust1", since = "1.0.0")]
786-
pub fn to_ipv6_mapped(&self) -> Ipv6Addr {
795+
pub const fn to_ipv6_mapped(&self) -> Ipv6Addr {
787796
let [a, b, c, d] = self.octets();
788-
Ipv6Addr::from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, a, b, c, d])
797+
Ipv6Addr {
798+
inner: c::in6_addr { s6_addr: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, a, b, c, d] },
799+
}
789800
}
790801
}
791802

src/test/ui/consts/std/net/ipv4.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![feature(ip)]
44
#![feature(const_ipv4)]
55

6-
use std::net::Ipv4Addr;
6+
use std::net::{Ipv4Addr, Ipv6Addr};
77

88
fn main() {
99
const IP_ADDRESS: Ipv4Addr = Ipv4Addr::new(127, 0, 0, 1);
@@ -24,6 +24,9 @@ fn main() {
2424
const IS_LINK_LOCAL : bool = IP_ADDRESS.is_link_local();
2525
assert!(!IS_LINK_LOCAL);
2626

27+
const IS_GLOBAL : bool = IP_ADDRESS.is_global();
28+
assert!(!IS_GLOBAL);
29+
2730
const IS_SHARED : bool = IP_ADDRESS.is_shared();
2831
assert!(!IS_SHARED);
2932

@@ -33,9 +36,23 @@ fn main() {
3336
const IS_BENCHMARKING : bool = IP_ADDRESS.is_benchmarking();
3437
assert!(!IS_BENCHMARKING);
3538

39+
const IS_RESERVED : bool = IP_ADDRESS.is_reserved();
40+
assert!(!IS_RESERVED);
41+
3642
const IS_MULTICAST : bool = IP_ADDRESS.is_multicast();
3743
assert!(!IS_MULTICAST);
3844

45+
const IS_BROADCAST : bool = IP_ADDRESS.is_broadcast();
46+
assert!(!IS_BROADCAST);
47+
3948
const IS_DOCUMENTATION : bool = IP_ADDRESS.is_documentation();
4049
assert!(!IS_DOCUMENTATION);
50+
51+
const IP_V6_COMPATIBLE : Ipv6Addr = IP_ADDRESS.to_ipv6_compatible();
52+
assert_eq!(IP_V6_COMPATIBLE,
53+
Ipv6Addr::from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 127, 0, 0, 1]));
54+
55+
const IP_V6_MAPPED : Ipv6Addr = IP_ADDRESS.to_ipv6_mapped();
56+
assert_eq!(IP_V6_MAPPED,
57+
Ipv6Addr::from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 127, 0, 0, 1]));
4158
}

0 commit comments

Comments
 (0)