From 22d77a92e567d777c6802c970244571e8c18bd1b Mon Sep 17 00:00:00 2001 From: Marc Planard Date: Wed, 23 Nov 2022 14:14:55 +0100 Subject: [PATCH] add From for Ip --- src/ip.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/ip.rs b/src/ip.rs index 1581456..332b5b7 100644 --- a/src/ip.rs +++ b/src/ip.rs @@ -44,6 +44,19 @@ impl From<&Ip> for u32 { } } +// convertion the other way arround, from u32 to Ip::V4 +impl From for Ip { + fn from(src: u32) -> Self { + let array = [ ((src >> 24) & 0xff) as u8, + ((src >> 16) & 0xff) as u8, + ((src >> 8) & 0xff) as u8, + (src & 0xff) as u8 + ]; + Ip::V4(array) + } +} + + // Display is a standard trait used by format! and println! and others // to display objects... impl Display for Ip { @@ -91,3 +104,9 @@ fn test_u32_from_ip() { let nbr = u32::from(&ip); assert_eq!(nbr, 0x7f000001); } + +#[test] +fn test_ip_from_u32() { + let ip = Ip::from(0x7f000001); + assert_eq!(ip, Ip::V4([127, 0, 0, 1])); +}