Skip to content

Commit e4621c6

Browse files
committed
Impl AsRawHandleOrSocket and IntoRawHandleOrSocket for more types.
1 parent 9bb968b commit e4621c6

File tree

14 files changed

+286
-24
lines changed

14 files changed

+286
-24
lines changed

cap-async-std/src/fs/dir.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use {
2525
use {
2626
async_std::os::windows::io::{AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle},
2727
cap_primitives::fs::{symlink_dir, symlink_file},
28+
unsafe_io::os::windows::{AsRawHandleOrSocket, IntoRawHandleOrSocket, RawHandleOrSocket},
2829
};
2930

3031
/// A reference to an open directory on a filesystem.
@@ -622,6 +623,14 @@ impl AsRawHandle for Dir {
622623
}
623624
}
624625

626+
#[cfg(windows)]
627+
impl AsRawHandleOrSocket for Dir {
628+
#[inline]
629+
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
630+
self.std_file.as_raw_handle_or_socket()
631+
}
632+
}
633+
625634
#[cfg(not(windows))]
626635
impl IntoRawFd for Dir {
627636
#[inline]
@@ -638,6 +647,14 @@ impl IntoRawHandle for Dir {
638647
}
639648
}
640649

650+
#[cfg(windows)]
651+
impl IntoRawHandleOrSocket for Dir {
652+
#[inline]
653+
fn into_raw_handle_or_socket(self) -> RawHandleOrSocket {
654+
self.std_file.into_raw_handle_or_socket()
655+
}
656+
}
657+
641658
// Safety: `Dir` wraps a `fs::File` which owns its handle.
642659
unsafe impl OwnsRaw for Dir {}
643660

cap-async-std/src/fs/file.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ use crate::fs::{Metadata, Permissions};
33
use async_std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
44
#[cfg(target_os = "wasi")]
55
use async_std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
6-
#[cfg(windows)]
7-
use async_std::os::windows::io::{AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle};
86
use async_std::{
97
fs,
108
io::{self, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write},
@@ -13,6 +11,11 @@ use async_std::{
1311
use cap_primitives::fs::is_file_read_write;
1412
use std::{fmt, pin::Pin};
1513
use unsafe_io::{AsUnsafeFile, OwnsRaw};
14+
#[cfg(windows)]
15+
use {
16+
async_std::os::windows::io::{AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle},
17+
unsafe_io::os::windows::{AsRawHandleOrSocket, IntoRawHandleOrSocket, RawHandleOrSocket},
18+
};
1619

1720
/// A reference to an open file on a filesystem.
1821
///
@@ -160,6 +163,14 @@ impl AsRawHandle for File {
160163
}
161164
}
162165

166+
#[cfg(windows)]
167+
impl AsRawHandleOrSocket for File {
168+
#[inline]
169+
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
170+
self.std.as_raw_handle_or_socket()
171+
}
172+
}
173+
163174
#[cfg(not(windows))]
164175
impl IntoRawFd for File {
165176
#[inline]
@@ -176,6 +187,14 @@ impl IntoRawHandle for File {
176187
}
177188
}
178189

190+
#[cfg(windows)]
191+
impl IntoRawHandleOrSocket for File {
192+
#[inline]
193+
fn into_raw_handle_or_socket(self) -> RawHandleOrSocket {
194+
self.std.into_raw_handle_or_socket()
195+
}
196+
}
197+
179198
// Safety: `File` wraps a `fs::File` which owns its handle.
180199
unsafe impl OwnsRaw for File {}
181200

cap-async-std/src/fs_utf8/dir.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ use crate::{
22
fs::{OpenOptions, Permissions},
33
fs_utf8::{from_utf8, to_utf8, DirBuilder, File, Metadata, ReadDir},
44
};
5-
#[cfg(windows)]
6-
use async_std::os::windows::io::{AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle};
75
use async_std::{fs, io};
86
use std::fmt;
97
use unsafe_io::OwnsRaw;
@@ -12,6 +10,11 @@ use {
1210
crate::os::unix::net::{UnixDatagram, UnixListener, UnixStream},
1311
async_std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd},
1412
};
13+
#[cfg(windows)]
14+
use {
15+
async_std::os::windows::io::{AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle},
16+
unsafe_io::os::windows::{AsRawHandleOrSocket, IntoRawHandleOrSocket, RawHandleOrSocket},
17+
};
1518

1619
/// A reference to an open directory on a filesystem.
1720
///
@@ -538,6 +541,14 @@ impl AsRawHandle for Dir {
538541
}
539542
}
540543

