Skip to content

Commit

Permalink
Stabilize {IpAddr, Ipv6Addr}::to_canonical
Browse files Browse the repository at this point in the history
Make `IpAddr::to_canonical` and `IpV6Addr::to_canonical` stable, as well as
const stabilize `Ipv6Addr::to_ipv4_mapped`.

Newly stable API:

    impl IpAddr {
        // Now stable under `ip_to_canonical`
        const fn to_canonical(&self) -> IpAddr;
    }

    impl Ipv6Addr {
        // Now stable under `ip_to_canonical`
        const fn to_canonical(&self) -> IpAddr;

        // Already stable, this makes it const stable under
        // `const_ipv6_to_ipv4_mapped
        const fn to_ipv4_mapped(&self) -> Option<Ipv4Addr>
    }

These stabilize a subset of the following tracking issues:

- #27709
- #76205
  • Loading branch information
tgross35 committed Sep 19, 2023
1 parent 65ea825 commit de32e4d
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions library/core/src/net/ip_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,21 +410,24 @@ impl IpAddr {
/// # Examples
///
/// ```
/// #![feature(ip)]
/// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
///
/// let localhost_v4 = Ipv4Addr::new(127, 0, 0, 1);
///
/// assert_eq!(IpAddr::V4(localhost_v4).to_canonical(), localhost_v4);
/// assert_eq!(IpAddr::V6(localhost_v4.to_ipv6_mapped()).to_canonical(), localhost_v4);
/// assert_eq!(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)).to_canonical().is_loopback(), true);
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1)).is_loopback(), false);
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1)).to_canonical().is_loopback(), true);
/// ```
#[inline]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[rustc_const_unstable(feature = "const_ip", issue = "76205")]
#[unstable(feature = "ip", issue = "27709")]
#[stable(feature = "ip_to_canonical", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "ip_to_canonical", since = "CURRENT_RUSTC_VERSION")]
pub const fn to_canonical(&self) -> IpAddr {
match self {
&v4 @ IpAddr::V4(_) => v4,
IpAddr::V4(_) => *self,
IpAddr::V6(v6) => v6.to_canonical(),
}
}
Expand Down Expand Up @@ -1748,11 +1751,11 @@ impl Ipv6Addr {
/// Some(Ipv4Addr::new(192, 10, 2, 255)));
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4_mapped(), None);
/// ```
#[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
#[stable(feature = "ipv6_to_ipv4_mapped", since = "1.63.0")]
#[inline]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[stable(feature = "ipv6_to_ipv4_mapped", since = "1.63.0")]
#[rustc_const_stable(feature = "const_ipv6_to_ipv4_mapped", since = "CURRENT_RUSTC_VERSION")]
pub const fn to_ipv4_mapped(&self) -> Option<Ipv4Addr> {
match self.octets() {
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, a, b, c, d] => {
Expand Down Expand Up @@ -1817,11 +1820,11 @@ impl Ipv6Addr {
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1).is_loopback(), false);
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1).to_canonical().is_loopback(), true);
/// ```
#[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
#[unstable(feature = "ip", issue = "27709")]
#[inline]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[stable(feature = "ip_to_canonical", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "ip_to_canonical", since = "CURRENT_RUSTC_VERSION")]
pub const fn to_canonical(&self) -> IpAddr {
if let Some(mapped) = self.to_ipv4_mapped() {
return IpAddr::V4(mapped);
Expand Down

0 comments on commit de32e4d

Please sign in to comment.