Skip to content

Commit d95a88d

Browse files
committed
feat(driver): add from_raw_* support back
1 parent f2d678f commit d95a88d

File tree

10 files changed

+94
-36
lines changed

10 files changed

+94
-36
lines changed

compio-driver/src/fd.rs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#[cfg(unix)]
2+
use std::os::fd::FromRawFd;
3+
#[cfg(windows)]
4+
use std::os::windows::io::{
5+
FromRawHandle, FromRawSocket, OwnedHandle, OwnedSocket, RawHandle, RawSocket,
6+
};
17
use std::{
28
future::{poll_fn, Future},
39
mem::ManuallyDrop,
@@ -99,14 +105,10 @@ impl Drop for SharedFd {
99105
#[doc(hidden)]
100106
impl SharedFd {
101107
pub unsafe fn to_file(&self) -> ManuallyDrop<std::fs::File> {
102-
use std::os::windows::io::FromRawHandle;
103-
104108
ManuallyDrop::new(std::fs::File::from_raw_handle(self.as_raw_fd() as _))
105109
}
106110

107111
pub unsafe fn to_socket(&self) -> ManuallyDrop<socket2::Socket> {
108-
use std::os::windows::io::FromRawSocket;
109-
110112
ManuallyDrop::new(socket2::Socket::from_raw_socket(self.as_raw_fd() as _))
111113
}
112114
}
@@ -115,14 +117,10 @@ impl SharedFd {
115117
#[doc(hidden)]
116118
impl SharedFd {
117119
pub unsafe fn to_file(&self) -> ManuallyDrop<std::fs::File> {
118-
use std::os::fd::FromRawFd;
119-
120120
ManuallyDrop::new(std::fs::File::from_raw_fd(self.as_raw_fd() as _))
121121
}
122122

123123
pub unsafe fn to_socket(&self) -> ManuallyDrop<socket2::Socket> {
124-
use std::os::fd::FromRawFd;
125-
126124
ManuallyDrop::new(socket2::Socket::from_raw_fd(self.as_raw_fd() as _))
127125
}
128126
}
@@ -133,6 +131,27 @@ impl AsRawFd for SharedFd {
133131
}
134132
}
135133

134+
#[cfg(windows)]
135+
impl FromRawHandle for SharedFd {
136+
unsafe fn from_raw_handle(handle: RawHandle) -> Self {
137+
Self::new(OwnedFd::File(OwnedHandle::from_raw_handle(handle)))
138+
}
139+
}
140+
141+
#[cfg(windows)]
142+
impl FromRawSocket for SharedFd {
143+
unsafe fn from_raw_socket(sock: RawSocket) -> Self {
144+
Self::new(OwnedFd::Socket(OwnedSocket::from_raw_socket(sock)))
145+
}
146+
}
147+
148+
#[cfg(unix)]
149+
impl FromRawFd for SharedFd {
150+
unsafe fn from_raw_fd(fd: RawFd) -> Self {
151+
Self::new(OwnedFd::from_raw_fd(fd))
152+
}
153+
}
154+
136155
impl From<OwnedFd> for SharedFd {
137156
fn from(value: OwnedFd) -> Self {
138157
Self::new(value)

compio-driver/src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,42 @@ macro_rules! impl_raw_fd {
114114
self.$inner.as_raw_fd()
115115
}
116116
}
117+
#[cfg(unix)]
118+
impl std::os::fd::FromRawFd for $t {
119+
unsafe fn from_raw_fd(fd: $crate::RawFd) -> Self {
120+
Self {
121+
$inner: std::os::fd::FromRawFd::from_raw_fd(fd),
122+
}
123+
}
124+
}
117125
impl $crate::ToSharedFd for $t {
118126
fn to_shared_fd(&self) -> $crate::SharedFd {
119127
self.$inner.to_shared_fd()
120128
}
121129
}
122130
};
131+
($t:ty, $inner:ident,file) => {
132+
$crate::impl_raw_fd!($t, $inner);
133+
#[cfg(windows)]
134+
impl std::os::windows::io::FromRawHandle for $t {
135+
unsafe fn from_raw_handle(handle: std::os::windows::io::RawHandle) -> Self {
136+
Self {
137+
$inner: std::os::windows::io::FromRawHandle::from_raw_handle(handle),
138+
}
139+
}
140+
}
141+
};
142+
($t:ty, $inner:ident,socket) => {
143+
$crate::impl_raw_fd!($t, $inner);
144+
#[cfg(windows)]
145+
impl std::os::windows::io::FromRawSocket for $t {
146+
unsafe fn from_raw_socket(sock: std::os::windows::io::RawSocket) -> Self {
147+
Self {
148+
$inner: std::os::windows::io::FromRawSocket::from_raw_socket(sock),
149+
}
150+
}
151+
}
152+
};
123153
}
124154

125155
/// The return type of [`Proactor::push`].

compio-fs/src/file.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,4 @@ impl AsyncWriteAt for &File {
214214
}
215215
}
216216

217-
impl_raw_fd!(File, inner);
217+
impl_raw_fd!(File, inner, file);

compio-fs/src/named_pipe.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ impl AsyncWrite for &NamedPipeServer {
229229
}
230230
}
231231

232-
impl_raw_fd!(NamedPipeServer, handle);
232+
impl_raw_fd!(NamedPipeServer, handle, file);
233233

