Skip to content

Commit 3df1ff2

Browse files
authored
Rollup merge of #68640 - LeSeulArtichaut:doc-from-ip, r=steveklabnik
Document remaining undocumented `From` implementations for IPs Some `From` implementations were missing documentation. I added examples, I tried to be as close to existing examples as possible. Related to #51430 (cc @skade) r? @steveklabnik
2 parents 4af33a9 + ab5e296 commit 3df1ff2

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

src/libstd/net/ip.rs

+72
Original file line numberDiff line numberDiff line change
@@ -813,13 +813,41 @@ impl fmt::Display for IpAddr {
813813

814814
#[stable(feature = "ip_from_ip", since = "1.16.0")]
815815
impl From<Ipv4Addr> for IpAddr {
816+
/// Copies this address to a new `IpAddr::V4`.
817+
///
818+
/// # Examples
819+
///
820+
/// ```
821+
/// use std::net::{IpAddr, Ipv4Addr};
822+
///
823+
/// let addr = Ipv4Addr::new(127, 0, 0, 1);
824+
///
825+
/// assert_eq!(
826+
/// IpAddr::V4(addr),
827+
/// IpAddr::from(addr)
828+
/// )
829+
/// ```
816830
fn from(ipv4: Ipv4Addr) -> IpAddr {
817831
IpAddr::V4(ipv4)
818832
}
819833
}
820834

821835
#[stable(feature = "ip_from_ip", since = "1.16.0")]
822836
impl From<Ipv6Addr> for IpAddr {
837+
/// Copies this address to a new `IpAddr::V6`.
838+
///
839+
/// # Examples
840+
///
841+
/// ```
842+
/// use std::net::{IpAddr, Ipv6Addr};
843+
///
844+
/// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff);
845+
///
846+
/// assert_eq!(
847+
/// IpAddr::V6(addr),
848+
/// IpAddr::from(addr)
849+
/// );
850+
/// ```
823851
fn from(ipv6: Ipv6Addr) -> IpAddr {
824852
IpAddr::V6(ipv6)
825853
}
@@ -975,6 +1003,8 @@ impl From<u32> for Ipv4Addr {
9751003

9761004
#[stable(feature = "from_slice_v4", since = "1.9.0")]
9771005
impl From<[u8; 4]> for Ipv4Addr {
1006+
/// Creates an `Ipv4Addr` from a four element byte array.
1007+
///
9781008
/// # Examples
9791009
///
9801010
/// ```
@@ -1734,6 +1764,27 @@ impl From<u128> for Ipv6Addr {
17341764

17351765
#[stable(feature = "ipv6_from_octets", since = "1.9.0")]
17361766
impl From<[u8; 16]> for Ipv6Addr {
1767+
/// Creates an `Ipv6Addr` from a sixteen element byte array.
1768+
///
1769+
/// # Examples
1770+
///
1771+
/// ```
1772+
/// use std::net::Ipv6Addr;
1773+
///
1774+
/// let addr = Ipv6Addr::from([
1775+
/// 25u8, 24u8, 23u8, 22u8, 21u8, 20u8, 19u8, 18u8,
1776+
/// 17u8, 16u8, 15u8, 14u8, 13u8, 12u8, 11u8, 10u8,
1777+
/// ]);
1778+
/// assert_eq!(
1779+
/// Ipv6Addr::new(
1780+
/// 0x1918, 0x1716,
1781+
/// 0x1514, 0x1312,
1782+
/// 0x1110, 0x0f0e,
1783+
/// 0x0d0c, 0x0b0a
1784+
/// ),
1785+
/// addr
1786+
/// );
1787+
/// ```
17371788
fn from(octets: [u8; 16]) -> Ipv6Addr {
17381789
let inner = c::in6_addr { s6_addr: octets };
17391790
Ipv6Addr::from_inner(inner)
@@ -1742,6 +1793,27 @@ impl From<[u8; 16]> for Ipv6Addr {
17421793

17431794
#[stable(feature = "ipv6_from_segments", since = "1.16.0")]
17441795
impl From<[u16; 8]> for Ipv6Addr {
1796+
/// Creates an `Ipv6Addr` from an eight element 16-bit array.
1797+
///
1798+
/// # Examples
1799+
///
1800+
/// ```
1801+
/// use std::net::Ipv6Addr;
1802+
///
1803+
/// let addr = Ipv6Addr::from([
1804+
/// 525u16, 524u16, 523u16, 522u16,
1805+
/// 521u16, 520u16, 519u16, 518u16,
1806+
/// ]);
1807+
/// assert_eq!(
1808+
/// Ipv6Addr::new(
1809+
/// 0x20d, 0x20c,
1810+
/// 0x20b, 0x20a,
1811+
/// 0x209, 0x208,
1812+
/// 0x207, 0x206
1813+
/// ),
1814+
/// addr
1815+
/// );
1816+
/// ```
17451817
fn from(segments: [u16; 8]) -> Ipv6Addr {
17461818
let [a, b, c, d, e, f, g, h] = segments;
17471819
Ipv6Addr::new(a, b, c, d, e, f, g, h)

0 commit comments

Comments
 (0)