Skip to content

Commit fc7e17a

Browse files
committed
Improve ergonomics between SockaddrIn and underlying libc type
1 parent 7badbee commit fc7e17a

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

changelog/2328.added.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add `From` trait implementation for conversions between `sockaddr_in` and `SockaddrIn`, `sockaddr_in6` and `SockaddrIn6`

src/sys/socket/addr.rs

+27
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,19 @@ impl From<SockaddrIn> for net::SocketAddrV4 {
919919
}
920920
}
921921

922+
#[cfg(feature = "net")]
923+
impl From<SockaddrIn> for libc::sockaddr_in {
924+
fn from(sin: SockaddrIn) -> libc::sockaddr_in {
925+
sin.0
926+
}
927+
}
928+
#[cfg(feature = "net")]
929+
impl From<libc::sockaddr_in> for SockaddrIn {
930+
fn from(sin: libc::sockaddr_in) -> SockaddrIn {
931+
SockaddrIn(sin)
932+
}
933+
}
934+
922935
#[cfg(feature = "net")]
923936
impl std::str::FromStr for SockaddrIn {
924937
type Err = net::AddrParseError;
@@ -969,6 +982,20 @@ impl SockaddrIn6 {
969982
}
970983
}
971984

985+
#[cfg(feature = "net")]
986+
impl From<SockaddrIn6> for libc::sockaddr_in6 {
987+
fn from(sin6: SockaddrIn6) -> libc::sockaddr_in6 {
988+
sin6.0
989+
}
990+
}
991+
992+
#[cfg(feature = "net")]
993+
impl From<libc::sockaddr_in6> for SockaddrIn6 {
994+
fn from(sin6: libc::sockaddr_in6) -> SockaddrIn6 {
995+
SockaddrIn6(sin6)
996+
}
997+
}
998+
972999
#[cfg(feature = "net")]
9731000
impl private::SockaddrLikePriv for SockaddrIn6 {}
9741001
#[cfg(feature = "net")]

0 commit comments

Comments
 (0)