Skip to content

Commit 5049ad2

Browse files
author
Clar Charr
committed
From<[u16; 8]> for Ipv6Addr.
1 parent 71c06a5 commit 5049ad2

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/libstd/net/ip.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,14 @@ impl From<[u8; 16]> for Ipv6Addr {
10681068
}
10691069
}
10701070

1071+
#[stable(feature = "ipv6_from_segments", since = "1.15.0")]
1072+
impl From<[u16; 8]> for Ipv6Addr {
1073+
fn from(segments: [u16; 8]) -> Ipv6Addr {
1074+
let [a, b, c, d, e, f, g, h] = segments;
1075+
Ipv6Addr::new(a, b, c, d, e, f, g, h)
1076+
}
1077+
}
1078+
10711079
// Tests for this module
10721080
#[cfg(all(test, not(target_os = "emscripten")))]
10731081
mod tests {
@@ -1413,10 +1421,28 @@ mod tests {
14131421
}
14141422

14151423
#[test]
1416-
fn ipv4_from_u32_slice() {
1424+
fn ipv4_from_octets() {
14171425
assert_eq!(Ipv4Addr::from([127, 0, 0, 1]), Ipv4Addr::new(127, 0, 0, 1))
14181426
}
14191427

1428+
#[test]
1429+
fn ipv6_from_segments() {
1430+
let from_u16s = Ipv6Addr::from([0x0011, 0x2233, 0x4455, 0x6677,
1431+
0x8899, 0xaabb, 0xccdd, 0xeeff]);
1432+
let new = Ipv6Addr::new(0x0011, 0x2233, 0x4455, 0x6677,
1433+
0x8899, 0xaabb, 0xccdd, 0xeeff);
1434+
assert_eq!(new, from_u16s);
1435+
}
1436+
1437+
#[test]
1438+
fn ipv6_from_octets() {
1439+
let from_u16s = Ipv6Addr::from([0x0011, 0x2233, 0x4455, 0x6677,
1440+
0x8899, 0xaabb, 0xccdd, 0xeeff]);
1441+
let from_u8s = Ipv6Addr::from([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
1442+
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
1443+
assert_eq!(from_u16s, from_u8s);
1444+
}
1445+
14201446
#[test]
14211447
fn ord() {
14221448
assert!(Ipv4Addr::new(100, 64, 3, 3) < Ipv4Addr::new(192, 0, 2, 2));

0 commit comments

Comments
 (0)