Add ip version specific addr/endpoint converters

Converting from `::std::net::*` types to the ip related library structs
previously required both ip-version features to be enabled. This
introduced dedicated converters from the ip-version specific standard
address and endpoint representations (`IpV4Addr`, `IpV6Addr`,
`SocketAddrV4`, and `SocketAddrV6`) that are enabled without requiring
the other ip-version feature to be selected.

Closes: #286
Approved by: whitequark
This commit is contained in:
Andreas Molzer 2019-04-08 19:54:51 +02:00 committed by Homu
parent 55d1e9c997
commit d9e0c8246c
1 changed files with 34 additions and 0 deletions

View File

@ -224,6 +224,20 @@ impl From<::std::net::IpAddr> for Address {
}
}
#[cfg(all(feature = "std", feature = "proto-ipv4"))]
impl From<::std::net::Ipv4Addr> for Address {
fn from(ipv4: ::std::net::Ipv4Addr) -> Address {
Address::Ipv4(ipv4.into())
}
}
#[cfg(all(feature = "std", feature = "proto-ipv6"))]
impl From<::std::net::Ipv6Addr> for Address {
fn from(ipv6: ::std::net::Ipv6Addr) -> Address {
Address::Ipv6(ipv6.into())
}
}
impl Default for Address {
fn default() -> Address {
Address::Unspecified
@ -413,6 +427,26 @@ impl From<::std::net::SocketAddr> for Endpoint {
}
}
#[cfg(all(feature = "std", feature = "proto-ipv4"))]
impl From<::std::net::SocketAddrV4> for Endpoint {
fn from(x: ::std::net::SocketAddrV4) -> Endpoint {
Endpoint {
addr: x.ip().clone().into(),
port: x.port(),
}
}
}
#[cfg(all(feature = "std", feature = "proto-ipv6"))]
impl From<::std::net::SocketAddrV6> for Endpoint {
fn from(x: ::std::net::SocketAddrV6) -> Endpoint {
Endpoint {
addr: x.ip().clone().into(),
port: x.port(),
}
}
}
impl fmt::Display for Endpoint {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}:{}", self.addr, self.port)