Skip to content

Commit 8f39c27

Browse files
committed
Use as_inet and as_inet6 in SockAddr::as_std
Reduces the amount of unsafe code.
1 parent fd1565a commit 8f39c27

File tree

1 file changed

+3
-10
lines changed

1 file changed

+3
-10
lines changed

src/sockaddr.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,9 @@ impl SockAddr {
5555
/// Returns this address as a `SocketAddr` if it is in the `AF_INET` (IP v4)
5656
/// or `AF_INET6` (IP v6) family, otherwise returns `None`.
5757
pub fn as_std(&self) -> Option<SocketAddr> {
58-
let storage: *const sockaddr_storage = (&self.storage) as *const _;
59-
match self.storage.ss_family as c_int {
60-
AF_INET => Some(SocketAddr::V4(unsafe {
61-
*(storage as *const sockaddr_in as *const _)
62-
})),
63-
AF_INET6 => Some(SocketAddr::V6(unsafe {
64-
*(storage as *const sockaddr_in6 as *const _)
65-
})),
66-
_ => None,
67-
}
58+
self.as_inet()
59+
.map(|a| a.into())
60+
.or_else(|| self.as_inet6().map(|a| a.into()))
6861
}
6962

7063
/// Returns this address as a `SocketAddrV4` if it is in the `AF_INET`

0 commit comments

Comments
 (0)