Skip to content

Commit c14f87e

Browse files
committed
Auto merge of #39324 - clarcharr:ipv6_u128, r=alexcrichton
Ipv6Addr <-> u128 Because we have `u128` now, it makes sense to add a conversion between `Ipv6Addr` and `u128` in addition to the existing one between `Ipv4Addr` and `u32`. This shouldn't violate the existing feature gate on `u128` because you can't use the type without the feature gate, but if i have to add something, I can.
2 parents a797b6e + 8b2e334 commit c14f87e

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

src/libstd/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@
261261
#![feature(generic_param_attrs)]
262262
#![feature(hashmap_hasher)]
263263
#![feature(heap_api)]
264+
#![feature(i128)]
265+
#![feature(i128_type)]
264266
#![feature(inclusive_range)]
265267
#![feature(int_error_internals)]
266268
#![feature(integer_atomics)]
@@ -304,7 +306,6 @@
304306
#![feature(unwind_attributes)]
305307
#![feature(vec_push_all)]
306308
#![feature(zero_one)]
307-
#![feature(i128)]
308309
#![cfg_attr(test, feature(update_panic_count))]
309310

310311
// Explicitly import the prelude. The compiler uses this same unstable attribute

src/libstd/net/ip.rs

+36-4
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,26 @@ impl FromInner<c::in6_addr> for Ipv6Addr {
11491149
}
11501150
}
11511151

1152+
#[unstable(feature = "i128", issue = "35118")]
1153+
impl From<Ipv6Addr> for u128 {
1154+
fn from(ip: Ipv6Addr) -> u128 {
1155+
let ip = ip.segments();
1156+
((ip[0] as u128) << 112) + ((ip[1] as u128) << 96) + ((ip[2] as u128) << 80) +
1157+
((ip[3] as u128) << 64) + ((ip[4] as u128) << 48) + ((ip[5] as u128) << 32) +
1158+
((ip[6] as u128) << 16) + (ip[7] as u128)
1159+
}
1160+
}
1161+
#[unstable(feature = "i128", issue = "35118")]
1162+
impl From<u128> for Ipv6Addr {
1163+
fn from(ip: u128) -> Ipv6Addr {
1164+
Ipv6Addr::new(
1165+
(ip >> 112) as u16, (ip >> 96) as u16, (ip >> 80) as u16,
1166+
(ip >> 64) as u16, (ip >> 48) as u16, (ip >> 32) as u16,
1167+
(ip >> 16) as u16, ip as u16,
1168+
)
1169+
}
1170+
}
1171+
11521172
#[stable(feature = "ipv6_from_octets", since = "1.9.0")]
11531173
impl From<[u8; 16]> for Ipv6Addr {
11541174
fn from(octets: [u8; 16]) -> Ipv6Addr {
@@ -1500,14 +1520,26 @@ mod tests {
15001520

15011521
#[test]
15021522
fn test_ipv4_to_int() {
1503-
let a = Ipv4Addr::new(127, 0, 0, 1);
1504-
assert_eq!(u32::from(a), 2130706433);
1523+
let a = Ipv4Addr::new(0x11, 0x22, 0x33, 0x44);
1524+
assert_eq!(u32::from(a), 0x11223344);
15051525
}
15061526

15071527
#[test]
15081528
fn test_int_to_ipv4() {
1509-
let a = Ipv4Addr::new(127, 0, 0, 1);
1510-
assert_eq!(Ipv4Addr::from(2130706433), a);
1529+
let a = Ipv4Addr::new(0x11, 0x22, 0x33, 0x44);
1530+
assert_eq!(Ipv4Addr::from(0x11223344), a);
1531+
}
1532+
1533+
#[test]
1534+
fn test_ipv6_to_int() {
1535+
let a = Ipv6Addr::new(0x1122, 0x3344, 0x5566, 0x7788, 0x99aa, 0xbbcc, 0xddee, 0xff11);
1536+
assert_eq!(u128::from(a), 0x112233445566778899aabbccddeeff11u128);
1537+
}
1538+
1539+
#[test]
1540+
fn test_int_to_ipv6() {
1541+
let a = Ipv6Addr::new(0x1122, 0x3344, 0x5566, 0x7788, 0x99aa, 0xbbcc, 0xddee, 0xff11);
1542+
assert_eq!(Ipv6Addr::from(0x112233445566778899aabbccddeeff11u128), a);
15111543
}
15121544

15131545
#[test]

0 commit comments

Comments
 (0)