544+
#[cfg(windows)]
545+
impl AsRawHandleOrSocket for Dir {
546+
#[inline]
547+
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
548+
self.cap_std.as_raw_handle_or_socket()
549+
}
550+
}
551+
541552
#[cfg(not(windows))]
542553
impl IntoRawFd for Dir {
543554
#[inline]
@@ -554,6 +565,14 @@ impl IntoRawHandle for Dir {
554565
}
555566
}
556567

568+
#[cfg(windows)]
569+
impl IntoRawHandleOrSocket for Dir {
570+
#[inline]
571+
fn into_raw_handle_or_socket(self) -> RawHandleOrSocket {
572+
self.cap_std.into_raw_handle_or_socket()
573+
}
574+
}
575+
557576
// Safety: `Dir` wraps a `fs::File` which owns its handle.
558577
unsafe impl OwnsRaw for Dir {}
559578

cap-async-std/src/fs_utf8/file.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ use crate::fs::{Metadata, Permissions};
33
use async_std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
44
#[cfg(target_os = "wasi")]
55
use async_std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
6-
#[cfg(windows)]
7-
use async_std::os::windows::io::{AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle};
86
use async_std::{
97
fs,
108
io::{self, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write},
119
task::{Context, Poll},
1210
};
1311
use std::{fmt, pin::Pin};
1412
use unsafe_io::OwnsRaw;
13+
#[cfg(windows)]
14+
use {
15+
async_std::os::windows::io::{AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle},
16+
unsafe_io::os::windows::{AsRawHandleOrSocket, IntoRawHandleOrSocket, RawHandleOrSocket},
17+
};
1518

1619
/// A reference to an open file on a filesystem.
1720
///
@@ -131,6 +134,14 @@ impl AsRawHandle for File {
131134
}
132135
}
133136

137+
#[cfg(windows)]
138+
impl AsRawHandleOrSocket for File {
139+
#[inline]
140+
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
141+
self.cap_std.as_raw_handle_or_socket()
142+
}
143+
}
144+
134145
#[cfg(not(windows))]
135146
impl IntoRawFd for File {
136147
#[inline]
@@ -147,6 +158,14 @@ impl IntoRawHandle for File {
147158
}
148159
}
149160

161+
#[cfg(windows)]
162+
impl IntoRawHandleOrSocket for File {
163+
#[inline]
164+
fn into_raw_handle_or_socket(self) -> RawHandleOrSocket {
165+
self.cap_std.into_raw_handle_or_socket()
166+
}
167+
}
168+
150169
// Safety: `File` wraps a `fs::File` which owns its handle.
151170
unsafe impl OwnsRaw for File {}
152171

cap-async-std/src/net/tcp_listener.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
use crate::net::{Incoming, SocketAddr, TcpStream};
22
#[cfg(unix)]
33
use async_std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
4-
#[cfg(windows)]
5-
use async_std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};
64
use async_std::{io, net};
75
use unsafe_io::OwnsRaw;
6+
#[cfg(windows)]
7+
use {
8+
async_std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket},
9+
unsafe_io::os::windows::{AsRawHandleOrSocket, IntoRawHandleOrSocket, RawHandleOrSocket},
10+
};
811

912
/// A TCP socket server, listening for connections.
1013
///
@@ -103,6 +106,14 @@ impl AsRawSocket for TcpListener {
103106
}
104107
}
105108

109+
#[cfg(windows)]
110+
impl AsRawHandleOrSocket for TcpListener {
111+
#[inline]
112+
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
113+
self.std.as_raw_handle_or_socket()
114+
}
115+
}
116+
106117
#[cfg(not(windows))]
107118
impl IntoRawFd for TcpListener {
108119
#[inline]
@@ -119,6 +130,14 @@ impl IntoRawSocket for TcpListener {
119130
}
120131
}
121132

133+
#[cfg(windows)]
134+
impl IntoRawHandleOrSocket for TcpListener {
135+
#[inline]
136+
fn into_raw_handle_or_socket(self) -> RawHandleOrSocket {
137+
self.std.into_raw_handle_or_socket()
138+
}
139+
}
140+
122141
/// Safety: `TcpListener` wraps a `net::TcpListener` which owns its handle.
123142
unsafe impl OwnsRaw for TcpListener {}
124143

