add From<u32> for Ip
This commit is contained in:
parent
29d39d1420
commit
22d77a92e5
19
src/ip.rs
19
src/ip.rs
@ -44,6 +44,19 @@ impl From<&Ip> for u32 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// convertion the other way arround, from u32 to Ip::V4
|
||||||
|
impl From<u32> 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
|
// Display is a standard trait used by format! and println! and others
|
||||||
// to display objects...
|
// to display objects...
|
||||||
impl Display for Ip {
|
impl Display for Ip {
|
||||||
@ -91,3 +104,9 @@ fn test_u32_from_ip() {
|
|||||||
let nbr = u32::from(&ip);
|
let nbr = u32::from(&ip);
|
||||||
assert_eq!(nbr, 0x7f000001);
|
assert_eq!(nbr, 0x7f000001);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ip_from_u32() {
|
||||||
|
let ip = Ip::from(0x7f000001);
|
||||||
|
assert_eq!(ip, Ip::V4([127, 0, 0, 1]));
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user