Skip to content

Commit b89fb71

Browse files
committed
Clarify network byte order conversions for integer / IP address conversions.
Opened primarily to address #48819.
1 parent 184156e commit b89fb71

File tree

1 file changed

+80
-2
lines changed

1 file changed

+80
-2
lines changed

src/libstd/net/ip.rs

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,16 @@ impl FromInner<c::in_addr> for Ipv4Addr {
769769

770770
#[stable(feature = "ip_u32", since = "1.1.0")]
771771
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+
/// ```
773782
fn from(ip: Ipv4Addr) -> u32 {
774783
let ip = ip.octets();
775784
((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 {
778787

779788
#[stable(feature = "ip_u32", since = "1.1.0")]
780789
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+
/// ```
782800
fn from(ip: u32) -> Ipv4Addr {
783801
Ipv4Addr::new((ip >> 24) as u8, (ip >> 16) as u8, (ip >> 8) as u8, ip as u8)
784802
}
785803
}
786804

787805
#[stable(feature = "from_slice_v4", since = "1.9.0")]
788806
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+
/// ```
789815
fn from(octets: [u8; 4]) -> Ipv4Addr {
790816
Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3])
791817
}
792818
}
793819

794820
#[stable(feature = "ip_from_slice", since = "1.17.0")]
795821
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+
/// ```
796832
fn from(octets: [u8; 4]) -> IpAddr {
797833
IpAddr::V4(Ipv4Addr::from(octets))
798834
}
@@ -1386,13 +1422,55 @@ impl From<[u16; 8]> for Ipv6Addr {
13861422

13871423
#[stable(feature = "ip_from_slice", since = "1.17.0")]
13881424
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+
/// ```
13891446
fn from(octets: [u8; 16]) -> IpAddr {
13901447
IpAddr::V6(Ipv6Addr::from(octets))
13911448
}
13921449
}
13931450

13941451
#[stable(feature = "ip_from_slice", since = "1.17.0")]
13951452
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+
/// ```
13961474
fn from(segments: [u16; 8]) -> IpAddr {
13971475
IpAddr::V6(Ipv6Addr::from(segments))
13981476
}

0 commit comments

Comments
 (0)