@@ -19,7 +19,7 @@ pub enum File {
19
19
Tokio ( tokio:: fs:: File ) ,
20
20
#[ cfg( target_os = "linux" ) ]
21
21
/// Tokio-uring asynchronous `File`.
22
- Uring ( RawFd ) ,
22
+ Uring ( tokio_uring :: fs :: File ) ,
23
23
}
24
24
25
25
impl File {
@@ -44,13 +44,7 @@ impl File {
44
44
. create ( create)
45
45
. open ( path)
46
46
. await
47
- . map ( |v| {
48
- // Convert the tokio_uring::fs::File object into RawFd(): v.into_raw_fd().
49
- let file = File :: Uring ( v. as_raw_fd ( ) ) ;
50
- std:: mem:: forget ( v) ;
51
- file
52
- } ) ,
53
- _ => panic ! ( "should not happen" ) ,
47
+ . map ( File :: Uring ) ,
54
48
}
55
49
}
56
50
@@ -69,15 +63,7 @@ impl File {
69
63
( res, bufs[ 0 ] )
70
64
}
71
65
#[ cfg( target_os = "linux" ) ]
72
- File :: Uring ( fd) => {
73
- // Safety: we rely on tokio_uring::fs::File internal implementation details.
74
- // It should be implemented as self.async_try_clone().await.unwrap().read_at,
75
- // but that causes two more syscalls.
76
- let file = unsafe { tokio_uring:: fs:: File :: from_raw_fd ( * fd) } ;
77
- let res = file. read_at ( buf, offset) . await ;
78
- std:: mem:: forget ( file) ;
79
- res
80
- }
66
+ File :: Uring ( f) => f. read_at ( buf, offset) . await ,
81
67
}
82
68
}
83
69
@@ -95,15 +81,7 @@ impl File {
95
81
( res, bufs)
96
82
}
97
83
#[ cfg( target_os = "linux" ) ]
98
- File :: Uring ( fd) => {
99
- // Safety: we rely on tokio_uring::fs::File internal implementation details.
100
- // It should be implemented as self.async_try_clone().await.unwrap().readv_at,
101
- // but that causes two more syscalls.
102
- let file = unsafe { tokio_uring:: fs:: File :: from_raw_fd ( * fd) } ;
103
- let res = file. readv_at ( bufs, offset) . await ;
104
- std:: mem:: forget ( file) ;
105
- res
106
- }
84
+ File :: Uring ( f) => f. readv_at ( bufs, offset) . await ,
107
85
}
108
86
}
109
87
@@ -122,15 +100,7 @@ impl File {
122
100
( res, bufs[ 0 ] )
123
101
}
124
102
#[ cfg( target_os = "linux" ) ]
125
- File :: Uring ( fd) => {
126
- // Safety: we rely on tokio_uring::fs::File internal implementation details.
127
- // It should be implemented as self.async_try_clone().await.unwrap().write_at,
128
- // but that causes two more syscalls.
129
- let file = unsafe { tokio_uring:: fs:: File :: from_raw_fd ( * fd) } ;
130
- let res = file. write_at ( buf, offset) . await ;
131
- std:: mem:: forget ( file) ;
132
- res
133
- }
103
+ File :: Uring ( f) => f. write_at ( buf, offset) . await ,
134
104
}
135
105
}
136
106
@@ -148,15 +118,7 @@ impl File {
148
118
( res, bufs)
149
119
}
150
120
#[ cfg( target_os = "linux" ) ]
151
- File :: Uring ( fd) => {
152
- // Safety: we rely on tokio_uring::fs::File internal implementation details.
153
- // It should be implemented as self.async_try_clone().await.unwrap().writev_at,
154
- // but that causes two more syscalls.
155
- let file = unsafe { tokio_uring:: fs:: File :: from_raw_fd ( * fd) } ;
156
- let res = file. writev_at ( bufs, offset) . await ;
157
- std:: mem:: forget ( file) ;
158
- res
159
- }
121
+ File :: Uring ( f) => f. writev_at ( bufs, offset) . await ,
160
122
}
161
123
}
162
124
@@ -174,13 +136,16 @@ impl File {
174
136
match self {
175
137
File :: Tokio ( f) => f. try_clone ( ) . await . map ( File :: Tokio ) ,
176
138
#[ cfg( target_os = "linux" ) ]
177
- File :: Uring ( fd ) => {
139
+ File :: Uring ( f ) => {
178
140
// Safe because file.as_raw_fd() is valid RawFd and we have checked the result.
179
- let fd = unsafe { libc:: dup ( * fd ) } ;
141
+ let fd = unsafe { libc:: dup ( f . as_raw_fd ( ) ) } ;
180
142
if fd < 0 {
181
143
Err ( std:: io:: Error :: last_os_error ( ) )
182
144
} else {
183
- Ok ( File :: Uring ( fd) )
145
+ // Safe because we dup a new raw fd.
146
+ Ok ( File :: Uring ( unsafe {
147
+ tokio_uring:: fs:: File :: from_raw_fd ( fd)
148
+ } ) )
184
149
}
185
150
}
186
151
}
@@ -192,7 +157,7 @@ impl AsRawFd for File {
192
157
match self {
193
158
File :: Tokio ( f) => f. as_raw_fd ( ) ,
194
159
#[ cfg( target_os = "linux" ) ]
195
- File :: Uring ( fd ) => * fd ,
160
+ File :: Uring ( f ) => f . as_raw_fd ( ) ,
196
161
}
197
162
}
198
163
}
@@ -204,15 +169,6 @@ impl Debug for File {
204
169
}
205
170
}
206
171
207
- impl Drop for File {
208
- fn drop ( & mut self ) {
209
- #[ cfg( target_os = "linux" ) ]
210
- if let File :: Uring ( fd) = self {
211
- let _ = unsafe { tokio_uring:: fs:: File :: from_raw_fd ( * fd) } ;
212
- }
213
- }
214
- }
215
-
216
172
/// A simple wrapper over posix `preadv` to deal with `FileVolatileBuf`.
217
173
pub fn preadv ( fd : RawFd , bufs : & mut [ FileVolatileBuf ] , offset : u64 ) -> std:: io:: Result < usize > {
218
174
let iov: Vec < IoSliceMut > = bufs. iter ( ) . map ( |v| v. io_slice_mut ( ) ) . collect ( ) ;
0 commit comments