Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add From impls for SocketAddrAny. #482

Merged
merged 1 commit into from Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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