Skip to content

Commit 1823a87

Browse files
committed
Auto merge of rust-lang#76226 - CDirkx:const-ipaddr, r=dtolnay
Stabilize `IpAddr::is_ipv4` and `is_ipv6` as const Insta-stabilize the methods `is_ipv4` and `is_ipv6` of `std::net::IpAddr` as const, in the same way as [PR#76198](rust-lang#76198). Possible because of the recent stabilization of const control flow. Part of rust-lang#76225 and rust-lang#76205.
2 parents f32459c + 4613bc9 commit 1823a87

File tree

2 files changed

+17
-2
lines changed
  • library/std/src/net
  • src/test/ui/consts/std/net

2 files changed

+17
-2
lines changed

library/std/src/net/ip.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,9 @@ impl IpAddr {
263263
/// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_ipv4(), true);
264264
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_ipv4(), false);
265265
/// ```
266+
#[rustc_const_stable(feature = "const_ip", since = "1.50.0")]
266267
#[stable(feature = "ipaddr_checker", since = "1.16.0")]
267-
pub fn is_ipv4(&self) -> bool {
268+
pub const fn is_ipv4(&self) -> bool {
268269
matches!(self, IpAddr::V4(_))
269270
}
270271

@@ -281,8 +282,9 @@ impl IpAddr {
281282
/// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_ipv6(), false);
282283
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_ipv6(), true);
283284
/// ```
285+
#[rustc_const_stable(feature = "const_ip", since = "1.50.0")]
284286
#[stable(feature = "ipaddr_checker", since = "1.16.0")]
285-
pub fn is_ipv6(&self) -> bool {
287+
pub const fn is_ipv6(&self) -> bool {
286288
matches!(self, IpAddr::V6(_))
287289
}
288290
}

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

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// run-pass
2+
3+
use std::net::{IpAddr, Ipv4Addr};
4+
5+
fn main() {
6+
const IP_ADDRESS : IpAddr = IpAddr::V4(Ipv4Addr::LOCALHOST);
7+
8+
const IS_IP_V4 : bool = IP_ADDRESS.is_ipv4();
9+
assert!(IS_IP_V4);
10+
11+
const IS_IP_V6 : bool = IP_ADDRESS.is_ipv6();
12+
assert!(!IS_IP_V6);
13+
}

0 commit comments

Comments
 (0)