@@ -778,7 +778,16 @@ impl FromInner<c::in_addr> for Ipv4Addr {
778
778
779
779
#[ stable( feature = "ip_u32" , since = "1.1.0" ) ]
780
780
impl From < Ipv4Addr > for u32 {
781
- /// It performs the conversion in network order (big-endian).
781
+ /// Convert an `Ipv4Addr` into a host byte order `u32`.
782
+ ///
783
+ /// # Examples
784
+ ///
785
+ /// ```
786
+ /// use std::net::Ipv4Addr;
787
+ ///
788
+ /// let addr = Ipv4Addr::new(13, 12, 11, 10);
789
+ /// assert_eq!(0x0d0c0b0au32, u32::from(addr));
790
+ /// ```
782
791
fn from ( ip : Ipv4Addr ) -> u32 {
783
792
let ip = ip. octets ( ) ;
784
793
( ( ip[ 0 ] as u32 ) << 24 ) + ( ( ip[ 1 ] as u32 ) << 16 ) + ( ( ip[ 2 ] as u32 ) << 8 ) + ( ip[ 3 ] as u32 )
@@ -787,21 +796,48 @@ impl From<Ipv4Addr> for u32 {
787
796
788
797
#[ stable( feature = "ip_u32" , since = "1.1.0" ) ]
789
798
impl From < u32 > for Ipv4Addr {
790
- /// It performs the conversion in network order (big-endian).
799
+ /// Convert a host byte order `u32` into an `Ipv4Addr`.
800
+ ///
801
+ /// # Examples
802
+ ///
803
+ /// ```
804
+ /// use std::net::Ipv4Addr;
805
+ ///
806
+ /// let addr = Ipv4Addr::from(0x0d0c0b0au32);
807
+ /// assert_eq!(Ipv4Addr::new(13, 12, 11, 10), addr);
808
+ /// ```
791
809
fn from ( ip : u32 ) -> Ipv4Addr {
792
810
Ipv4Addr :: new ( ( ip >> 24 ) as u8 , ( ip >> 16 ) as u8 , ( ip >> 8 ) as u8 , ip as u8 )
793
811
}
794
812
}
795
813
796
814
#[ stable( feature = "from_slice_v4" , since = "1.9.0" ) ]
797
815
impl From < [ u8 ; 4 ] > for Ipv4Addr {
816
+ /// # Examples
817
+ ///
818
+ /// ```
819
+ /// use std::net::Ipv4Addr;
820
+ ///
821
+ /// let addr = Ipv4Addr::from([13u8, 12u8, 11u8, 10u8]);
822
+ /// assert_eq!(Ipv4Addr::new(13, 12, 11, 10), addr);
823
+ /// ```
798
824
fn from ( octets : [ u8 ; 4 ] ) -> Ipv4Addr {
799
825
Ipv4Addr :: new ( octets[ 0 ] , octets[ 1 ] , octets[ 2 ] , octets[ 3 ] )
800
826
}
801
827
}
802
828
803
829
#[ stable( feature = "ip_from_slice" , since = "1.17.0" ) ]
804
830
impl From < [ u8 ; 4 ] > for IpAddr {
831
+ /// Create an `IpAddr::V4` from a four element byte array.
832
+ ///
833
+ /// # Examples
834
+ ///
835
+ /// ```
836
+ /// use std::net::{IpAddr, Ipv4Addr};
837
+ ///
838
+ /// let addr = IpAddr::from([13u8, 12u8, 11u8, 10u8]);
839
+ /// assert_eq!(IpAddr::V4(Ipv4Addr::new(13, 12, 11, 10)), addr);
840
+ /// ```
805
841
fn from ( octets : [ u8 ; 4 ] ) -> IpAddr {
806
842
IpAddr :: V4 ( Ipv4Addr :: from ( octets) )
807
843
}
@@ -1395,13 +1431,55 @@ impl From<[u16; 8]> for Ipv6Addr {
1395
1431
1396
1432
#[ stable( feature = "ip_from_slice" , since = "1.17.0" ) ]
1397
1433
impl From < [ u8 ; 16 ] > for IpAddr {
1434
+ /// Create an `IpAddr::V6` from a sixteen element byte array.
1435
+ ///
1436
+ /// # Examples
1437
+ ///
1438
+ /// ```
1439
+ /// use std::net::{IpAddr, Ipv6Addr};
1440
+ ///
1441
+ /// let addr = IpAddr::from([
1442
+ /// 25u8, 24u8, 23u8, 22u8, 21u8, 20u8, 19u8, 18u8,
1443
+ /// 17u8, 16u8, 15u8, 14u8, 13u8, 12u8, 11u8, 10u8,
1444
+ /// ]);
1445
+ /// assert_eq!(
1446
+ /// IpAddr::V6(Ipv6Addr::new(
1447
+ /// 0x1918, 0x1716,
1448
+ /// 0x1514, 0x1312,
1449
+ /// 0x1110, 0x0f0e,
1450
+ /// 0x0d0c, 0x0b0a
1451
+ /// )),
1452
+ /// addr
1453
+ /// );
1454
+ /// ```
1398
1455
fn from ( octets : [ u8 ; 16 ] ) -> IpAddr {
1399
1456
IpAddr :: V6 ( Ipv6Addr :: from ( octets) )
1400
1457
}
1401
1458
}
1402
1459
1403
1460
#[ stable( feature = "ip_from_slice" , since = "1.17.0" ) ]
1404
1461
impl From < [ u16 ; 8 ] > for IpAddr {
1462
+ /// Create an `IpAddr::V6` from an eight element 16-bit array.
1463
+ ///
1464
+ /// # Examples
1465
+ ///
1466
+ /// ```
1467
+ /// use std::net::{IpAddr, Ipv6Addr};
1468
+ ///
1469
+ /// let addr = IpAddr::from([
1470
+ /// 525u16, 524u16, 523u16, 522u16,
1471
+ /// 521u16, 520u16, 519u16, 518u16,
1472
+ /// ]);
1473
+ /// assert_eq!(
1474
+ /// IpAddr::V6(Ipv6Addr::new(
1475
+ /// 0x20d, 0x20c,
1476
+ /// 0x20b, 0x20a,
1477
+ /// 0x209, 0x208,
1478
+ /// 0x207, 0x206
1479
+ /// )),
1480
+ /// addr
1481
+ /// );
1482
+ /// ```
1405
1483
fn from ( segments : [ u16 ; 8 ] ) -> IpAddr {
1406
1484
IpAddr :: V6 ( Ipv6Addr :: from ( segments) )
1407
1485
}
0 commit comments