@@ -468,7 +468,13 @@ impl Ipv4Addr {
468
468
#[ unstable( feature = "ip_bits" , issue = "113744" ) ]
469
469
pub const BITS : u32 = 32 ;
470
470
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.
472
478
///
473
479
/// # Examples
474
480
///
@@ -479,6 +485,16 @@ impl Ipv4Addr {
479
485
/// let addr = Ipv4Addr::new(0x12, 0x34, 0x56, 0x78);
480
486
/// assert_eq!(0x12345678, addr.to_bits());
481
487
/// ```
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
+ /// ```
482
498
#[ rustc_const_unstable( feature = "ip_bits" , issue = "113744" ) ]
483
499
#[ unstable( feature = "ip_bits" , issue = "113744" ) ]
484
500
#[ must_use]
@@ -487,7 +503,9 @@ impl Ipv4Addr {
487
503
u32:: from_be_bytes ( self . octets )
488
504
}
489
505
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.
491
509
///
492
510
/// # Examples
493
511
///
@@ -1224,7 +1242,13 @@ impl Ipv6Addr {
1224
1242
#[ unstable( feature = "ip_bits" , issue = "113744" ) ]
1225
1243
pub const BITS : u32 = 128 ;
1226
1244
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.
1228
1252
///
1229
1253
/// # Examples
1230
1254
///
@@ -1238,6 +1262,24 @@ impl Ipv6Addr {
1238
1262
/// );
1239
1263
/// assert_eq!(0x102030405060708090A0B0C0D0E0F00D_u128, u128::from(addr));
1240
1264
/// ```
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
+ /// ```
1241
1283
#[ rustc_const_unstable( feature = "ip_bits" , issue = "113744" ) ]
1242
1284
#[ unstable( feature = "ip_bits" , issue = "113744" ) ]
1243
1285
#[ must_use]
@@ -1246,7 +1288,9 @@ impl Ipv6Addr {
1246
1288
u128:: from_be_bytes ( self . octets )
1247
1289
}
1248
1290
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.
1250
1294
///
1251
1295
/// # Examples
1252
1296
///
0 commit comments