Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stabilize IpAddr::is_ipv4 and is_ipv6 as const #76226

Merged
merged 2 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions library/std/src/net/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,9 @@ impl IpAddr {
/// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_ipv4(), true);
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_ipv4(), false);
/// ```
#[rustc_const_stable(feature = "const_ip", since = "1.50.0")]
#[stable(feature = "ipaddr_checker", since = "1.16.0")]
pub fn is_ipv4(&self) -> bool {
pub const fn is_ipv4(&self) -> bool {
matches!(self, IpAddr::V4(_))
}

Expand All @@ -281,8 +282,9 @@ impl IpAddr {
/// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_ipv6(), false);
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_ipv6(), true);
/// ```
#[rustc_const_stable(feature = "const_ip", since = "1.50.0")]
#[stable(feature = "ipaddr_checker", since = "1.16.0")]
pub fn is_ipv6(&self) -> bool {
pub const fn is_ipv6(&self) -> bool {
matches!(self, IpAddr::V6(_))
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/ui/consts/std/net/ip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// run-pass

use std::net::{IpAddr, Ipv4Addr};

fn main() {
const IP_ADDRESS : IpAddr = IpAddr::V4(Ipv4Addr::LOCALHOST);

const IS_IP_V4 : bool = IP_ADDRESS.is_ipv4();
assert!(IS_IP_V4);

const IS_IP_V6 : bool = IP_ADDRESS.is_ipv6();
assert!(!IS_IP_V6);
}