1
1
//! Send data from a file to a socket, bypassing userland.
2
2
3
3
use cfg_if:: cfg_if;
4
- use std:: os:: unix:: io:: RawFd ;
4
+ use std:: os:: unix:: io:: { AsFd , AsRawFd } ;
5
5
use std:: ptr;
6
6
7
7
use libc:: { self , off_t} ;
@@ -23,16 +23,23 @@ use crate::Result;
23
23
/// For more information, see [the sendfile(2) man page.](https://man7.org/linux/man-pages/man2/sendfile.2.html)
24
24
#[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
25
25
#[ cfg_attr( docsrs, doc( cfg( all( ) ) ) ) ]
26
- pub fn sendfile (
27
- out_fd : RawFd ,
28
- in_fd : RawFd ,
26
+ pub fn sendfile < F1 : AsFd , F2 : AsFd > (
27
+ out_fd : F1 ,
28
+ in_fd : F2 ,
29
29
offset : Option < & mut off_t > ,
30
30
count : usize ,
31
31
) -> Result < usize > {
32
32
let offset = offset
33
33
. map ( |offset| offset as * mut _ )
34
34
. unwrap_or ( ptr:: null_mut ( ) ) ;
35
- let ret = unsafe { libc:: sendfile ( out_fd, in_fd, offset, count) } ;
35
+ let ret = unsafe {
36
+ libc:: sendfile (
37
+ out_fd. as_fd ( ) . as_raw_fd ( ) ,
38
+ in_fd. as_fd ( ) . as_raw_fd ( ) ,
39
+ offset,
40
+ count,
41
+ )
42
+ } ;
36
43
Errno :: result ( ret) . map ( |r| r as usize )
37
44
}
38
45
@@ -50,16 +57,23 @@ pub fn sendfile(
50
57
/// For more information, see [the sendfile(2) man page.](https://man7.org/linux/man-pages/man2/sendfile.2.html)
51
58
#[ cfg( target_os = "linux" ) ]
52
59
#[ cfg_attr( docsrs, doc( cfg( all( ) ) ) ) ]
53
- pub fn sendfile64 (
54
- out_fd : RawFd ,
55
- in_fd : RawFd ,
60
+ pub fn sendfile64 < F1 : AsFd , F2 : AsFd > (
61
+ out_fd : F1 ,
62
+ in_fd : F2 ,
56
63
offset : Option < & mut libc:: off64_t > ,
57
64
count : usize ,
58
65
) -> Result < usize > {
59
66
let offset = offset
60
67
. map ( |offset| offset as * mut _ )
61
68
. unwrap_or ( ptr:: null_mut ( ) ) ;
62
- let ret = unsafe { libc:: sendfile64 ( out_fd, in_fd, offset, count) } ;
69
+ let ret = unsafe {
70
+ libc:: sendfile64 (
71
+ out_fd. as_fd ( ) . as_raw_fd ( ) ,
72
+ in_fd. as_fd ( ) . as_raw_fd ( ) ,
73
+ offset,
74
+ count,
75
+ )
76
+ } ;
63
77
Errno :: result ( ret) . map ( |r| r as usize )
64
78
}
65
79
@@ -156,9 +170,9 @@ cfg_if! {
156
170
/// For more information, see
157
171
/// [the sendfile(2) man page.](https://www.freebsd.org/cgi/man.cgi?query=sendfile&sektion=2)
158
172
#[ allow( clippy:: too_many_arguments) ]
159
- pub fn sendfile(
160
- in_fd: RawFd ,
161
- out_sock: RawFd ,
173
+ pub fn sendfile< F1 : AsFd , F2 : AsFd > (
174
+ in_fd: F1 ,
175
+ out_sock: F2 ,
162
176
offset: off_t,
163
177
count: Option <usize >,
164
178
headers: Option <& [ & [ u8 ] ] >,
@@ -175,8 +189,8 @@ cfg_if! {
175
189
let hdtr = headers. or( trailers) . map( |_| SendfileHeaderTrailer :: new( headers, trailers) ) ;
176
190
let hdtr_ptr = hdtr. as_ref( ) . map_or( ptr:: null( ) , |s| & s. 0 as * const libc:: sf_hdtr) ;
177
191
let return_code = unsafe {
178
- libc:: sendfile( in_fd,
179
- out_sock,
192
+ libc:: sendfile( in_fd. as_fd ( ) . as_raw_fd ( ) ,
193
+ out_sock. as_fd ( ) . as_raw_fd ( ) ,
180
194
offset,
181
195
count. unwrap_or( 0 ) ,
182
196
hdtr_ptr as * mut libc:: sf_hdtr,
@@ -206,9 +220,9 @@ cfg_if! {
206
220
///
207
221
/// For more information, see
208
222
/// [the sendfile(2) man page.](https://leaf.dragonflybsd.org/cgi/web-man?command=sendfile§ion=2)
209
- pub fn sendfile(
210
- in_fd: RawFd ,
211
- out_sock: RawFd ,
223
+ pub fn sendfile< F1 : AsFd , F2 : AsFd > (
224
+ in_fd: F1 ,
225
+ out_sock: F2 ,
212
226
offset: off_t,
213
227
count: Option <usize >,
214
228
headers: Option <& [ & [ u8 ] ] >,
@@ -218,8 +232,8 @@ cfg_if! {
218
232
let hdtr = headers. or( trailers) . map( |_| SendfileHeaderTrailer :: new( headers, trailers) ) ;
219
233
let hdtr_ptr = hdtr. as_ref( ) . map_or( ptr:: null( ) , |s| & s. 0 as * const libc:: sf_hdtr) ;
220
234
let return_code = unsafe {
221
- libc:: sendfile( in_fd,
222
- out_sock,
235
+ libc:: sendfile( in_fd. as_fd ( ) . as_raw_fd ( ) ,
236
+ out_sock. as_fd ( ) . as_raw_fd ( ) ,
223
237
offset,
224
238
count. unwrap_or( 0 ) ,
225
239
hdtr_ptr as * mut libc:: sf_hdtr,
@@ -252,9 +266,9 @@ cfg_if! {
252
266
///
253
267
/// For more information, see
254
268
/// [the sendfile(2) man page.](https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man2/sendfile.2.html)
255
- pub fn sendfile(
256
- in_fd: RawFd ,
257
- out_sock: RawFd ,
269
+ pub fn sendfile< F1 : AsFd , F2 : AsFd > (
270
+ in_fd: F1 ,
271
+ out_sock: F2 ,
258
272
offset: off_t,
259
273
count: Option <off_t>,
260
274
headers: Option <& [ & [ u8 ] ] >,
@@ -264,8 +278,8 @@ cfg_if! {
264
278
let hdtr = headers. or( trailers) . map( |_| SendfileHeaderTrailer :: new( headers, trailers) ) ;
265
279
let hdtr_ptr = hdtr. as_ref( ) . map_or( ptr:: null( ) , |s| & s. 0 as * const libc:: sf_hdtr) ;
266
280
let return_code = unsafe {
267
- libc:: sendfile( in_fd,
268
- out_sock,
281
+ libc:: sendfile( in_fd. as_fd ( ) . as_raw_fd ( ) ,
282
+ out_sock. as_fd ( ) . as_raw_fd ( ) ,
269
283
offset,
270
284
& mut len as * mut off_t,
271
285
hdtr_ptr as * mut libc:: sf_hdtr,
0 commit comments