|
| 1 | +use super::*; |
| 2 | + |
| 3 | +/// On windows, handle and socket are in the same size. |
| 4 | +/// Both of them could be attached to an IOCP. |
| 5 | +/// Therefore, both could be seen as fd. |
| 6 | +pub type RawFd = HANDLE; |
| 7 | + |
| 8 | +/// Extracts raw fds. |
| 9 | +pub trait AsRawFd { |
| 10 | + /// Extracts the raw fd. |
| 11 | + fn as_raw_fd(&self) -> RawFd; |
| 12 | +} |
| 13 | + |
| 14 | +/// Owned handle or socket on Windows. |
| 15 | +#[derive(Debug)] |
| 16 | +pub enum OwnedFd { |
| 17 | + /// Win32 handle. |
| 18 | + File(OwnedHandle), |
| 19 | + /// Windows socket handle. |
| 20 | + Socket(OwnedSocket), |
| 21 | +} |
| 22 | + |
| 23 | +impl AsRawFd for OwnedFd { |
| 24 | + fn as_raw_fd(&self) -> RawFd { |
| 25 | + match self { |
| 26 | + Self::File(fd) => fd.as_raw_handle() as _, |
| 27 | + Self::Socket(s) => s.as_raw_socket() as _, |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +impl AsRawFd for RawFd { |
| 33 | + fn as_raw_fd(&self) -> RawFd { |
| 34 | + *self |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl AsRawFd for std::fs::File { |
| 39 | + fn as_raw_fd(&self) -> RawFd { |
| 40 | + self.as_raw_handle() as _ |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl AsRawFd for OwnedHandle { |
| 45 | + fn as_raw_fd(&self) -> RawFd { |
| 46 | + self.as_raw_handle() as _ |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl AsRawFd for socket2::Socket { |
| 51 | + fn as_raw_fd(&self) -> RawFd { |
| 52 | + self.as_raw_socket() as _ |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl AsRawFd for OwnedSocket { |
| 57 | + fn as_raw_fd(&self) -> RawFd { |
| 58 | + self.as_raw_socket() as _ |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +impl AsRawFd for std::process::ChildStdin { |
| 63 | + fn as_raw_fd(&self) -> RawFd { |
| 64 | + self.as_raw_handle() as _ |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +impl AsRawFd for std::process::ChildStdout { |
| 69 | + fn as_raw_fd(&self) -> RawFd { |
| 70 | + self.as_raw_handle() as _ |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +impl AsRawFd for std::process::ChildStderr { |
| 75 | + fn as_raw_fd(&self) -> RawFd { |
| 76 | + self.as_raw_handle() as _ |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl From<OwnedHandle> for OwnedFd { |
| 81 | + fn from(value: OwnedHandle) -> Self { |
| 82 | + Self::File(value) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +impl From<std::fs::File> for OwnedFd { |
| 87 | + fn from(value: std::fs::File) -> Self { |
| 88 | + Self::File(OwnedHandle::from(value)) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl From<std::process::ChildStdin> for OwnedFd { |
| 93 | + fn from(value: std::process::ChildStdin) -> Self { |
| 94 | + Self::File(OwnedHandle::from(value)) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +impl From<std::process::ChildStdout> for OwnedFd { |
| 99 | + fn from(value: std::process::ChildStdout) -> Self { |
| 100 | + Self::File(OwnedHandle::from(value)) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +impl From<std::process::ChildStderr> for OwnedFd { |
| 105 | + fn from(value: std::process::ChildStderr) -> Self { |
| 106 | + Self::File(OwnedHandle::from(value)) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +impl From<OwnedSocket> for OwnedFd { |
| 111 | + fn from(value: OwnedSocket) -> Self { |
| 112 | + Self::Socket(value) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +impl From<socket2::Socket> for OwnedFd { |
| 117 | + fn from(value: socket2::Socket) -> Self { |
| 118 | + Self::Socket(OwnedSocket::from(value)) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +/// Borrowed handle or socket on Windows. |
| 123 | +#[derive(Debug)] |
| 124 | +pub enum BorrowedFd<'a> { |
| 125 | + /// Win32 handle. |
| 126 | + File(BorrowedHandle<'a>), |
| 127 | + /// Windows socket handle. |
| 128 | + Socket(BorrowedSocket<'a>), |
| 129 | +} |
| 130 | + |
| 131 | +impl AsRawFd for BorrowedFd<'_> { |
| 132 | + fn as_raw_fd(&self) -> RawFd { |
| 133 | + match self { |
| 134 | + Self::File(fd) => fd.as_raw_handle() as RawFd, |
| 135 | + Self::Socket(s) => s.as_raw_socket() as RawFd, |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +impl<'a> From<BorrowedHandle<'a>> for BorrowedFd<'a> { |
| 141 | + fn from(value: BorrowedHandle<'a>) -> Self { |
| 142 | + Self::File(value) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +impl<'a> From<BorrowedSocket<'a>> for BorrowedFd<'a> { |
| 147 | + fn from(value: BorrowedSocket<'a>) -> Self { |
| 148 | + Self::Socket(value) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +/// Extracts fds. |
| 153 | +pub trait AsFd { |
| 154 | + /// Extracts the borrowed fd. |
| 155 | + fn as_fd(&self) -> BorrowedFd<'_>; |
| 156 | +} |
| 157 | + |
| 158 | +impl AsFd for OwnedFd { |
| 159 | + fn as_fd(&self) -> BorrowedFd<'_> { |
| 160 | + match self { |
| 161 | + Self::File(fd) => fd.as_fd(), |
| 162 | + Self::Socket(s) => s.as_fd(), |
| 163 | + } |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +impl AsFd for std::fs::File { |
| 168 | + fn as_fd(&self) -> BorrowedFd<'_> { |
| 169 | + self.as_handle().into() |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +impl AsFd for OwnedHandle { |
| 174 | + fn as_fd(&self) -> BorrowedFd<'_> { |
| 175 | + self.as_handle().into() |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +impl AsFd for BorrowedHandle<'_> { |
| 180 | + fn as_fd(&self) -> BorrowedFd<'_> { |
| 181 | + (*self).into() |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +impl AsFd for socket2::Socket { |
| 186 | + fn as_fd(&self) -> BorrowedFd<'_> { |
| 187 | + self.as_socket().into() |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +impl AsFd for OwnedSocket { |
| 192 | + fn as_fd(&self) -> BorrowedFd<'_> { |
| 193 | + self.as_socket().into() |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +impl AsFd for BorrowedSocket<'_> { |
| 198 | + fn as_fd(&self) -> BorrowedFd<'_> { |
| 199 | + (*self).into() |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +impl AsFd for std::process::ChildStdin { |
| 204 | + fn as_fd(&self) -> BorrowedFd<'_> { |
| 205 | + self.as_handle().into() |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +impl AsFd for std::process::ChildStdout { |
| 210 | + fn as_fd(&self) -> BorrowedFd<'_> { |
| 211 | + self.as_handle().into() |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +impl AsFd for std::process::ChildStderr { |
| 216 | + fn as_fd(&self) -> BorrowedFd<'_> { |
| 217 | + self.as_handle().into() |
| 218 | + } |
| 219 | +} |
0 commit comments