Skip to content

Commit 3d1c191

Browse files
committed
Make examples for to_ipv6_compatible, to_ipv6_mapped, to_ipv4 and to_ipv4_mapped more readable
1 parent 5bd7b91 commit 3d1c191

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

library/std/src/net/ip.rs

+30-13
Original file line numberDiff line numberDiff line change
@@ -822,10 +822,10 @@ impl Ipv4Addr {
822822
/// ```
823823
/// use std::net::{Ipv4Addr, Ipv6Addr};
824824
///
825-
/// assert_eq!(
826-
/// Ipv4Addr::new(192, 0, 2, 255).to_ipv6_compatible(),
827-
/// Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc000, 0x2ff)
828-
/// );
825+
/// // ::192.0.2.255
826+
/// let ipv6_compatible = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc000, 0x2ff);
827+
///
828+
/// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).to_ipv6_compatible(), ipv6_compatible);
829829
/// ```
830830
#[rustc_const_stable(feature = "const_ipv4", since = "1.50.0")]
831831
#[stable(feature = "rust1", since = "1.0.0")]
@@ -849,8 +849,10 @@ impl Ipv4Addr {
849849
/// ```
850850
/// use std::net::{Ipv4Addr, Ipv6Addr};
851851
///
852-
/// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).to_ipv6_mapped(),
853-
/// Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc000, 0x2ff));
852+
/// // ::ffff:192.0.2.255
853+
/// let ipv6_mapped = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc000, 0x2ff);
854+
///
855+
/// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).to_ipv6_mapped(), ipv6_mapped);
854856
/// ```
855857
#[rustc_const_stable(feature = "const_ipv4", since = "1.50.0")]
856858
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1599,10 +1601,18 @@ impl Ipv6Addr {
15991601
///
16001602
/// use std::net::{Ipv4Addr, Ipv6Addr};
16011603
///
1604+
/// let ipv4 = Ipv4Addr::new(192, 10, 2, 255);
1605+
/// let ipv6_compatible = ipv4.to_ipv6_compatible();
1606+
/// let ipv6_mapped = ipv4.to_ipv6_mapped();
1607+
///
1608+
/// // Only IPv4-mapped addresses are converted.
1609+
/// assert_eq!(ipv6_compatible.to_ipv4_mapped(), None);
1610+
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc00a, 0x2ff).to_ipv4_mapped(), None);
1611+
/// assert_eq!(ipv6_mapped.to_ipv4_mapped(), Some(ipv4));
1612+
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).to_ipv4_mapped(), Some(ipv4));
1613+
///
1614+
/// // Addresses that are neither an IPv4-compatible or IPv4-mapped address are not converted.
16021615
/// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).to_ipv4_mapped(), None);
1603-
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).to_ipv4_mapped(),
1604-
/// Some(Ipv4Addr::new(192, 10, 2, 255)));
1605-
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4_mapped(), None);
16061616
/// ```
16071617
#[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
16081618
#[unstable(feature = "ip", issue = "27709")]
@@ -1632,11 +1642,18 @@ impl Ipv6Addr {
16321642
/// ```
16331643
/// use std::net::{Ipv4Addr, Ipv6Addr};
16341644
///
1645+
/// let ipv4 = Ipv4Addr::new(192, 10, 2, 255);
1646+
/// let ipv6_compatible = ipv4.to_ipv6_compatible();
1647+
/// let ipv6_mapped = ipv4.to_ipv6_mapped();
1648+
///
1649+
/// // Both IPv4-compatible and IPv4-mapped addresses are converted.
1650+
/// assert_eq!(ipv6_compatible.to_ipv4(), Some(ipv4));
1651+
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc00a, 0x2ff).to_ipv4(), Some(ipv4));
1652+
/// assert_eq!(ipv6_mapped.to_ipv4(), Some(ipv4));
1653+
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).to_ipv4(), Some(ipv4));
1654+
///
1655+
/// // Addresses that are neither an IPv4-compatible or IPv4-mapped address are not converted.
16351656
/// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).to_ipv4(), None);
1636-
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).to_ipv4(),
1637-
/// Some(Ipv4Addr::new(192, 10, 2, 255)));
1638-
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4(),
1639-
/// Some(Ipv4Addr::new(0, 0, 0, 1)));
16401657
/// ```
16411658
#[rustc_const_stable(feature = "const_ipv6", since = "1.50.0")]
16421659
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)