Skip to content

Commit

Permalink
Improve ergonomics between SockaddrIn and underlying libc type (nix-r…
Browse files Browse the repository at this point in the history
  • Loading branch information
pronebird authored Mar 7, 2024
1 parent 7badbee commit 99f2dc2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
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

0 comments on commit 99f2dc2

Please sign in to comment.