about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2016-03-18 17:30:22 -0700
committerbors <bors@rust-lang.org>2016-03-18 17:30:22 -0700
commit10bdd808b5e6e23ed8dd9f281c60193a570865f9 (patch)
treece4b184b08506a886fa057423c1e6ce00178a7a6 /src/libstd
parent02954ae0a86c5c2f00e9e06b520999c1e67e9cd7 (diff)
parentee18d8e99b03a451f9dd549fd756ce006134bd73 (diff)
downloadrust-10bdd808b5e6e23ed8dd9f281c60193a570865f9.tar.gz
rust-10bdd808b5e6e23ed8dd9f281c60193a570865f9.zip
Auto merge of #32050 - achanda:from-slice-v4, r=alexcrichton
Add an impl for From trait

Converts a u8 slice to a Ipv4Addr
More discussion on this here: https://github.com/rust-lang/rfcs/pull/1498#issuecomment-191921655
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/net/ip.rs12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/libstd/net/ip.rs b/src/libstd/net/ip.rs
index 678b581a904..934d0468a38 100644
--- a/src/libstd/net/ip.rs
+++ b/src/libstd/net/ip.rs
@@ -279,6 +279,13 @@ impl From<u32> for Ipv4Addr {
     }
 }
 
+#[stable(feature = "from_slice_v4", since = "1.9.0")]
+impl From<[u8; 4]> for Ipv4Addr {
+    fn from(octets: [u8; 4]) -> Ipv4Addr {
+        Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3])
+    }
+}
+
 impl Ipv6Addr {
     /// Creates a new IPv6 address from eight 16-bit segments.
     ///
@@ -832,6 +839,11 @@ mod tests {
     }
 
     #[test]
+    fn ipv4_from_u32_slice() {
+        assert_eq!(Ipv4Addr::from([127, 0, 0, 1]), Ipv4Addr::new(127, 0, 0, 1))
+    }
+
+    #[test]
     fn ord() {
         assert!(Ipv4Addr::new(100, 64, 3, 3) < Ipv4Addr::new(192, 0, 2, 2));
         assert!("2001:db8:f00::1002".parse::<Ipv6Addr>().unwrap() <