@@ -468,7 +468,13 @@ impl Ipv4Addr {
468468 #[ unstable( feature = "ip_bits" , issue = "113744" ) ]
469469 pub const BITS : u32 = 32 ;
470470
471- /// Converts an IPv4 address into host byte order `u32`.
471+ /// Converts an IPv4 address into a `u32` representation using native byte order.
472+ ///
473+ /// Although IPv4 addresses are big-endian, the `u32` value will use the target platform's
474+ /// native byte order. That is, the `u32` value is an integer representation of the IPv4
475+ /// address and not an integer interpretation of the IPv4 address's big-endian bitstring. This
476+ /// means that the `u32` value masked with `0xffffff00` will set the last octet in the address
477+ /// to 0, regardless of the target platform's endianness.
472478 ///
473479 /// # Examples
474480 ///
@@ -479,6 +485,16 @@ impl Ipv4Addr {
479485 /// let addr = Ipv4Addr::new(0x12, 0x34, 0x56, 0x78);
480486 /// assert_eq!(0x12345678, addr.to_bits());
481487 /// ```
488+ ///
489+ /// ```
490+ /// #![feature(ip_bits)]
491+ /// use std::net::Ipv4Addr;
492+ ///
493+ /// let addr = Ipv4Addr::new(0x12, 0x34, 0x56, 0x78);
494+ /// let addr_bits = addr.to_bits() & 0xffffff00;
495+ /// assert_eq!(Ipv4Addr::new(0x12, 0x34, 0x56, 0x00), Ipv4Addr::from_bits(addr_bits));
496+ ///
497+ /// ```
482498 #[ rustc_const_unstable( feature = "ip_bits" , issue = "113744" ) ]
483499 #[ unstable( feature = "ip_bits" , issue = "113744" ) ]
484500 #[ must_use]
@@ -487,7 +503,9 @@ impl Ipv4Addr {
487503 u32:: from_be_bytes ( self . octets )
488504 }
489505
490- /// Converts a host byte order `u32` into an IPv4 address.
506+ /// Converts a native byte order `u32` into an IPv4 address.
507+ ///
508+ /// See [`Ipv4Addr::to_bits`] for an explanation on endianness.
491509 ///
492510 /// # Examples
493511 ///
@@ -1224,7 +1242,13 @@ impl Ipv6Addr {
12241242 #[ unstable( feature = "ip_bits" , issue = "113744" ) ]
12251243 pub const BITS : u32 = 128 ;
12261244
1227- /// Converts an IPv6 address into host byte order `u128`.
1245+ /// Converts an IPv6 address into a `u128` representation using native byte order.
1246+ ///
1247+ /// Although IPv6 addresses are big-endian, the `u128` value will use the target platform's
1248+ /// native byte order. That is, the `u128` value is an integer representation of the IPv6
1249+ /// address and not an integer interpretation of the IPv6 address's big-endian bitstring. This
1250+ /// means that the `u128` value masked with `0xffffffffffffffffffffffffffff0000_u128` will set
1251+ /// the last segment in the address to 0, regardless of the target platform's endianness.
12281252 ///
12291253 /// # Examples
12301254 ///
@@ -1238,6 +1262,24 @@ impl Ipv6Addr {
12381262 /// );
12391263 /// assert_eq!(0x102030405060708090A0B0C0D0E0F00D_u128, u128::from(addr));
12401264 /// ```
1265+ ///
1266+ /// ```
1267+ /// #![feature(ip_bits)]
1268+ /// use std::net::Ipv6Addr;
1269+ ///
1270+ /// let addr = Ipv6Addr::new(
1271+ /// 0x1020, 0x3040, 0x5060, 0x7080,
1272+ /// 0x90A0, 0xB0C0, 0xD0E0, 0xF00D,
1273+ /// );
1274+ /// let addr_bits = addr.to_bits() & 0xffffffffffffffffffffffffffff0000_u128;
1275+ /// assert_eq!(
1276+ /// Ipv6Addr::new(
1277+ /// 0x1020, 0x3040, 0x5060, 0x7080,
1278+ /// 0x90A0, 0xB0C0, 0xD0E0, 0x0000,
1279+ /// ),
1280+ /// Ipv6Addr::from_bits(addr_bits));
1281+ ///
1282+ /// ```
12411283 #[ rustc_const_unstable( feature = "ip_bits" , issue = "113744" ) ]
12421284 #[ unstable( feature = "ip_bits" , issue = "113744" ) ]
12431285 #[ must_use]
@@ -1246,7 +1288,9 @@ impl Ipv6Addr {
12461288 u128:: from_be_bytes ( self . octets )
12471289 }
12481290
1249- /// Converts a host byte order `u128` into an IPv6 address.
1291+ /// Converts a native byte order `u128` into an IPv6 address.
1292+ ///
1293+ /// See [`Ipv6Addr::to_bits`] for an explanation on endianness.
12501294 ///
12511295 /// # Examples
12521296 ///
0 commit comments