Skip to content

Commit 0e5cf4f

Browse files
authored
Rollup merge of rust-lang#115955 - tgross35:ip-to-canonical, r=dtolnay
Stabilize `{IpAddr, Ipv6Addr}::to_canonical` Make `IpAddr::to_canonical` and `IpV6Addr::to_canonical` stable (+const), as well as const stabilize `Ipv6Addr::to_ipv4_mapped`. Newly stable API: ```rust impl IpAddr { // Newly stable under `ip_to_canonical` const fn to_canonical(&self) -> IpAddr; } impl Ipv6Addr { // Newly 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: - rust-lang#27709 - rust-lang#76205 Stabilization of all methods under the `ip` gate was attempted once at rust-lang#66584 then again at rust-lang#76098. These were not successful because there are still unknowns about `is_documentation` `is_benchmarking` and similar; `to_canonical` is much more straightforward. I have looked and could not find any known issues with `to_canonical`. These were added in 2021 in rust-lang#87708 cc implementor `@the8472` r? libs-api `@rustbot` label +T-libs-api +needs-fcp
2 parents d60d63f + 7381f9d commit 0e5cf4f

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

library/core/src/net/ip_addr.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -410,21 +410,24 @@ impl IpAddr {
410410
/// # Examples
411411
///
412412
/// ```
413-
/// #![feature(ip)]
414413
/// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
415414
///
415+
/// let localhost_v4 = Ipv4Addr::new(127, 0, 0, 1);
416+
///
417+
/// assert_eq!(IpAddr::V4(localhost_v4).to_canonical(), localhost_v4);
418+
/// assert_eq!(IpAddr::V6(localhost_v4.to_ipv6_mapped()).to_canonical(), localhost_v4);
416419
/// assert_eq!(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)).to_canonical().is_loopback(), true);
417420
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1)).is_loopback(), false);
418421
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1)).to_canonical().is_loopback(), true);
419422
/// ```
420423
#[inline]
421424
#[must_use = "this returns the result of the operation, \
422425
without modifying the original"]
423-
#[rustc_const_unstable(feature = "const_ip", issue = "76205")]
424-
#[unstable(feature = "ip", issue = "27709")]
426+
#[stable(feature = "ip_to_canonical", since = "CURRENT_RUSTC_VERSION")]
427+
#[rustc_const_stable(feature = "ip_to_canonical", since = "CURRENT_RUSTC_VERSION")]
425428
pub const fn to_canonical(&self) -> IpAddr {
426429
match self {
427-
&v4 @ IpAddr::V4(_) => v4,
430+
IpAddr::V4(_) => *self,
428431
IpAddr::V6(v6) => v6.to_canonical(),
429432
}
430433
}
@@ -1748,11 +1751,11 @@ impl Ipv6Addr {
17481751
/// Some(Ipv4Addr::new(192, 10, 2, 255)));
17491752
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4_mapped(), None);
17501753
/// ```
1751-
#[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1752-
#[stable(feature = "ipv6_to_ipv4_mapped", since = "1.63.0")]
1754+
#[inline]
17531755
#[must_use = "this returns the result of the operation, \
17541756
without modifying the original"]
1755-
#[inline]
1757+
#[stable(feature = "ipv6_to_ipv4_mapped", since = "1.63.0")]
1758+
#[rustc_const_stable(feature = "const_ipv6_to_ipv4_mapped", since = "CURRENT_RUSTC_VERSION")]
17561759
pub const fn to_ipv4_mapped(&self) -> Option<Ipv4Addr> {
17571760
match self.octets() {
17581761
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, a, b, c, d] => {
@@ -1817,11 +1820,11 @@ impl Ipv6Addr {
18171820
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1).is_loopback(), false);
18181821
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1).to_canonical().is_loopback(), true);
18191822
/// ```
1820-
#[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1821-
#[unstable(feature = "ip", issue = "27709")]
1823+
#[inline]
18221824
#[must_use = "this returns the result of the operation, \
18231825
without modifying the original"]
1824-
#[inline]
1826+
#[stable(feature = "ip_to_canonical", since = "CURRENT_RUSTC_VERSION")]
1827+
#[rustc_const_stable(feature = "ip_to_canonical", since = "CURRENT_RUSTC_VERSION")]
18251828
pub const fn to_canonical(&self) -> IpAddr {
18261829
if let Some(mapped) = self.to_ipv4_mapped() {
18271830
return IpAddr::V4(mapped);

0 commit comments

Comments
 (0)