1
1
#[ cfg( unix) ]
2
2
use std:: os:: fd:: FromRawFd ;
3
3
#[ cfg( windows) ]
4
- use std:: os:: windows:: io:: {
5
- FromRawHandle , FromRawSocket , OwnedHandle , OwnedSocket , RawHandle , RawSocket ,
6
- } ;
4
+ use std:: os:: windows:: io:: { FromRawHandle , FromRawSocket , RawHandle , RawSocket } ;
7
5
use std:: {
8
6
future:: { poll_fn, Future } ,
9
7
mem:: ManuallyDrop ,
8
+ ops:: Deref ,
10
9
panic:: RefUnwindSafe ,
11
10
sync:: {
12
11
atomic:: { AtomicBool , Ordering } ,
@@ -17,35 +16,35 @@ use std::{
17
16
18
17
use futures_util:: task:: AtomicWaker ;
19
18
20
- use crate :: { AsRawFd , OwnedFd , RawFd } ;
19
+ use crate :: { AsRawFd , RawFd } ;
21
20
22
21
#[ derive( Debug ) ]
23
- struct Inner {
24
- fd : OwnedFd ,
22
+ struct Inner < T > {
23
+ fd : T ,
25
24
// whether there is a future waiting
26
25
waits : AtomicBool ,
27
26
waker : AtomicWaker ,
28
27
}
29
28
30
- impl RefUnwindSafe for Inner { }
29
+ impl < T > RefUnwindSafe for Inner < T > { }
31
30
32
31
/// A shared fd. It is passed to the operations to make sure the fd won't be
33
32
/// closed before the operations complete.
34
- #[ derive( Debug , Clone ) ]
35
- pub struct SharedFd ( Arc < Inner > ) ;
33
+ #[ derive( Debug ) ]
34
+ pub struct SharedFd < T > ( Arc < Inner < T > > ) ;
36
35
37
- impl SharedFd {
36
+ impl < T > SharedFd < T > {
38
37
/// Create the shared fd from an owned fd.
39
- pub fn new ( fd : impl Into < OwnedFd > ) -> Self {
38
+ pub fn new ( fd : T ) -> Self {
40
39
Self ( Arc :: new ( Inner {
41
- fd : fd . into ( ) ,
40
+ fd,
42
41
waits : AtomicBool :: new ( false ) ,
43
42
waker : AtomicWaker :: new ( ) ,
44
43
} ) )
45
44
}
46
45
47
46
/// Try to take the inner owned fd.
48
- pub fn try_unwrap ( self ) -> Result < OwnedFd , Self > {
47
+ pub fn try_unwrap ( self ) -> Result < T , Self > {
49
48
let this = ManuallyDrop :: new ( self ) ;
50
49
if let Some ( fd) = unsafe { Self :: try_unwrap_inner ( & this) } {
51
50
Ok ( fd)
@@ -55,7 +54,7 @@ impl SharedFd {
55
54
}
56
55
57
56
// SAFETY: if `Some` is returned, the method should not be called again.
58
- unsafe fn try_unwrap_inner ( this : & ManuallyDrop < Self > ) -> Option < OwnedFd > {
57
+ unsafe fn try_unwrap_inner ( this : & ManuallyDrop < Self > ) -> Option < T > {
59
58
let ptr = ManuallyDrop :: new ( std:: ptr:: read ( & this. 0 ) ) ;
60
59
// The ptr is duplicated without increasing the strong count, should forget.
61
60
match Arc :: try_unwrap ( ManuallyDrop :: into_inner ( ptr) ) {
@@ -68,7 +67,7 @@ impl SharedFd {
68
67
}
69
68
70
69
/// Wait and take the inner owned fd.
71
- pub fn take ( self ) -> impl Future < Output = Option < OwnedFd > > {
70
+ pub fn take ( self ) -> impl Future < Output = Option < T > > {
72
71
let this = ManuallyDrop :: new ( self ) ;
73
72
async move {
74
73
if !this. 0 . waits . swap ( true , Ordering :: AcqRel ) {
@@ -93,7 +92,7 @@ impl SharedFd {
93
92
}
94
93
}
95
94
96
- impl Drop for SharedFd {
95
+ impl < T > Drop for SharedFd < T > {
97
96
fn drop ( & mut self ) {
98
97
// It's OK to wake multiple times.
99
98
if Arc :: strong_count ( & self . 0 ) == 2 {
@@ -102,71 +101,61 @@ impl Drop for SharedFd {
102
101
}
103
102
}
104
103
105
- #[ cfg( windows) ]
106
- #[ doc( hidden) ]
107
- impl SharedFd {
108
- pub unsafe fn to_file ( & self ) -> ManuallyDrop < std:: fs:: File > {
109
- ManuallyDrop :: new ( std:: fs:: File :: from_raw_handle ( self . as_raw_fd ( ) as _ ) )
110
- }
111
-
112
- pub unsafe fn to_socket ( & self ) -> ManuallyDrop < socket2:: Socket > {
113
- ManuallyDrop :: new ( socket2:: Socket :: from_raw_socket ( self . as_raw_fd ( ) as _ ) )
114
- }
115
- }
116
-
117
- #[ cfg( unix) ]
118
- #[ doc( hidden) ]
119
- impl SharedFd {
120
- pub unsafe fn to_file ( & self ) -> ManuallyDrop < std:: fs:: File > {
121
- ManuallyDrop :: new ( std:: fs:: File :: from_raw_fd ( self . as_raw_fd ( ) as _ ) )
122
- }
123
-
124
- pub unsafe fn to_socket ( & self ) -> ManuallyDrop < socket2:: Socket > {
125
- ManuallyDrop :: new ( socket2:: Socket :: from_raw_fd ( self . as_raw_fd ( ) as _ ) )
126
- }
127
- }
128
-
129
- impl AsRawFd for SharedFd {
104
+ impl < T : AsRawFd > AsRawFd for SharedFd < T > {
130
105
fn as_raw_fd ( & self ) -> RawFd {
131
106
self . 0 . fd . as_raw_fd ( )
132
107
}
133
108
}
134
109
135
110
#[ cfg( windows) ]
136
- impl FromRawHandle for SharedFd {
111
+ impl < T : FromRawHandle > FromRawHandle for SharedFd < T > {
137
112
unsafe fn from_raw_handle ( handle : RawHandle ) -> Self {
138
- Self :: new ( OwnedFd :: File ( OwnedHandle :: from_raw_handle ( handle) ) )
113
+ Self :: new ( T :: from_raw_handle ( handle) )
139
114
}
140
115
}
141
116
142
117
#[ cfg( windows) ]
143
- impl FromRawSocket for SharedFd {
118
+ impl < T : FromRawSocket > FromRawSocket for SharedFd < T > {
144
119
unsafe fn from_raw_socket ( sock : RawSocket ) -> Self {
145
- Self :: new ( OwnedFd :: Socket ( OwnedSocket :: from_raw_socket ( sock) ) )
120
+ Self :: new ( T :: from_raw_socket ( sock) )
146
121
}
147
122
}
148
123
149
124
#[ cfg( unix) ]
150
- impl FromRawFd for SharedFd {
125
+ impl < T : FromRawFd > FromRawFd for SharedFd < T > {
151
126
unsafe fn from_raw_fd ( fd : RawFd ) -> Self {
152
- Self :: new ( OwnedFd :: from_raw_fd ( fd) )
127
+ Self :: new ( T :: from_raw_fd ( fd) )
153
128
}
154
129
}
155
130
156
- impl From < OwnedFd > for SharedFd {
157
- fn from ( value : OwnedFd ) -> Self {
131
+ impl < T > From < T > for SharedFd < T > {
132
+ fn from ( value : T ) -> Self {
158
133
Self :: new ( value)
159
134
}
160
135
}
161
136
137
+ impl < T > Clone for SharedFd < T > {
138
+ fn clone ( & self ) -> Self {
139
+ Self ( self . 0 . clone ( ) )
140
+ }
141
+ }
142
+
143
+ impl < T > Deref for SharedFd < T > {
144
+ type Target = T ;
145
+
146
+ fn deref ( & self ) -> & Self :: Target {
147
+ & self . 0 . fd
148
+ }
149
+ }
150
+
162
151
/// Get a clone of [`SharedFd`].
163
- pub trait ToSharedFd {
152
+ pub trait ToSharedFd < T > {
164
153
/// Return a cloned [`SharedFd`].
165
- fn to_shared_fd ( & self ) -> SharedFd ;
154
+ fn to_shared_fd ( & self ) -> SharedFd < T > ;
166
155
}
167
156
168
- impl ToSharedFd for SharedFd {
169
- fn to_shared_fd ( & self ) -> SharedFd {
157
+ impl < T > ToSharedFd < T > for SharedFd < T > {
158
+ fn to_shared_fd ( & self ) -> SharedFd < T > {
170
159
self . clone ( )
171
160
}
172
161
}
0 commit comments