@@ -450,6 +450,57 @@ impl Ipv4Addr {
450
450
Ipv4Addr { octets : [ a, b, c, d] }
451
451
}
452
452
453
+ /// The size of an IPv4 address in bits.
454
+ ///
455
+ /// # Examples
456
+ ///
457
+ /// ```
458
+ /// #![feature(ip_bits)]
459
+ /// use std::net::Ipv4Addr;
460
+ ///
461
+ /// assert_eq!(Ipv4Addr::BITS, 32);
462
+ /// ```
463
+ #[ unstable( feature = "ip_bits" , issue = "113744" ) ]
464
+ pub const BITS : u32 = 32 ;
465
+
466
+ /// Converts an IPv4 address into host byte order `u32`.
467
+ ///
468
+ /// # Examples
469
+ ///
470
+ /// ```
471
+ /// #![feature(ip_bits)]
472
+ /// use std::net::Ipv4Addr;
473
+ ///
474
+ /// let addr = Ipv4Addr::new(0x12, 0x34, 0x56, 0x78);
475
+ /// assert_eq!(0x12345678, addr.to_bits());
476
+ /// ```
477
+ #[ rustc_const_unstable( feature = "ip_bits" , issue = "113744" ) ]
478
+ #[ unstable( feature = "ip_bits" , issue = "113744" ) ]
479
+ #[ must_use]
480
+ #[ inline]
481
+ pub const fn to_bits ( self ) -> u32 {
482
+ u32:: from_be_bytes ( self . octets )
483
+ }
484
+
485
+ /// Converts a host byte order `u32` into an IPv4 address.
486
+ ///
487
+ /// # Examples
488
+ ///
489
+ /// ```
490
+ /// #![feature(ip_bits)]
491
+ /// use std::net::Ipv4Addr;
492
+ ///
493
+ /// let addr = Ipv4Addr::from(0x12345678);
494
+ /// assert_eq!(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78), addr);
495
+ /// ```
496
+ #[ rustc_const_unstable( feature = "ip_bits" , issue = "113744" ) ]
497
+ #[ unstable( feature = "ip_bits" , issue = "113744" ) ]
498
+ #[ must_use]
499
+ #[ inline]
500
+ pub const fn from_bits ( bits : u32 ) -> Ipv4Addr {
501
+ Ipv4Addr { octets : bits. to_be_bytes ( ) }
502
+ }
503
+
453
504
/// An IPv4 address with the address pointing to localhost: `127.0.0.1`
454
505
///
455
506
/// # Examples
@@ -1069,37 +1120,17 @@ impl Ord for Ipv4Addr {
1069
1120
1070
1121
#[ stable( feature = "ip_u32" , since = "1.1.0" ) ]
1071
1122
impl From < Ipv4Addr > for u32 {
1072
- /// Converts an `Ipv4Addr` into a host byte order `u32`.
1073
- ///
1074
- /// # Examples
1075
- ///
1076
- /// ```
1077
- /// use std::net::Ipv4Addr;
1078
- ///
1079
- /// let addr = Ipv4Addr::new(0x12, 0x34, 0x56, 0x78);
1080
- /// assert_eq!(0x12345678, u32::from(addr));
1081
- /// ```
1082
1123
#[ inline]
1083
1124
fn from ( ip : Ipv4Addr ) -> u32 {
1084
- u32 :: from_be_bytes ( ip. octets )
1125
+ ip. to_bits ( )
1085
1126
}
1086
1127
}
1087
1128
1088
1129
#[ stable( feature = "ip_u32" , since = "1.1.0" ) ]
1089
1130
impl From < u32 > for Ipv4Addr {
1090
- /// Converts a host byte order `u32` into an `Ipv4Addr`.
1091
- ///
1092
- /// # Examples
1093
- ///
1094
- /// ```
1095
- /// use std::net::Ipv4Addr;
1096
- ///
1097
- /// let addr = Ipv4Addr::from(0x12345678);
1098
- /// assert_eq!(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78), addr);
1099
- /// ```
1100
1131
#[ inline]
1101
1132
fn from ( ip : u32 ) -> Ipv4Addr {
1102
- Ipv4Addr { octets : ip . to_be_bytes ( ) }
1133
+ Ipv4Addr :: from_bits ( ip )
1103
1134
}
1104
1135
}
1105
1136
@@ -1173,6 +1204,65 @@ impl Ipv6Addr {
1173
1204
}
1174
1205
}
1175
1206
1207
+ /// The size of an IPv6 address in bits.
1208
+ ///
1209
+ /// # Examples
1210
+ ///
1211
+ /// ```
1212
+ /// #![feature(ip_bits)]
1213
+ /// use std::net::Ipv6Addr;
1214
+ ///
1215
+ /// assert_eq!(Ipv6Addr::BITS, 128);
1216
+ /// ```
1217
+ #[ unstable( feature = "ip_bits" , issue = "113744" ) ]
1218
+ pub const BITS : u32 = 128 ;
1219
+
1220
+ /// Converts an IPv6 address into host byte order `u128`.
1221
+ ///
1222
+ /// # Examples
1223
+ ///
1224
+ /// ```
1225
+ /// #![feature(ip_bits)]
1226
+ /// use std::net::Ipv6Addr;
1227
+ ///
1228
+ /// let addr = Ipv6Addr::new(
1229
+ /// 0x1020, 0x3040, 0x5060, 0x7080,
1230
+ /// 0x90A0, 0xB0C0, 0xD0E0, 0xF00D,
1231
+ /// );
1232
+ /// assert_eq!(0x102030405060708090A0B0C0D0E0F00D_u128, u128::from(addr));
1233
+ /// ```
1234
+ #[ rustc_const_unstable( feature = "ip_bits" , issue = "113744" ) ]
1235
+ #[ unstable( feature = "ip_bits" , issue = "113744" ) ]
1236
+ #[ must_use]
1237
+ #[ inline]
1238
+ pub const fn to_bits ( self ) -> u128 {
1239
+ u128:: from_be_bytes ( self . octets )
1240
+ }
1241
+
1242
+ /// Converts a host byte order `u128` into an IPv6 address.
1243
+ ///
1244
+ /// # Examples
1245
+ ///
1246
+ /// ```
1247
+ /// #![feature(ip_bits)]
1248
+ /// use std::net::Ipv6Addr;
1249
+ ///
1250
+ /// let addr = Ipv6Addr::from(0x102030405060708090A0B0C0D0E0F00D_u128);
1251
+ /// assert_eq!(
1252
+ /// Ipv6Addr::new(
1253
+ /// 0x1020, 0x3040, 0x5060, 0x7080,
1254
+ /// 0x90A0, 0xB0C0, 0xD0E0, 0xF00D,
1255
+ /// ),
1256
+ /// addr);
1257
+ /// ```
1258
+ #[ rustc_const_unstable( feature = "ip_bits" , issue = "113744" ) ]
1259
+ #[ unstable( feature = "ip_bits" , issue = "113744" ) ]
1260
+ #[ must_use]
1261
+ #[ inline]
1262
+ pub const fn from_bits ( bits : u128 ) -> Ipv6Addr {
1263
+ Ipv6Addr { octets : bits. to_be_bytes ( ) }
1264
+ }
1265
+
1176
1266
/// An IPv6 address representing localhost: `::1`.
1177
1267
///
1178
1268
/// This corresponds to constant `IN6ADDR_LOOPBACK_INIT` or `in6addr_loopback` in other
@@ -1905,44 +1995,16 @@ impl Ord for Ipv6Addr {
1905
1995
1906
1996
#[ stable( feature = "i128" , since = "1.26.0" ) ]
1907
1997
impl From < Ipv6Addr > for u128 {
1908
- /// Convert an `Ipv6Addr` into a host byte order `u128`.
1909
- ///
1910
- /// # Examples
1911
- ///
1912
- /// ```
1913
- /// use std::net::Ipv6Addr;
1914
- ///
1915
- /// let addr = Ipv6Addr::new(
1916
- /// 0x1020, 0x3040, 0x5060, 0x7080,
1917
- /// 0x90A0, 0xB0C0, 0xD0E0, 0xF00D,
1918
- /// );
1919
- /// assert_eq!(0x102030405060708090A0B0C0D0E0F00D_u128, u128::from(addr));
1920
- /// ```
1921
1998
#[ inline]
1922
1999
fn from ( ip : Ipv6Addr ) -> u128 {
1923
- u128 :: from_be_bytes ( ip. octets )
2000
+ ip. to_bits ( )
1924
2001
}
1925
2002
}
1926
2003
#[ stable( feature = "i128" , since = "1.26.0" ) ]
1927
2004
impl From < u128 > for Ipv6Addr {
1928
- /// Convert a host byte order `u128` into an `Ipv6Addr`.
1929
- ///
1930
- /// # Examples
1931
- ///
1932
- /// ```
1933
- /// use std::net::Ipv6Addr;
1934
- ///
1935
- /// let addr = Ipv6Addr::from(0x102030405060708090A0B0C0D0E0F00D_u128);
1936
- /// assert_eq!(
1937
- /// Ipv6Addr::new(
1938
- /// 0x1020, 0x3040, 0x5060, 0x7080,
1939
- /// 0x90A0, 0xB0C0, 0xD0E0, 0xF00D,
1940
- /// ),
1941
- /// addr);
1942
- /// ```
1943
2005
#[ inline]
1944
2006
fn from ( ip : u128 ) -> Ipv6Addr {
1945
- Ipv6Addr :: from ( ip. to_be_bytes ( ) )
2007
+ Ipv6Addr :: from_bits ( ip)
1946
2008
}
1947
2009
}
1948
2010
0 commit comments