Stabilize {IpAddr, Ipv6Addr}::to_canonical

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:

- https://github.com/rust-lang/rust/issues/27709
- https://github.com/rust-lang/rust/issues/76205
This commit is contained in:
Trevor Gross 2023-09-18 20:51:58 -04:00
parent 65ea825f40
commit 7381f9d6dc

View File

@ -410,9 +410,12 @@ impl IpAddr {
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// #![feature(ip)]
/// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; /// 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::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)).is_loopback(), false);
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1)).to_canonical().is_loopback(), true); /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1)).to_canonical().is_loopback(), true);
@ -420,11 +423,11 @@ impl IpAddr {
#[inline] #[inline]
#[must_use = "this returns the result of the operation, \ #[must_use = "this returns the result of the operation, \
without modifying the original"] without modifying the original"]
#[rustc_const_unstable(feature = "const_ip", issue = "76205")] #[stable(feature = "ip_to_canonical", since = "CURRENT_RUSTC_VERSION")]
#[unstable(feature = "ip", issue = "27709")] #[rustc_const_stable(feature = "ip_to_canonical", since = "CURRENT_RUSTC_VERSION")]
pub const fn to_canonical(&self) -> IpAddr { pub const fn to_canonical(&self) -> IpAddr {
match self { match self {
&v4 @ IpAddr::V4(_) => v4, IpAddr::V4(_) => *self,
IpAddr::V6(v6) => v6.to_canonical(), IpAddr::V6(v6) => v6.to_canonical(),
} }
} }
@ -1748,11 +1751,11 @@ impl Ipv6Addr {
/// Some(Ipv4Addr::new(192, 10, 2, 255))); /// Some(Ipv4Addr::new(192, 10, 2, 255)));
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4_mapped(), None); /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4_mapped(), None);
/// ``` /// ```
#[rustc_const_unstable(feature = "const_ipv6", issue = "76205")] #[inline]
#[stable(feature = "ipv6_to_ipv4_mapped", since = "1.63.0")]
#[must_use = "this returns the result of the operation, \ #[must_use = "this returns the result of the operation, \
without modifying the original"] 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> { pub const fn to_ipv4_mapped(&self) -> Option<Ipv4Addr> {
match self.octets() { match self.octets() {
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, a, b, c, d] => { [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, a, b, c, d] => {
@ -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).is_loopback(), false);
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1).to_canonical().is_loopback(), true); /// 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")] #[inline]
#[unstable(feature = "ip", issue = "27709")]
#[must_use = "this returns the result of the operation, \ #[must_use = "this returns the result of the operation, \
without modifying the original"] 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 { pub const fn to_canonical(&self) -> IpAddr {
if let Some(mapped) = self.to_ipv4_mapped() { if let Some(mapped) = self.to_ipv4_mapped() {
return IpAddr::V4(mapped); return IpAddr::V4(mapped);