cap-async-std/src/net/tcp_stream.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
use crate::net::{Shutdown, SocketAddr};
22
#[cfg(unix)]
33
use async_std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
4-
#[cfg(windows)]
5-
use async_std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};
64
use async_std::{
75
io::{self, IoSlice, IoSliceMut, Read, Write},
86
net,
97
task::{Context, Poll},
108
};
119
use std::pin::Pin;
1210
use unsafe_io::OwnsRaw;
11+
#[cfg(windows)]
12+
use {
13+
async_std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket},
14+
unsafe_io::os::windows::{AsRawHandleOrSocket, IntoRawHandleOrSocket, RawHandleOrSocket},
15+
};
1316

1417
/// A TCP stream between a local and a remote socket.
1518
///
@@ -141,6 +144,14 @@ impl AsRawSocket for TcpStream {
141144
}
142145
}
143146

147+
#[cfg(windows)]
148+
impl AsRawHandleOrSocket for TcpStream {
149+
#[inline]
150+
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
151+
self.std.as_raw_handle_or_socket()
152+
}
153+
}
154+
144155
#[cfg(not(windows))]
145156
impl IntoRawFd for TcpStream {
146157
#[inline]
@@ -157,6 +168,14 @@ impl IntoRawSocket for TcpStream {
157168
}
158169
}
159170

171+
#[cfg(windows)]
172+
impl IntoRawHandleOrSocket for TcpStream {
173+
#[inline]
174+
fn into_raw_handle_or_socket(self) -> RawHandleOrSocket {
175+
self.std.into_raw_handle_or_socket()
176+
}
177+
}
178+
160179
// Safety: `TcpStream` wraps a `net::TcpStream` which owns its handle.
161180
unsafe impl OwnsRaw for TcpStream {}
162181

cap-async-std/src/net/udp_socket.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
use crate::net::{Ipv4Addr, Ipv6Addr, SocketAddr};
22
#[cfg(unix)]
33
use async_std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
4-
#[cfg(windows)]
5-
use async_std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};
64
use async_std::{io, net};
75
use unsafe_io::OwnsRaw;
6+
#[cfg(windows)]
7+
use {
8+
async_std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket},
9+
unsafe_io::os::windows::{AsRawHandleOrSocket, IntoRawHandleOrSocket, RawHandleOrSocket},
10+
};
811

912
/// A UDP socket.
1013
///
@@ -244,6 +247,14 @@ impl AsRawSocket for UdpSocket {
244247
}
245248
}
246249

250+
#[cfg(windows)]
251+
impl AsRawHandleOrSocket for UdpSocket {
252+
#[inline]
253+
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
254+
self.std.as_raw_handle_or_socket()
255+
}
256+
}
257+
247258
#[cfg(not(windows))]
248259
impl IntoRawFd for UdpSocket {
249260
#[inline]
@@ -260,6 +271,14 @@ impl IntoRawSocket for UdpSocket {
260271
}
261272
}
262273

274+
#[cfg(windows)]
275+
impl IntoRawHandleOrSocket for UdpSocket {
276+
#[inline]
277+
fn into_raw_handle_or_socket(self) -> RawHandleOrSocket {
278+
self.std.into_raw_handle_or_socket()
279+
}
280+
}
281+
263282
/// Safety: `UdpSocket` wraps a `net::UdpSocket` which owns its handle.
264283
unsafe impl OwnsRaw for UdpSocket {}
265284

cap-std/src/fs/dir.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use {
2323
use {
2424
cap_primitives::fs::{symlink_dir, symlink_file},
2525
std::os::windows::io::{AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle},
26+
unsafe_io::os::windows::{AsRawHandleOrSocket, IntoRawHandleOrSocket, RawHandleOrSocket},
2627
};
2728

2829
/// A reference to an open directory on a filesystem.
@@ -599,6 +600,14 @@ impl AsRawHandle for Dir {
599600
}
600601
}
601602

603+
#[cfg(windows)]
604+
impl AsRawHandleOrSocket for Dir {
605+
#[inline]
606+
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
607+
self.std_file.as_raw_handle_or_socket()
608+
}
609+
}
610+
602611
#[cfg(not(windows))]
603612
impl IntoRawFd for Dir {
604613
#[inline]
@@ -615,6 +624,14 @@ impl IntoRawHandle for Dir {
615624
}
616625
}
617626

627+
#[cfg(windows)]
628+
impl IntoRawHandleOrSocket for Dir {
629+
#[inline]
630+
fn into_raw_handle_or_socket(self) -> RawHandleOrSocket {
631+
self.std_file.into_raw_handle_or_socket()
632+
}
633+
}
634+
618635
// Safety: `Dir` wraps a `fs::File` which owns its handle.
619636
unsafe impl OwnsRaw for Dir {}
620637

0 commit comments

Comments
 (0)