From 99f2dc2329af7bd154edb237995f414100b787dc Mon Sep 17 00:00:00 2001 From: Andrej Mihajlov Date: Thu, 7 Mar 2024 12:59:37 +0100 Subject: [PATCH] Improve ergonomics between SockaddrIn and underlying libc type (#2328) --- changelog/2328.added.md | 1 + src/sys/socket/addr.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 changelog/2328.added.md diff --git a/changelog/2328.added.md b/changelog/2328.added.md new file mode 100644 index 0000000000..2e3f440f50 --- /dev/null +++ b/changelog/2328.added.md @@ -0,0 +1 @@ +Add `From` trait implementation for conversions between `sockaddr_in` and `SockaddrIn`, `sockaddr_in6` and `SockaddrIn6` diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs index f6800aa5d0..f3c3d980b6 100644 --- a/src/sys/socket/addr.rs +++ b/src/sys/socket/addr.rs @@ -919,6 +919,19 @@ impl From for net::SocketAddrV4 { } } +#[cfg(feature = "net")] +impl From for libc::sockaddr_in { + fn from(sin: SockaddrIn) -> libc::sockaddr_in { + sin.0 + } +} +#[cfg(feature = "net")] +impl From for SockaddrIn { + fn from(sin: libc::sockaddr_in) -> SockaddrIn { + SockaddrIn(sin) + } +} + #[cfg(feature = "net")] impl std::str::FromStr for SockaddrIn { type Err = net::AddrParseError; @@ -969,6 +982,20 @@ impl SockaddrIn6 { } } +#[cfg(feature = "net")] +impl From for libc::sockaddr_in6 { + fn from(sin6: SockaddrIn6) -> libc::sockaddr_in6 { + sin6.0 + } +} + +#[cfg(feature = "net")] +impl From for SockaddrIn6 { + fn from(sin6: libc::sockaddr_in6) -> SockaddrIn6 { + SockaddrIn6(sin6) + } +} + #[cfg(feature = "net")] impl private::SockaddrLikePriv for SockaddrIn6 {} #[cfg(feature = "net")]