Skip to content

Commit

Permalink
Add From impls for SocketAddrAny. (#482)
Browse files Browse the repository at this point in the history
This allows more convenient construction of `SocketAddrAny` values.
  • Loading branch information
sunfishcode committed Dec 13, 2022
1 parent cb074ea commit a493c37
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/net/socket_addr_any.rs
Expand Up @@ -32,6 +32,28 @@ pub enum SocketAddrAny {
Unix(SocketAddrUnix),
}

impl From<SocketAddrV4> for SocketAddrAny {
#[inline]
fn from(from: SocketAddrV4) -> Self {
Self::V4(from)
}
}

impl From<SocketAddrV6> for SocketAddrAny {
#[inline]
fn from(from: SocketAddrV6) -> Self {
Self::V6(from)
}
}

#[cfg(unix)]
impl From<SocketAddrUnix> for SocketAddrAny {
#[inline]
fn from(from: SocketAddrUnix) -> Self {
Self::Unix(from)
}
}

impl SocketAddrAny {
/// Return the address family of this socket address.
#[inline]
Expand Down
2 changes: 1 addition & 1 deletion tests/net/connect_bind_send.rs
Expand Up @@ -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");
Expand Down

0 comments on commit a493c37

Please sign in to comment.