Эх сурвалжийг харах

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
Andreas Molzer 6 жил өмнө
parent
commit
e2fcd02414
1 өөрчлөгдсөн 34 нэмэгдсэн , 0 устгасан
  1. 34 0
      src/wire/ip.rs

+ 34 - 0
src/wire/ip.rs

@@ -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)