Skip to content

Commit 2592bcc

Browse files
committed
impl conversions for IpAddress
1 parent 13c1c2b commit 2592bcc

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

uefi/src/proto/network/mod.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,40 @@ impl IpAddress {
3434
Self(ip_addr)
3535
}
3636
}
37+
38+
impl From<core::net::Ipv4Addr> for IpAddress {
39+
fn from(t: core::net::Ipv4Addr) -> Self {
40+
IpAddress::new_v4(t.octets())
41+
}
42+
}
43+
44+
impl From<IpAddress> for core::net::Ipv4Addr {
45+
fn from(IpAddress(o): IpAddress) -> Self {
46+
core::net::Ipv4Addr::from([o[0], o[1], o[2], o[3]])
47+
}
48+
}
49+
50+
impl From<core::net::Ipv6Addr> for IpAddress {
51+
fn from(t: core::net::Ipv6Addr) -> Self {
52+
IpAddress::new_v6(t.octets())
53+
}
54+
}
55+
56+
impl From<IpAddress> for core::net::Ipv6Addr {
57+
fn from(value: IpAddress) -> Self {
58+
core::net::Ipv6Addr::from(value.0)
59+
}
60+
}
61+
62+
impl From<core::net::IpAddr> for IpAddress {
63+
fn from(t: core::net::IpAddr) -> Self {
64+
match t {
65+
core::net::IpAddr::V4(a) => a.into(),
66+
core::net::IpAddr::V6(a) => a.into(),
67+
}
68+
}
69+
}
70+
71+
// NOTE: We cannot impl From<IpAddress> for core::net::IpAddr
72+
// because IpAddress is a raw union, with nothing indicating
73+
// whether it should be considered v4 or v6.

0 commit comments

Comments
 (0)