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])); +}