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