Skip to content

Commit

Permalink
Rollup merge of #76304 - CDirkx:const-ip, r=ecstatic-morse
Browse files Browse the repository at this point in the history
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](#76142) and [PR#76206](#76206)), and the recent stabilization of const control flow.

Part of #76205

r? @ecstatic-morse
  • Loading branch information
jonas-schievink authored Sep 25, 2020
2 parents 15efed4 + 947536f commit 88e3693
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
1 change: 1 addition & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
15 changes: 10 additions & 5 deletions library/std/src/net/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down
19 changes: 19 additions & 0 deletions library/std/src/net/ip/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 88e3693

Please sign in to comment.