Make delegation methods of std::net::IpAddr unstable 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, and the recent stabilization of const control flow.

Part of #76205
This commit is contained in:
Christiaan Dirkx 2020-09-07 17:43:48 +02:00 committed by Christiaan
parent a6008fac97
commit 947536fca0
3 changed files with 30 additions and 5 deletions

View File

@ -238,6 +238,7 @@
#![feature(const_cstr_unchecked)]
#![feature(const_fn_transmute)]
#![feature(const_fn)]
#![feature(const_ip)]
#![feature(const_ipv6)]
#![feature(const_raw_ptr_deref)]
#![feature(const_ipv4)]

View File

@ -148,8 +148,9 @@ impl IpAddr {
/// assert_eq!(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)).is_unspecified(), true);
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)).is_unspecified(), true);
/// ```
#[rustc_const_unstable(feature = "const_ip", issue = "76205")]
#[stable(feature = "ip_shared", since = "1.12.0")]
pub fn is_unspecified(&self) -> bool {
pub const fn is_unspecified(&self) -> bool {
match self {
IpAddr::V4(ip) => ip.is_unspecified(),
IpAddr::V6(ip) => ip.is_unspecified(),
@ -169,8 +170,9 @@ impl IpAddr {
/// assert_eq!(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)).is_loopback(), true);
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1)).is_loopback(), true);
/// ```
#[rustc_const_unstable(feature = "const_ip", issue = "76205")]
#[stable(feature = "ip_shared", since = "1.12.0")]
pub fn is_loopback(&self) -> bool {
pub const fn is_loopback(&self) -> bool {
match self {
IpAddr::V4(ip) => ip.is_loopback(),
IpAddr::V6(ip) => ip.is_loopback(),
@ -192,7 +194,8 @@ impl IpAddr {
/// assert_eq!(IpAddr::V4(Ipv4Addr::new(80, 9, 12, 3)).is_global(), true);
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0x1c9, 0, 0, 0xafc8, 0, 0x1)).is_global(), true);
/// ```
pub fn is_global(&self) -> bool {
#[rustc_const_unstable(feature = "const_ip", issue = "76205")]
pub const fn is_global(&self) -> bool {
match self {
IpAddr::V4(ip) => ip.is_global(),
IpAddr::V6(ip) => ip.is_global(),
@ -212,8 +215,9 @@ impl IpAddr {
/// assert_eq!(IpAddr::V4(Ipv4Addr::new(224, 254, 0, 0)).is_multicast(), true);
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0)).is_multicast(), true);
/// ```
#[rustc_const_unstable(feature = "const_ip", issue = "76205")]
#[stable(feature = "ip_shared", since = "1.12.0")]
pub fn is_multicast(&self) -> bool {
pub const fn is_multicast(&self) -> bool {
match self {
IpAddr::V4(ip) => ip.is_multicast(),
IpAddr::V6(ip) => ip.is_multicast(),
@ -238,7 +242,8 @@ impl IpAddr {
/// true
/// );
/// ```
pub fn is_documentation(&self) -> bool {
#[rustc_const_unstable(feature = "const_ip", issue = "76205")]
pub const fn is_documentation(&self) -> bool {
match self {
IpAddr::V4(ip) => ip.is_documentation(),
IpAddr::V6(ip) => ip.is_documentation(),

View File

@ -918,3 +918,22 @@ fn ipv6_const() {
const IP_V4: Option<Ipv4Addr> = IP_ADDRESS.to_ipv4();
assert_eq!(IP_V4.unwrap(), Ipv4Addr::new(0, 0, 0, 1));
}
#[test]
fn ip_const() {
// test that the methods of `IpAddr` are usable in a const context
const IP_ADDRESS: IpAddr = IpAddr::V4(Ipv4Addr::LOCALHOST);
const IS_UNSPECIFIED: bool = IP_ADDRESS.is_unspecified();
assert!(!IS_UNSPECIFIED);
const IS_LOOPBACK: bool = IP_ADDRESS.is_loopback();
assert!(IS_LOOPBACK);
const IS_GLOBAL: bool = IP_ADDRESS.is_global();
assert!(!IS_GLOBAL);
const IS_MULTICAST: bool = IP_ADDRESS.is_multicast();
assert!(!IS_MULTICAST);
}