@@ -450,6 +450,57 @@ impl Ipv4Addr {
450450 Ipv4Addr { octets : [ a, b, c, d] }
451451 }
452452
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+
453504 /// An IPv4 address with the address pointing to localhost: `127.0.0.1`
454505 ///
455506 /// # Examples
@@ -1069,37 +1120,19 @@ impl Ord for Ipv4Addr {
10691120
10701121#[ stable( feature = "ip_u32" , since = "1.1.0" ) ]
10711122impl 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- /// ```
1123+ /// Uses [`Ipv4Addr::to_bits`] to convert an IPv4 address to a host byte order `u32`.
10821124 #[ inline]
10831125 fn from ( ip : Ipv4Addr ) -> u32 {
1084- u32 :: from_be_bytes ( ip. octets )
1126+ ip. to_bits ( )
10851127 }
10861128}
10871129
10881130#[ stable( feature = "ip_u32" , since = "1.1.0" ) ]
10891131impl 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- /// ```
1132+ /// Uses [`Ipv4Addr::from_bits`] to convert a host byte order `u32` into an IPv4 address.
11001133 #[ inline]
11011134 fn from ( ip : u32 ) -> Ipv4Addr {
1102- Ipv4Addr { octets : ip . to_be_bytes ( ) }
1135+ Ipv4Addr :: from_bits ( ip )
11031136 }
11041137}
11051138
@@ -1173,6 +1206,65 @@ impl Ipv6Addr {
11731206 }
11741207 }
11751208
1209+ /// The size of an IPv6 address in bits.
1210+ ///
1211+ /// # Examples
1212+ ///
1213+ /// ```
1214+ /// #![feature(ip_bits)]
1215+ /// use std::net::Ipv6Addr;
1216+ ///
1217+ /// assert_eq!(Ipv6Addr::BITS, 128);
1218+ /// ```
1219+ #[ unstable( feature = "ip_bits" , issue = "113744" ) ]
1220+ pub const BITS : u32 = 128 ;
1221+
1222+ /// Converts an IPv6 address into host byte order `u128`.
1223+ ///
1224+ /// # Examples
1225+ ///
1226+ /// ```
1227+ /// #![feature(ip_bits)]
1228+ /// use std::net::Ipv6Addr;
1229+ ///
1230+ /// let addr = Ipv6Addr::new(
1231+ /// 0x1020, 0x3040, 0x5060, 0x7080,
1232+ /// 0x90A0, 0xB0C0, 0xD0E0, 0xF00D,
1233+ /// );
1234+ /// assert_eq!(0x102030405060708090A0B0C0D0E0F00D_u128, u128::from(addr));
1235+ /// ```
1236+ #[ rustc_const_unstable( feature = "ip_bits" , issue = "113744" ) ]
1237+ #[ unstable( feature = "ip_bits" , issue = "113744" ) ]
1238+ #[ must_use]
1239+ #[ inline]
1240+ pub const fn to_bits ( self ) -> u128 {
1241+ u128:: from_be_bytes ( self . octets )
1242+ }
1243+
1244+ /// Converts a host byte order `u128` into an IPv6 address.
1245+ ///
1246+ /// # Examples
1247+ ///
1248+ /// ```
1249+ /// #![feature(ip_bits)]
1250+ /// use std::net::Ipv6Addr;
1251+ ///
1252+ /// let addr = Ipv6Addr::from(0x102030405060708090A0B0C0D0E0F00D_u128);
1253+ /// assert_eq!(
1254+ /// Ipv6Addr::new(
1255+ /// 0x1020, 0x3040, 0x5060, 0x7080,
1256+ /// 0x90A0, 0xB0C0, 0xD0E0, 0xF00D,
1257+ /// ),
1258+ /// addr);
1259+ /// ```
1260+ #[ rustc_const_unstable( feature = "ip_bits" , issue = "113744" ) ]
1261+ #[ unstable( feature = "ip_bits" , issue = "113744" ) ]
1262+ #[ must_use]
1263+ #[ inline]
1264+ pub const fn from_bits ( bits : u128 ) -> Ipv6Addr {
1265+ Ipv6Addr { octets : bits. to_be_bytes ( ) }
1266+ }
1267+
11761268 /// An IPv6 address representing localhost: `::1`.
11771269 ///
11781270 /// This corresponds to constant `IN6ADDR_LOOPBACK_INIT` or `in6addr_loopback` in other
@@ -1905,44 +1997,18 @@ impl Ord for Ipv6Addr {
19051997
19061998#[ stable( feature = "i128" , since = "1.26.0" ) ]
19071999impl 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- /// ```
2000+ /// Uses [`Ipv6Addr::to_bits`] to convert an IPv6 address to a host byte order `u128`.
19212001 #[ inline]
19222002 fn from ( ip : Ipv6Addr ) -> u128 {
1923- u128 :: from_be_bytes ( ip. octets )
2003+ ip. to_bits ( )
19242004 }
19252005}
19262006#[ stable( feature = "i128" , since = "1.26.0" ) ]
19272007impl 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- /// ```
2008+ /// Uses [`Ipv6Addr::from_bits`] to convert a host byte order `u128` to an IPv6 address.
19432009 #[ inline]
19442010 fn from ( ip : u128 ) -> Ipv6Addr {
1945- Ipv6Addr :: from ( ip. to_be_bytes ( ) )
2011+ Ipv6Addr :: from_bits ( ip)
19462012 }
19472013}
19482014
0 commit comments