From a493c373bf208db4c52ca0092f11ba9bd8032295 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Tue, 13 Dec 2022 10:27:53 -0800 Subject: [PATCH] Add `From` impls for `SocketAddrAny`. (#482) This allows more convenient construction of `SocketAddrAny` values. --- src/net/socket_addr_any.rs | 22 ++++++++++++++++++++++ tests/net/connect_bind_send.rs | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/net/socket_addr_any.rs b/src/net/socket_addr_any.rs index 287c6c1ee..403ad11c2 100644 --- a/src/net/socket_addr_any.rs +++ b/src/net/socket_addr_any.rs @@ -32,6 +32,28 @@ pub enum SocketAddrAny { Unix(SocketAddrUnix), } +impl From for SocketAddrAny { + #[inline] + fn from(from: SocketAddrV4) -> Self { + Self::V4(from) + } +} + +impl From for SocketAddrAny { + #[inline] + fn from(from: SocketAddrV6) -> Self { + Self::V6(from) + } +} + +#[cfg(unix)] +impl From for SocketAddrAny { + #[inline] + fn from(from: SocketAddrUnix) -> Self { + Self::Unix(from) + } +} + impl SocketAddrAny { /// Return the address family of this socket address. #[inline] diff --git a/tests/net/connect_bind_send.rs b/tests/net/connect_bind_send.rs index 84720975f..96436211a 100644 --- a/tests/net/connect_bind_send.rs +++ b/tests/net/connect_bind_send.rs @@ -158,7 +158,7 @@ fn net_v6_connect() -> std::io::Result<()> { #[test] fn net_v4_bind_any() -> std::io::Result<()> { let localhost = Ipv4Addr::LOCALHOST; - let addr = SocketAddrAny::V4(SocketAddrV4::new(localhost, 0)); + let addr = SocketAddrV4::new(localhost, 0).into(); let listener = rustix::net::socket(AddressFamily::INET, SocketType::STREAM, Protocol::default())?; rustix::net::bind_any(&listener, &addr).expect("bind");