Skip to content

Commit 17113f7

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 1de910f + 7381f9d commit 17113f7

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
@@ -412,21 +412,24 @@ impl IpAddr {
412412
/// # Examples
413413
///
414414
/// ```
415-
/// #![feature(ip)]
416415
/// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
417416
///
417+
/// let localhost_v4 = Ipv4Addr::new(127, 0, 0, 1);
418+
///
419+
/// assert_eq!(IpAddr::V4(localhost_v4).to_canonical(), localhost_v4);
420+
/// assert_eq!(IpAddr::V6(localhost_v4.to_ipv6_mapped()).to_canonical(), localhost_v4);
418421
/// assert_eq!(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)).to_canonical().is_loopback(), true);
419422
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1)).is_loopback(), false);
420423
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1)).to_canonical().is_loopback(), true);
421424
/// ```
422425
#[inline]
423426
#[must_use = "this returns the result of the operation, \
424427
without modifying the original"]
425-
#[rustc_const_unstable(feature = "const_ip", issue = "76205")]
426-
#[unstable(feature = "ip", issue = "27709")]
428+
#[stable(feature = "ip_to_canonical", since = "CURRENT_RUSTC_VERSION")]
429+
#[rustc_const_stable(feature = "ip_to_canonical", since = "CURRENT_RUSTC_VERSION")]
427430
pub const fn to_canonical(&self) -> IpAddr {
428431
match self {
429-
&v4 @ IpAddr::V4(_) => v4,
432+
IpAddr::V4(_) => *self,
430433
IpAddr::V6(v6) => v6.to_canonical(),
431434
}
432435
}
@@ -1750,11 +1753,11 @@ impl Ipv6Addr {
17501753
/// Some(Ipv4Addr::new(192, 10, 2, 255)));
17511754
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4_mapped(), None);
17521755
/// ```
1753-
#[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1754-
#[stable(feature = "ipv6_to_ipv4_mapped", since = "1.63.0")]
1756+
#[inline]
17551757
#[must_use = "this returns the result of the operation, \
17561758
without modifying the original"]
1757-
#[inline]
1759+
#[stable(feature = "ipv6_to_ipv4_mapped", since = "1.63.0")]
1760+
#[rustc_const_stable(feature = "const_ipv6_to_ipv4_mapped", since = "CURRENT_RUSTC_VERSION")]
17581761
pub const fn to_ipv4_mapped(&self) -> Option<Ipv4Addr> {
17591762
match self.octets() {
17601763
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, a, b, c, d] => {
@@ -1819,11 +1822,11 @@ impl Ipv6Addr {
18191822
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1).is_loopback(), false);
18201823
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1).to_canonical().is_loopback(), true);
18211824
/// ```
1822-
#[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1823-
#[unstable(feature = "ip", issue = "27709")]
1825+
#[inline]
18241826
#[must_use = "this returns the result of the operation, \
18251827
without modifying the original"]
1826-
#[inline]
1828+
#[stable(feature = "ip_to_canonical", since = "CURRENT_RUSTC_VERSION")]
1829+
#[rustc_const_stable(feature = "ip_to_canonical", since = "CURRENT_RUSTC_VERSION")]
18271830
pub const fn to_canonical(&self) -> IpAddr {
18281831
if let Some(mapped) = self.to_ipv4_mapped() {
18291832
return IpAddr::V4(mapped);

0 commit comments

Comments
 (0)