234234
/// A [Windows named pipe] client.
235235
///
@@ -350,7 +350,7 @@ impl AsyncWrite for &NamedPipeClient {
350350
}
351351
}
352352

353-
impl_raw_fd!(NamedPipeClient, handle);
353+
impl_raw_fd!(NamedPipeClient, handle, file);
354354

355355
/// A builder structure for construct a named pipe with named pipe-specific
356356
/// options. This is required to use for named pipe servers who wants to modify

compio-fs/src/pipe.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use compio_buf::{BufResult, IntoInner, IoBuf, IoBufMut, IoVectoredBuf, IoVectore
1111
use compio_driver::{
1212
impl_raw_fd,
1313
op::{BufResultExt, Recv, RecvVectored, Send, SendVectored},
14-
syscall, AsRawFd, RawFd, ToSharedFd,
14+
syscall, AsRawFd, ToSharedFd,
1515
};
1616
use compio_io::{AsyncRead, AsyncWrite};
1717
use compio_runtime::Runtime;
@@ -390,15 +390,7 @@ impl AsyncWrite for &Sender {
390390
}
391391
}
392392

393-
impl_raw_fd!(Sender, file);
394-
395-
impl FromRawFd for Sender {
396-
unsafe fn from_raw_fd(fd: RawFd) -> Self {
397-
Self {
398-
file: File::from_std(std::fs::File::from_raw_fd(fd)).expect("attach should be Ok"),
399-
}
400-
}
401-
}
393+
impl_raw_fd!(Sender, file, file);
402394

403395
/// Reading end of a Unix pipe.
404396
///
@@ -519,15 +511,7 @@ impl AsyncRead for &Receiver {
519511
}
520512
}
521513

522-
impl_raw_fd!(Receiver, file);
523-
524-
impl FromRawFd for Receiver {
525-
unsafe fn from_raw_fd(fd: RawFd) -> Self {
526-
Self {
527-
file: File::from_std(std::fs::File::from_raw_fd(fd)).expect("attach should be Ok"),
528-
}
529-
}
530-
}
514+
impl_raw_fd!(Receiver, file, file);
531515

532516
/// Checks if file is a FIFO
533517
async fn is_fifo(file: &File) -> io::Result<bool> {

compio-net/src/socket.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,4 @@ impl Socket {
274274
}
275275
}
276276

277-
impl_raw_fd!(Socket, socket);
277+
impl_raw_fd!(Socket, socket, socket);

compio-net/src/tcp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl TcpListener {
109109
}
110110
}
111111

112-
impl_raw_fd!(TcpListener, inner);
112+
impl_raw_fd!(TcpListener, inner, socket);
113113

114114
/// A TCP stream between a local and a remote socket.
115115
///
@@ -273,4 +273,4 @@ impl AsyncWrite for &TcpStream {
273273
}
274274
}
275275

276-
impl_raw_fd!(TcpStream, inner);
276+
impl_raw_fd!(TcpStream, inner, socket);

compio-net/src/udp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,4 +251,4 @@ impl UdpSocket {
251251
}
252252
}
253253

254-
impl_raw_fd!(UdpSocket, inner);
254+
impl_raw_fd!(UdpSocket, inner, socket);

compio-net/src/unix.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl UnixListener {
8787
}
8888
}
8989

90-
impl_raw_fd!(UnixListener, inner);
90+
impl_raw_fd!(UnixListener, inner, socket);
9191

9292
/// A Unix stream between two local sockets on Windows & WSL.
9393
///
@@ -257,7 +257,7 @@ impl AsyncWrite for &UnixStream {
257257
}
258258
}
259259

260-
impl_raw_fd!(UnixStream, inner);
260+
impl_raw_fd!(UnixStream, inner, socket);
261261

262262
#[cfg(windows)]
263263
#[inline]

compio-runtime/src/attacher.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#[cfg(unix)]
2+
use std::os::fd::{FromRawFd, RawFd};
3+
#[cfg(windows)]
4+
use std::os::windows::io::{FromRawHandle, FromRawSocket, RawHandle, RawSocket};
15
use std::{
26
io,
37
ops::{Deref, DerefMut},
@@ -52,6 +56,27 @@ impl<S> IntoInner for Attacher<S> {
5256
}
5357
}
5458

59+
#[cfg(windows)]
60+
impl<S: FromRawHandle> FromRawHandle for Attacher<S> {
61+
unsafe fn from_raw_handle(handle: RawHandle) -> Self {
62+
Self::new_unchecked(S::from_raw_handle(handle))
63+
}
64+
}
65+
66+
#[cfg(windows)]
67+
impl<S: FromRawSocket> FromRawSocket for Attacher<S> {
68+
unsafe fn from_raw_socket(sock: RawSocket) -> Self {
69+
Self::new_unchecked(S::from_raw_socket(sock))
70+
}
71+
}
72+
73+
#[cfg(unix)]
74+
impl<S: FromRawFd> FromRawFd for Attacher<S> {
75+
unsafe fn from_raw_fd(fd: RawFd) -> Self {
76+
Self::new_unchecked(S::from_raw_fd(fd))
77+
}
78+
}
79+
5580
impl<S> Deref for Attacher<S> {
5681
type Target = S;
5782

0 commit comments

Comments
 (0)