]> git.proxmox.com Git - rustc.git/blame - vendor/io-lifetimes/src/impls_socket2.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / vendor / io-lifetimes / src / impls_socket2.rs
CommitLineData
064997fb
FG
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
064997fb
FG
5#[cfg(any(unix, target_os = "wasi"))]
6use crate::{AsFd, BorrowedFd, FromFd, IntoFd, OwnedFd};
7#[cfg(windows)]
8use crate::{AsSocket, BorrowedSocket, FromSocket, IntoSocket, OwnedSocket};
9#[cfg(unix)]
10use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
11#[cfg(target_os = "wasi")]
12use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd};
13#[cfg(windows)]
14use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket};
15
064997fb
FG
16#[cfg(any(unix, target_os = "wasi"))]
17impl AsFd for socket2::Socket {
18 #[inline]
19 fn as_fd(&self) -> BorrowedFd<'_> {
20 unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
21 }
22}
23
24#[cfg(windows)]
25impl AsSocket for socket2::Socket {
26 #[inline]
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"))]
33impl IntoFd for socket2::Socket {
34 #[inline]
35 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"))]
41impl From<socket2::Socket> for OwnedFd {
42 #[inline]
43 fn from(owned: socket2::Socket) -> Self {
44 unsafe { OwnedFd::from_raw_fd(owned.into_raw_fd()) }
45 }
46}
47
48#[cfg(windows)]
49impl IntoSocket for socket2::Socket {
50 #[inline]
51 fn into_socket(self) -> OwnedSocket {
52 unsafe { OwnedSocket::from_raw_socket(self.into_raw_socket()) }
53 }
54}
55
56#[cfg(windows)]
57impl From<socket2::Socket> for OwnedSocket {
58 #[inline]
59 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"))]
65impl FromFd for socket2::Socket {
66 #[inline]
67 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"))]
73impl From<OwnedFd> for socket2::Socket {
74 #[inline]
75 fn from(owned: OwnedFd) -> Self {
76 unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
77 }
78}
79
80#[cfg(windows)]
81impl FromSocket for socket2::Socket {
82 #[inline]
83 fn from_socket(owned: OwnedSocket) -> Self {
84 unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
85 }
86}
87
88#[cfg(windows)]
89impl From<OwnedSocket> for socket2::Socket {
90 #[inline]
91 fn from(owned: OwnedSocket) -> Self {
92 unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
93 }
94}