• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! Implementations of io-lifetimes' traits for socket2's types. In the
2 //! future, we'll prefer to have crates provide their own impls; this is
3 //! just a temporary measure.
4 
5 #[cfg(any(unix, target_os = "wasi"))]
6 use crate::{AsFd, BorrowedFd, FromFd, IntoFd, OwnedFd};
7 #[cfg(windows)]
8 use crate::{AsSocket, BorrowedSocket, FromSocket, IntoSocket, OwnedSocket};
9 #[cfg(unix)]
10 use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
11 #[cfg(target_os = "wasi")]
12 use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd};
13 #[cfg(windows)]
14 use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket};
15 
16 #[cfg(any(unix, target_os = "wasi"))]
17 impl AsFd for socket2::Socket {
18     #[inline]
as_fd(&self) -> BorrowedFd<'_>19     fn as_fd(&self) -> BorrowedFd<'_> {
20         unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
21     }
22 }
23 
24 #[cfg(windows)]
25 impl AsSocket for socket2::Socket {
26     #[inline]
as_socket(&self) -> BorrowedSocket<'_>27     fn as_socket(&self) -> BorrowedSocket<'_> {
28         unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
29     }
30 }
31 
32 #[cfg(any(unix, target_os = "wasi"))]
33 impl IntoFd for socket2::Socket {
34     #[inline]
into_fd(self) -> OwnedFd35     fn into_fd(self) -> OwnedFd {
36         unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
37     }
38 }
39 
40 #[cfg(any(unix, target_os = "wasi"))]
41 impl From<socket2::Socket> for OwnedFd {
42     #[inline]
from(owned: socket2::Socket) -> Self43     fn from(owned: socket2::Socket) -> Self {
44         unsafe { OwnedFd::from_raw_fd(owned.into_raw_fd()) }
45     }
46 }
47 
48 #[cfg(windows)]
49 impl IntoSocket for socket2::Socket {
50     #[inline]
into_socket(self) -> OwnedSocket51     fn into_socket(self) -> OwnedSocket {
52         unsafe { OwnedSocket::from_raw_socket(self.into_raw_socket()) }
53     }
54 }
55 
56 #[cfg(windows)]
57 impl From<socket2::Socket> for OwnedSocket {
58     #[inline]
from(owned: socket2::Socket) -> Self59     fn from(owned: socket2::Socket) -> Self {
60         unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
61     }
62 }
63 
64 #[cfg(any(unix, target_os = "wasi"))]
65 impl FromFd for socket2::Socket {
66     #[inline]
from_fd(owned: OwnedFd) -> Self67     fn from_fd(owned: OwnedFd) -> Self {
68         unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
69     }
70 }
71 
72 #[cfg(any(unix, target_os = "wasi"))]
73 impl From<OwnedFd> for socket2::Socket {
74     #[inline]
from(owned: OwnedFd) -> Self75     fn from(owned: OwnedFd) -> Self {
76         unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
77     }
78 }
79 
80 #[cfg(windows)]
81 impl FromSocket for socket2::Socket {
82     #[inline]
from_socket(owned: OwnedSocket) -> Self83     fn from_socket(owned: OwnedSocket) -> Self {
84         unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
85     }
86 }
87 
88 #[cfg(windows)]
89 impl From<OwnedSocket> for socket2::Socket {
90     #[inline]
from(owned: OwnedSocket) -> Self91     fn from(owned: OwnedSocket) -> Self {
92         unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
93     }
94 }
95