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
6pub use io_lifetimes::{BorrowedSocket as BorrowedFd, OwnedSocket as OwnedFd};
7#[cfg(feature = "std")]
8pub use std::os::windows::io::RawSocket as RawFd;
9pub(crate) use windows_sys::Win32::Networking::WinSock::SOCKET as LibcFd;
10
11// Re-export the `Socket` traits so that users can implement them.
12pub 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
17pub 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
21    fn as_raw_fd(&self) -> RawFd;
22}
23#[cfg(feature = "std")]
24impl<T: std::os::windows::io::AsRawSocket> AsRawFd for T {
25    #[inline]
26    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
34pub 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
38    fn into_raw_fd(self) -> RawFd;
39}
40#[cfg(feature = "std")]
41impl<T: std::os::windows::io::IntoRawSocket> IntoRawFd for T {
42    #[inline]
43    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
51pub 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
55    unsafe fn from_raw_fd(raw_fd: RawFd) -> Self;
56}
57#[cfg(feature = "std")]
58impl<T: std::os::windows::io::FromRawSocket> FromRawFd for T {
59    #[inline]
60    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
68pub trait AsFd {
69    /// An `as_fd` function for Winsock2, where a `Fd` is a `Socket`.
70    fn as_fd(&self) -> BorrowedFd;
71}
72impl<T: AsSocket> AsFd for T {
73    #[inline]
74    fn as_fd(&self) -> BorrowedFd {
75        self.as_socket()
76    }
77}
78