Skip to content

Commit

Permalink
Rollup merge of rust-lang#38131 - clarcharr:from_segments, r=alexcric…
Browse files Browse the repository at this point in the history
…hton

Add From<[u16; 8]> to Ipv6Addr

Not really sure that this requires an RFC, but I figured that I'd offer a pull request and see what people think. It seems like a reasonable addition.
  • Loading branch information
alexcrichton committed Dec 20, 2016
2 parents 68dd6fd + 5049ad2 commit 65e9691
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/libstd/net/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,14 @@ impl From<[u8; 16]> for Ipv6Addr {
}
}

#[stable(feature = "ipv6_from_segments", since = "1.15.0")]
impl From<[u16; 8]> for Ipv6Addr {
fn from(segments: [u16; 8]) -> Ipv6Addr {
let [a, b, c, d, e, f, g, h] = segments;
Ipv6Addr::new(a, b, c, d, e, f, g, h)
}
}

// Tests for this module
#[cfg(all(test, not(target_os = "emscripten")))]
mod tests {
Expand Down Expand Up @@ -1413,10 +1421,28 @@ mod tests {
}

#[test]
fn ipv4_from_u32_slice() {
fn ipv4_from_octets() {
assert_eq!(Ipv4Addr::from([127, 0, 0, 1]), Ipv4Addr::new(127, 0, 0, 1))
}

#[test]
fn ipv6_from_segments() {
let from_u16s = Ipv6Addr::from([0x0011, 0x2233, 0x4455, 0x6677,
0x8899, 0xaabb, 0xccdd, 0xeeff]);
let new = Ipv6Addr::new(0x0011, 0x2233, 0x4455, 0x6677,
0x8899, 0xaabb, 0xccdd, 0xeeff);
assert_eq!(new, from_u16s);
}

#[test]
fn ipv6_from_octets() {
let from_u16s = Ipv6Addr::from([0x0011, 0x2233, 0x4455, 0x6677,
0x8899, 0xaabb, 0xccdd, 0xeeff]);
let from_u8s = Ipv6Addr::from([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
assert_eq!(from_u16s, from_u8s);
}

#[test]
fn ord() {
assert!(Ipv4Addr::new(100, 64, 3, 3) < Ipv4Addr::new(192, 0, 2, 2));
Expand Down

0 comments on commit 65e9691

Please sign in to comment.