@@ -41,6 +41,7 @@ impl Fd {
41
41
}
42
42
}
43
43
44
+ #[ inline]
44
45
pub fn read ( & self , buf : & mut [ u8 ] ) -> std:: io:: Result < usize > {
45
46
let fd = self . as_raw_fd ( ) ;
46
47
let amount = unsafe { libc:: read ( fd, buf. as_mut_ptr ( ) as * mut _ , buf. len ( ) ) } ;
@@ -50,6 +51,26 @@ impl Fd {
50
51
Ok ( amount as usize )
51
52
}
52
53
54
+ #[ allow( dead_code) ]
55
+ #[ inline]
56
+ fn readv ( & self , bufs : & mut [ std:: io:: IoSliceMut < ' _ > ] ) -> std:: io:: Result < usize > {
57
+ if bufs. len ( ) > max_iov ( ) {
58
+ return Err ( std:: io:: Error :: from ( std:: io:: ErrorKind :: InvalidInput ) ) ;
59
+ }
60
+ let amount = unsafe {
61
+ libc:: readv (
62
+ self . as_raw_fd ( ) ,
63
+ bufs. as_mut_ptr ( ) as * mut libc:: iovec as * const libc:: iovec ,
64
+ bufs. len ( ) as libc:: c_int ,
65
+ )
66
+ } ;
67
+ if amount < 0 {
68
+ return Err ( std:: io:: Error :: last_os_error ( ) ) ;
69
+ }
70
+ Ok ( amount as usize )
71
+ }
72
+
73
+ #[ inline]
53
74
pub fn write ( & self , buf : & [ u8 ] ) -> std:: io:: Result < usize > {
54
75
let fd = self . as_raw_fd ( ) ;
55
76
let amount = unsafe { libc:: write ( fd, buf. as_ptr ( ) as * const _ , buf. len ( ) ) } ;
@@ -58,6 +79,25 @@ impl Fd {
58
79
}
59
80
Ok ( amount as usize )
60
81
}
82
+
83
+ #[ allow( dead_code) ]
84
+ #[ inline]
85
+ pub fn writev ( & self , bufs : & [ std:: io:: IoSlice < ' _ > ] ) -> std:: io:: Result < usize > {
86
+ if bufs. len ( ) > max_iov ( ) {
87
+ return Err ( std:: io:: Error :: from ( std:: io:: ErrorKind :: InvalidInput ) ) ;
88
+ }
89
+ let amount = unsafe {
90
+ libc:: writev (
91
+ self . as_raw_fd ( ) ,
92
+ bufs. as_ptr ( ) as * const libc:: iovec ,
93
+ bufs. len ( ) as libc:: c_int ,
94
+ )
95
+ } ;
96
+ if amount < 0 {
97
+ return Err ( std:: io:: Error :: last_os_error ( ) ) ;
98
+ }
99
+ Ok ( amount as usize )
100
+ }
61
101
}
62
102
63
103
impl AsRawFd for Fd {
@@ -81,3 +121,24 @@ impl Drop for Fd {
81
121
}
82
122
}
83
123
}
124
+
125
+ #[ cfg( any(
126
+ target_os = "dragonfly" ,
127
+ target_os = "freebsd" ,
128
+ target_os = "netbsd" ,
129
+ target_os = "openbsd" ,
130
+ target_vendor = "apple" ,
131
+ ) ) ]
132
+ pub ( crate ) const fn max_iov ( ) -> usize {
133
+ libc:: IOV_MAX as usize
134
+ }
135
+
136
+ #[ cfg( any(
137
+ target_os = "android" ,
138
+ target_os = "emscripten" ,
139
+ target_os = "linux" ,
140
+ target_os = "nto" ,
141
+ ) ) ]
142
+ pub ( crate ) const fn max_iov ( ) -> usize {
143
+ libc:: UIO_MAXIOV as usize
144
+ }
0 commit comments