Skip to content

Commit 4893b4f

Browse files
authored
Rollup merge of rust-lang#76304 - CDirkx:const-ip, r=ecstatic-morse
Make delegation methods of `std::net::IpAddr` unstably const Make the following methods of `std::net::IpAddr` unstable const under the `const_ip` feature: - `is_unspecified` - `is_loopback` - `is_global` - `is_multicast` Also adds a test for these methods in a const context. Possible because these methods delegate to the inner `Ipv4Addr` or `Ipv6Addr`, which were made const ([PR#76205](rust-lang#76142) and [PR#76206](rust-lang#76206)), and the recent stabilization of const control flow. Part of rust-lang#76205 r? @ecstatic-morse
2 parents c7d68ed + 947536f commit 4893b4f

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@
238238
#![feature(const_cstr_unchecked)]
239239
#![feature(const_fn_transmute)]
240240
#![feature(const_fn)]
241+
#![feature(const_ip)]
241242
#![feature(const_ipv6)]
242243
#![feature(const_raw_ptr_deref)]
243244
#![feature(const_ipv4)]

library/std/src/net/ip.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,9 @@ impl IpAddr {
148148
/// assert_eq!(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)).is_unspecified(), true);
149149
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)).is_unspecified(), true);
150150
/// ```
151+
#[rustc_const_unstable(feature = "const_ip", issue = "76205")]
151152
#[stable(feature = "ip_shared", since = "1.12.0")]
152-
pub fn is_unspecified(&self) -> bool {
153+
pub const fn is_unspecified(&self) -> bool {
153154
match self {
154155
IpAddr::V4(ip) => ip.is_unspecified(),
155156
IpAddr::V6(ip) => ip.is_unspecified(),
@@ -169,8 +170,9 @@ impl IpAddr {
169170
/// assert_eq!(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)).is_loopback(), true);
170171
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1)).is_loopback(), true);
171172
/// ```
173+
#[rustc_const_unstable(feature = "const_ip", issue = "76205")]
172174
#[stable(feature = "ip_shared", since = "1.12.0")]
173-
pub fn is_loopback(&self) -> bool {
175+
pub const fn is_loopback(&self) -> bool {
174176
match self {
175177
IpAddr::V4(ip) => ip.is_loopback(),
176178
IpAddr::V6(ip) => ip.is_loopback(),
@@ -192,7 +194,8 @@ impl IpAddr {
192194
/// assert_eq!(IpAddr::V4(Ipv4Addr::new(80, 9, 12, 3)).is_global(), true);
193195
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0x1c9, 0, 0, 0xafc8, 0, 0x1)).is_global(), true);
194196
/// ```
195-
pub fn is_global(&self) -> bool {
197+
#[rustc_const_unstable(feature = "const_ip", issue = "76205")]
198+
pub const fn is_global(&self) -> bool {
196199
match self {
197200
IpAddr::V4(ip) => ip.is_global(),
198201
IpAddr::V6(ip) => ip.is_global(),
@@ -212,8 +215,9 @@ impl IpAddr {
212215
/// assert_eq!(IpAddr::V4(Ipv4Addr::new(224, 254, 0, 0)).is_multicast(), true);
213216
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0)).is_multicast(), true);
214217
/// ```
218+
#[rustc_const_unstable(feature = "const_ip", issue = "76205")]
215219
#[stable(feature = "ip_shared", since = "1.12.0")]
216-
pub fn is_multicast(&self) -> bool {
220+
pub const fn is_multicast(&self) -> bool {
217221
match self {
218222
IpAddr::V4(ip) => ip.is_multicast(),
219223
IpAddr::V6(ip) => ip.is_multicast(),
@@ -238,7 +242,8 @@ impl IpAddr {
238242
/// true
239243
/// );
240244
/// ```
241-
pub fn is_documentation(&self) -> bool {
245+
#[rustc_const_unstable(feature = "const_ip", issue = "76205")]
246+
pub const fn is_documentation(&self) -> bool {
242247
match self {
243248
IpAddr::V4(ip) => ip.is_documentation(),
244249
IpAddr::V6(ip) => ip.is_documentation(),

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

+19
Original file line numberDiff line numberDiff line change
@@ -918,3 +918,22 @@ fn ipv6_const() {
918918
const IP_V4: Option<Ipv4Addr> = IP_ADDRESS.to_ipv4();
919919
assert_eq!(IP_V4.unwrap(), Ipv4Addr::new(0, 0, 0, 1));
920920
}
921+
922+
#[test]
923+
fn ip_const() {
924+
// test that the methods of `IpAddr` are usable in a const context
925+
926+
const IP_ADDRESS: IpAddr = IpAddr::V4(Ipv4Addr::LOCALHOST);
927+
928+
const IS_UNSPECIFIED: bool = IP_ADDRESS.is_unspecified();
929+
assert!(!IS_UNSPECIFIED);
930+
931+
const IS_LOOPBACK: bool = IP_ADDRESS.is_loopback();
932+
assert!(IS_LOOPBACK);
933+
934+
const IS_GLOBAL: bool = IP_ADDRESS.is_global();
935+
assert!(!IS_GLOBAL);
936+
937+
const IS_MULTICAST: bool = IP_ADDRESS.is_multicast();
938+
assert!(!IS_MULTICAST);
939+
}

0 commit comments

Comments
 (0)