Skip to content

Commit cb1f898

Browse files
committed
Auto merge of #49418 - frewsxcv:frewsxcv-network-order, r=TimNN
Clarify network byte order conversions for integer / IP address conversions. Opened primarily to address #48819. Also added a few other conversion docs/examples.
2 parents 85f0098 + b89fb71 commit cb1f898

File tree

1 file changed

+80
-2
lines changed

1 file changed

+80
-2
lines changed

src/libstd/net/ip.rs

+80-2
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,16 @@ impl FromInner<c::in_addr> for Ipv4Addr {
778778

779779
#[stable(feature = "ip_u32", since = "1.1.0")]
780780
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+
/// ```
782791
fn from(ip: Ipv4Addr) -> u32 {
783792
let ip = ip.octets();
784793
((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 {
787796

788797
#[stable(feature = "ip_u32", since = "1.1.0")]
789798
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+
/// ```
791809
fn from(ip: u32) -> Ipv4Addr {
792810
Ipv4Addr::new((ip >> 24) as u8, (ip >> 16) as u8, (ip >> 8) as u8, ip as u8)
793811
}
794812
}
795813

796814
#[stable(feature = "from_slice_v4", since = "1.9.0")]
797815
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+
/// ```
798824
fn from(octets: [u8; 4]) -> Ipv4Addr {
799825
Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3])
800826
}
801827
}
802828

803829
#[stable(feature = "ip_from_slice", since = "1.17.0")]
804830
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+
/// ```
805841
fn from(octets: [u8; 4]) -> IpAddr {
806842
IpAddr::V4(Ipv4Addr::from(octets))
807843
}
@@ -1395,13 +1431,55 @@ impl From<[u16; 8]> for Ipv6Addr {
13951431

13961432
#[stable(feature = "ip_from_slice", since = "1.17.0")]
13971433
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+
/// ```
13981455
fn from(octets: [u8; 16]) -> IpAddr {
13991456
IpAddr::V6(Ipv6Addr::from(octets))
14001457
}
14011458
}
14021459

14031460
#[stable(feature = "ip_from_slice", since = "1.17.0")]
14041461
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+
/// ```
14051483
fn from(segments: [u16; 8]) -> IpAddr {
14061484
IpAddr::V6(Ipv6Addr::from(segments))
14071485
}

0 commit comments

Comments
 (0)