diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2016-12-20 11:16:19 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-12-20 11:16:19 -0800 |
| commit | 65e9691cefdf48e09dcd6cce5bbd521044919598 (patch) | |
| tree | ae6f323a857571468f14325cd72558b99ac6c84e /src/libstd | |
| parent | 68dd6fd964691cb50fc01ae6d0bad7f4b723b2d4 (diff) | |
| parent | 5049ad22ec3fff948ed6e568336e055402b5a246 (diff) | |
| download | rust-65e9691cefdf48e09dcd6cce5bbd521044919598.tar.gz rust-65e9691cefdf48e09dcd6cce5bbd521044919598.zip | |
Rollup merge of #38131 - clarcharr:from_segments, r=alexcrichton
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.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/net/ip.rs | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/src/libstd/net/ip.rs b/src/libstd/net/ip.rs index c1e610f33fb..6aab7486004 100644 --- a/src/libstd/net/ip.rs +++ b/src/libstd/net/ip.rs @@ -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 { @@ -1413,11 +1421,29 @@ 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)); assert!("2001:db8:f00::1002".parse::<Ipv6Addr>().unwrap() < |
