@@ -813,13 +813,41 @@ impl fmt::Display for IpAddr {
813
813
814
814
#[ stable( feature = "ip_from_ip" , since = "1.16.0" ) ]
815
815
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
+ /// ```
816
830
fn from ( ipv4 : Ipv4Addr ) -> IpAddr {
817
831
IpAddr :: V4 ( ipv4)
818
832
}
819
833
}
820
834
821
835
#[ stable( feature = "ip_from_ip" , since = "1.16.0" ) ]
822
836
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
+ /// ```
823
851
fn from ( ipv6 : Ipv6Addr ) -> IpAddr {
824
852
IpAddr :: V6 ( ipv6)
825
853
}
@@ -975,6 +1003,8 @@ impl From<u32> for Ipv4Addr {
975
1003
976
1004
#[ stable( feature = "from_slice_v4" , since = "1.9.0" ) ]
977
1005
impl From < [ u8 ; 4 ] > for Ipv4Addr {
1006
+ /// Creates an `Ipv4Addr` from a four element byte array.
1007
+ ///
978
1008
/// # Examples
979
1009
///
980
1010
/// ```
@@ -1734,6 +1764,27 @@ impl From<u128> for Ipv6Addr {
1734
1764
1735
1765
#[ stable( feature = "ipv6_from_octets" , since = "1.9.0" ) ]
1736
1766
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
+ /// ```
1737
1788
fn from ( octets : [ u8 ; 16 ] ) -> Ipv6Addr {
1738
1789
let inner = c:: in6_addr { s6_addr : octets } ;
1739
1790
Ipv6Addr :: from_inner ( inner)
@@ -1742,6 +1793,27 @@ impl From<[u8; 16]> for Ipv6Addr {
1742
1793
1743
1794
#[ stable( feature = "ipv6_from_segments" , since = "1.16.0" ) ]
1744
1795
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
+ /// ```
1745
1817
fn from ( segments : [ u16 ; 8 ] ) -> Ipv6Addr {
1746
1818
let [ a, b, c, d, e, f, g, h] = segments;
1747
1819
Ipv6Addr :: new ( a, b, c, d, e, f, g, h)
0 commit comments