Skip to content

Commit a493c37

Browse files
authored
Add From impls for SocketAddrAny. (#482)
This allows more convenient construction of `SocketAddrAny` values.
1 parent cb074ea commit a493c37

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/net/socket_addr_any.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,28 @@ pub enum SocketAddrAny {
3232
Unix(SocketAddrUnix),
3333
}
3434

35+
impl From<SocketAddrV4> for SocketAddrAny {
36+
#[inline]
37+
fn from(from: SocketAddrV4) -> Self {
38+
Self::V4(from)
39+
}
40+
}
41+
42+
impl From<SocketAddrV6> for SocketAddrAny {
43+
#[inline]
44+
fn from(from: SocketAddrV6) -> Self {
45+
Self::V6(from)
46+
}
47+
}
48+
49+
#[cfg(unix)]
50+
impl From<SocketAddrUnix> for SocketAddrAny {
51+
#[inline]
52+
fn from(from: SocketAddrUnix) -> Self {
53+
Self::Unix(from)
54+
}
55+
}
56+
3557
impl SocketAddrAny {
3658
/// Return the address family of this socket address.
3759
#[inline]

tests/net/connect_bind_send.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ fn net_v6_connect() -> std::io::Result<()> {
158158
#[test]
159159
fn net_v4_bind_any() -> std::io::Result<()> {
160160
let localhost = Ipv4Addr::LOCALHOST;
161-
let addr = SocketAddrAny::V4(SocketAddrV4::new(localhost, 0));
161+
let addr = SocketAddrV4::new(localhost, 0).into();
162162
let listener =
163163
rustix::net::socket(AddressFamily::INET, SocketType::STREAM, Protocol::default())?;
164164
rustix::net::bind_any(&listener, &addr).expect("bind");

0 commit comments

Comments
 (0)