From 3f8fdf83ff2182938b29b93cbf92aec62e1f96cd Mon Sep 17 00:00:00 2001 From: Christiaan Dirkx Date: Wed, 2 Sep 2020 02:05:42 +0200 Subject: [PATCH 1/2] Stabilize `IpAddr::is_ipv4` and `is_ipv6` as const Insta-stabilize the methods `is_ipv4` and `is_ipv6` of `IpAddr`. Possible because of the recent stabilization of const control flow. Also adds a test for these methods in a const context. --- library/std/src/net/ip.rs | 6 ++++-- src/test/ui/consts/std/net/ip.rs | 13 +++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 src/test/ui/consts/std/net/ip.rs diff --git a/library/std/src/net/ip.rs b/library/std/src/net/ip.rs index bb3ece4c2739f..714ff78777c26 100644 --- a/library/std/src/net/ip.rs +++ b/library/std/src/net/ip.rs @@ -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.48.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(_)) } @@ -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.48.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(_)) } } diff --git a/src/test/ui/consts/std/net/ip.rs b/src/test/ui/consts/std/net/ip.rs new file mode 100644 index 0000000000000..6730946577d51 --- /dev/null +++ b/src/test/ui/consts/std/net/ip.rs @@ -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); +} From 4613bc96a4dd8b492623aac79d91314041d17af0 Mon Sep 17 00:00:00 2001 From: Christiaan Dirkx Date: Mon, 23 Nov 2020 01:40:26 +0100 Subject: [PATCH 2/2] Bump version to 1.50.0 --- library/std/src/net/ip.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/std/src/net/ip.rs b/library/std/src/net/ip.rs index 714ff78777c26..1bd80cf29a8df 100644 --- a/library/std/src/net/ip.rs +++ b/library/std/src/net/ip.rs @@ -263,7 +263,7 @@ 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.48.0")] + #[rustc_const_stable(feature = "const_ip", since = "1.50.0")] #[stable(feature = "ipaddr_checker", since = "1.16.0")] pub const fn is_ipv4(&self) -> bool { matches!(self, IpAddr::V4(_)) @@ -282,7 +282,7 @@ 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.48.0")] + #[rustc_const_stable(feature = "const_ip", since = "1.50.0")] #[stable(feature = "ipaddr_checker", since = "1.16.0")] pub const fn is_ipv6(&self) -> bool { matches!(self, IpAddr::V6(_))