Skip to content

Commit ee18d8e

Browse files
committed
Add an impl for From trait
Converts a u8 slice to a Ipv4Addr
1 parent c97524b commit ee18d8e

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/libstd/net/ip.rs

+12
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,13 @@ impl From<u32> for Ipv4Addr {
276276
}
277277
}
278278

279+
#[stable(feature = "from_slice_v4", since = "1.9.0")]
280+
impl From<[u8; 4]> for Ipv4Addr {
281+
fn from(octets: [u8; 4]) -> Ipv4Addr {
282+
Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3])
283+
}
284+
}
285+
279286
impl Ipv6Addr {
280287
/// Creates a new IPv6 address from eight 16-bit segments.
281288
///
@@ -816,6 +823,11 @@ mod tests {
816823
assert_eq!(Ipv4Addr::from(2130706433), a);
817824
}
818825

826+
#[test]
827+
fn ipv4_from_u32_slice() {
828+
assert_eq!(Ipv4Addr::from([127, 0, 0, 1]), Ipv4Addr::new(127, 0, 0, 1))
829+
}
830+
819831
#[test]
820832
fn ord() {
821833
assert!(Ipv4Addr::new(100, 64, 3, 3) < Ipv4Addr::new(192, 0, 2, 2));

0 commit comments

Comments
 (0)