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

Improve ergonomics between SockaddrIn and underlying libc type #2328

Merged
merged 1 commit into from
Mar 7, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/2328.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `From` trait implementation for conversions between `sockaddr_in` and `SockaddrIn`, `sockaddr_in6` and `SockaddrIn6`
27 changes: 27 additions & 0 deletions src/sys/socket/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,19 @@ impl From<SockaddrIn> for net::SocketAddrV4 {
}
}

#[cfg(feature = "net")]
impl From<SockaddrIn> for libc::sockaddr_in {
fn from(sin: SockaddrIn) -> libc::sockaddr_in {
sin.0
}
}
#[cfg(feature = "net")]
impl From<libc::sockaddr_in> for SockaddrIn {
fn from(sin: libc::sockaddr_in) -> SockaddrIn {
SockaddrIn(sin)
}
}

#[cfg(feature = "net")]
impl std::str::FromStr for SockaddrIn {
type Err = net::AddrParseError;
Expand Down Expand Up @@ -969,6 +982,20 @@ impl SockaddrIn6 {
}
}

#[cfg(feature = "net")]
impl From<SockaddrIn6> for libc::sockaddr_in6 {
fn from(sin6: SockaddrIn6) -> libc::sockaddr_in6 {
sin6.0
}
}

#[cfg(feature = "net")]
impl From<libc::sockaddr_in6> for SockaddrIn6 {
fn from(sin6: libc::sockaddr_in6) -> SockaddrIn6 {
SockaddrIn6(sin6)
}
}

#[cfg(feature = "net")]
impl private::SockaddrLikePriv for SockaddrIn6 {}
#[cfg(feature = "net")]
Expand Down
Loading