• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! `io_lifetimes` types for Windows assuming that Fd is Socket.
2 //!
3 //! We can make this assumption since `rustix` supports only `rustix::net` on
4 //! Windows.
5 
6 pub use io_lifetimes::{BorrowedSocket as BorrowedFd, OwnedSocket as OwnedFd};
7 #[cfg(feature = "std")]
8 pub use std::os::windows::io::RawSocket as RawFd;
9 pub(crate) use windows_sys::Win32::Networking::WinSock::SOCKET as LibcFd;
10 
11 // Re-export the `Socket` traits so that users can implement them.
12 pub use io_lifetimes::AsSocket;
13 
14 /// A version of [`AsRawFd`] for use with Winsock2 API.
15 ///
16 /// [`AsRawFd`]: https://doc.rust-lang.org/stable/std/os/unix/io/trait.AsRawFd.html
17 pub trait AsRawFd {
18     /// A version of [`as_raw_fd`] for use with Winsock2 API.
19     ///
20     /// [`as_raw_fd`]: https://doc.rust-lang.org/stable/std/os/unix/io/trait.FromRawFd.html#tymethod.as_raw_fd
as_raw_fd(&self) -> RawFd21     fn as_raw_fd(&self) -> RawFd;
22 }
23 #[cfg(feature = "std")]
24 impl<T: std::os::windows::io::AsRawSocket> AsRawFd for T {
25     #[inline]
as_raw_fd(&self) -> RawFd26     fn as_raw_fd(&self) -> RawFd {
27         self.as_raw_socket()
28     }
29 }
30 
31 /// A version of [`IntoRawFd`] for use with Winsock2 API.
32 ///
33 /// [`IntoRawFd`]: https://doc.rust-lang.org/stable/std/os/unix/io/trait.IntoRawFd.html
34 pub trait IntoRawFd {
35     /// A version of [`into_raw_fd`] for use with Winsock2 API.
36     ///
37     /// [`into_raw_fd`]: https://doc.rust-lang.org/stable/std/os/unix/io/trait.FromRawFd.html#tymethod.into_raw_fd
into_raw_fd(self) -> RawFd38     fn into_raw_fd(self) -> RawFd;
39 }
40 #[cfg(feature = "std")]
41 impl<T: std::os::windows::io::IntoRawSocket> IntoRawFd for T {
42     #[inline]
into_raw_fd(self) -> RawFd43     fn into_raw_fd(self) -> RawFd {
44         self.into_raw_socket()
45     }
46 }
47 
48 /// A version of [`FromRawFd`] for use with Winsock2 API.
49 ///
50 /// [`FromRawFd`]: https://doc.rust-lang.org/stable/std/os/unix/io/trait.FromRawFd.html
51 pub trait FromRawFd {
52     /// A version of [`from_raw_fd`] for use with Winsock2 API.
53     ///
54     /// [`from_raw_fd`]: https://doc.rust-lang.org/stable/std/os/unix/io/trait.FromRawFd.html#tymethod.from_raw_fd
from_raw_fd(raw_fd: RawFd) -> Self55     unsafe fn from_raw_fd(raw_fd: RawFd) -> Self;
56 }
57 #[cfg(feature = "std")]
58 impl<T: std::os::windows::io::FromRawSocket> FromRawFd for T {
59     #[inline]
from_raw_fd(raw_fd: RawFd) -> Self60     unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
61         Self::from_raw_socket(raw_fd)
62     }
63 }
64 
65 /// A version of [`AsFd`] for use with Winsock2 API.
66 ///
67 /// [`AsFd`]: https://doc.rust-lang.org/stable/std/os/unix/io/trait.AsFd.html
68 pub trait AsFd {
69     /// An `as_fd` function for Winsock2, where a `Fd` is a `Socket`.
as_fd(&self) -> BorrowedFd70     fn as_fd(&self) -> BorrowedFd;
71 }
72 impl<T: AsSocket> AsFd for T {
73     #[inline]
as_fd(&self) -> BorrowedFd74     fn as_fd(&self) -> BorrowedFd {
75         self.as_socket()
76     }
77 }
78