Skip to content

Commit 30710c3

Browse files
committed
Add BITS, from_bits, to_bits to IP addresses
1 parent 4124617 commit 30710c3

File tree

2 files changed

+115
-52
lines changed

2 files changed

+115
-52
lines changed

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
#![feature(duration_consts_float)]
166166
#![feature(internal_impls_macro)]
167167
#![feature(ip)]
168+
#![feature(ip_bits)]
168169
#![feature(is_ascii_octdigit)]
169170
#![feature(maybe_uninit_uninit_array)]
170171
#![feature(ptr_alignment_type)]

library/core/src/net/ip_addr.rs

+114-52
Original file line numberDiff line numberDiff line change
@@ -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,17 @@ impl Ord for Ipv4Addr {
10691120

10701121
#[stable(feature = "ip_u32", since = "1.1.0")]
10711122
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-
/// ```
10821123
#[inline]
10831124
fn from(ip: Ipv4Addr) -> u32 {
1084-
u32::from_be_bytes(ip.octets)
1125+
ip.to_bits()
10851126
}
10861127
}
10871128

10881129
#[stable(feature = "ip_u32", since = "1.1.0")]
10891130
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-
/// ```
11001131
#[inline]
11011132
fn from(ip: u32) -> Ipv4Addr {
1102-
Ipv4Addr { octets: ip.to_be_bytes() }
1133+
Ipv4Addr::from_bits(ip)
11031134
}
11041135
}
11051136

@@ -1173,6 +1204,65 @@ impl Ipv6Addr {
11731204
}
11741205
}
11751206

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+
11761266
/// An IPv6 address representing localhost: `::1`.
11771267
///
11781268
/// This corresponds to constant `IN6ADDR_LOOPBACK_INIT` or `in6addr_loopback` in other
@@ -1905,44 +1995,16 @@ impl Ord for Ipv6Addr {
19051995

19061996
#[stable(feature = "i128", since = "1.26.0")]
19071997
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-
/// ```
19211998
#[inline]
19221999
fn from(ip: Ipv6Addr) -> u128 {
1923-
u128::from_be_bytes(ip.octets)
2000+
ip.to_bits()
19242001
}
19252002
}
19262003
#[stable(feature = "i128", since = "1.26.0")]
19272004
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-
/// ```
19432005
#[inline]
19442006
fn from(ip: u128) -> Ipv6Addr {
1945-
Ipv6Addr::from(ip.to_be_bytes())
2007+
Ipv6Addr::from_bits(ip)
19462008
}
19472009
}
19482010

0 commit comments

Comments
 (0)