3
3
use crate :: io:: { self , IoSlice , IoSliceMut , SeekFrom } ;
4
4
use crate :: mem;
5
5
use crate :: net:: Shutdown ;
6
- use wasi:: wasi_unstable as wasi;
6
+ use super :: err2io;
7
+ use :: wasi:: wasi_unstable as wasi;
7
8
8
9
#[ derive( Debug ) ]
9
10
pub struct WasiFd {
10
11
fd : wasi:: Fd ,
11
12
}
12
13
13
- fn iovec ( a : & mut [ IoSliceMut < ' _ > ] ) -> & [ wasi:: IoVec ] {
14
+ fn iovec < ' a > ( a : & ' a mut [ IoSliceMut < ' _ > ] ) -> & ' a [ wasi:: IoVec ] {
14
15
assert_eq ! (
15
16
mem:: size_of:: <IoSliceMut <' _>>( ) ,
16
17
mem:: size_of:: <wasi:: IoVec >( )
@@ -23,7 +24,7 @@ fn iovec(a: &mut [IoSliceMut<'_>]) -> &[wasi::IoVec] {
23
24
unsafe { mem:: transmute ( a) }
24
25
}
25
26
26
- fn ciovec ( a : & [ IoSlice < ' _ > ] ) -> & [ wasi:: CIoVec ] {
27
+ fn ciovec < ' a > ( a : & ' a [ IoSlice < ' _ > ] ) -> & ' a [ wasi:: CIoVec ] {
27
28
assert_eq ! (
28
29
mem:: size_of:: <IoSlice <' _>>( ) ,
29
30
mem:: size_of:: <wasi:: CIoVec >( )
@@ -52,23 +53,23 @@ impl WasiFd {
52
53
}
53
54
54
55
pub fn datasync ( & self ) -> io:: Result < ( ) > {
55
- wasi:: fd_datasync ( self . fd ) . map_err ( From :: from )
56
+ wasi:: fd_datasync ( self . fd ) . map_err ( err2io )
56
57
}
57
58
58
59
pub fn pread ( & self , bufs : & mut [ IoSliceMut < ' _ > ] , offset : u64 ) -> io:: Result < usize > {
59
- wasi:: fd_pread ( self . fd , iovec ( bufs) , offset) . map_err ( From :: from )
60
+ wasi:: fd_pread ( self . fd , iovec ( bufs) , offset) . map_err ( err2io )
60
61
}
61
62
62
63
pub fn pwrite ( & self , bufs : & [ IoSlice < ' _ > ] , offset : u64 ) -> io:: Result < usize > {
63
- wasi:: fd_pwrite ( self . fd , ciovec ( bufs) , offset) . map_err ( From :: from )
64
+ wasi:: fd_pwrite ( self . fd , ciovec ( bufs) , offset) . map_err ( err2io )
64
65
}
65
66
66
67
pub fn read ( & self , bufs : & mut [ IoSliceMut < ' _ > ] ) -> io:: Result < usize > {
67
- wasi:: fd_read ( self . fd , iovec ( bufs) ) . map_err ( From :: from )
68
+ wasi:: fd_read ( self . fd , iovec ( bufs) ) . map_err ( err2io )
68
69
}
69
70
70
71
pub fn write ( & self , bufs : & [ IoSlice < ' _ > ] ) -> io:: Result < usize > {
71
- wasi:: fd_write ( self . fd , ciovec ( bufs) ) . map_err ( From :: from )
72
+ wasi:: fd_write ( self . fd , ciovec ( bufs) ) . map_err ( err2io )
72
73
}
73
74
74
75
pub fn seek ( & self , pos : SeekFrom ) -> io:: Result < u64 > {
@@ -77,37 +78,37 @@ impl WasiFd {
77
78
SeekFrom :: End ( pos) => ( wasi:: WHENCE_END , pos) ,
78
79
SeekFrom :: Current ( pos) => ( wasi:: WHENCE_CUR , pos) ,
79
80
} ;
80
- wasi:: fd_seek ( self . fd , offset, whence) . map_err ( From :: from )
81
+ wasi:: fd_seek ( self . fd , offset, whence) . map_err ( err2io )
81
82
}
82
83
83
84
pub fn tell ( & self ) -> io:: Result < u64 > {
84
- wasi:: fd_tell ( self . fd ) . map_err ( From :: from )
85
+ wasi:: fd_tell ( self . fd ) . map_err ( err2io )
85
86
}
86
87
87
88
// FIXME: __wasi_fd_fdstat_get
88
89
89
90
pub fn set_flags ( & self , flags : wasi:: FdFlags ) -> io:: Result < ( ) > {
90
- wasi:: fd_fdstat_set_flags ( self . fd , flags) . map_err ( From :: from )
91
+ wasi:: fd_fdstat_set_flags ( self . fd , flags) . map_err ( err2io )
91
92
}
92
93
93
94
pub fn set_rights ( & self , base : wasi:: Rights , inheriting : wasi:: Rights ) -> io:: Result < ( ) > {
94
- wasi:: fd_fdstat_set_rights ( self . fd , base, inheriting) . map_err ( From :: from )
95
+ wasi:: fd_fdstat_set_rights ( self . fd , base, inheriting) . map_err ( err2io )
95
96
}
96
97
97
98
pub fn sync ( & self ) -> io:: Result < ( ) > {
98
- wasi:: fd_sync ( self . fd ) . map_err ( From :: from )
99
+ wasi:: fd_sync ( self . fd ) . map_err ( err2io )
99
100
}
100
101
101
102
pub fn advise ( & self , offset : u64 , len : u64 , advice : wasi:: Advice ) -> io:: Result < ( ) > {
102
- wasi:: fd_advise ( self . fd , offset, len, advice) . map_err ( From :: from )
103
+ wasi:: fd_advise ( self . fd , offset, len, advice) . map_err ( err2io )
103
104
}
104
105
105
106
pub fn allocate ( & self , offset : u64 , len : u64 ) -> io:: Result < ( ) > {
106
- wasi:: fd_allocate ( self . fd , offset, len) . map_err ( From :: from )
107
+ wasi:: fd_allocate ( self . fd , offset, len) . map_err ( err2io )
107
108
}
108
109
109
110
pub fn create_directory ( & self , path : & [ u8 ] ) -> io:: Result < ( ) > {
110
- wasi:: path_create_directory ( self . fd , path) . map_err ( From :: from )
111
+ wasi:: path_create_directory ( self . fd , path) . map_err ( err2io )
111
112
}
112
113
113
114
pub fn link (
@@ -118,7 +119,7 @@ impl WasiFd {
118
119
new_path : & [ u8 ] ,
119
120
) -> io:: Result < ( ) > {
120
121
wasi:: path_link ( self . fd , old_flags, old_path, new_fd. fd , new_path)
121
- . map_err ( From :: from )
122
+ . map_err ( err2io )
122
123
}
123
124
124
125
pub fn open (
@@ -130,33 +131,32 @@ impl WasiFd {
130
131
fs_rights_inheriting : wasi:: Rights ,
131
132
fs_flags : wasi:: FdFlags ,
132
133
) -> io:: Result < WasiFd > {
133
- let fd = wasi_path_open (
134
+ wasi :: path_open (
134
135
self . fd ,
135
136
dirflags,
136
137
path,
137
138
oflags,
138
139
fs_rights_base,
139
140
fs_rights_inheriting,
140
141
fs_flags,
141
- ) ?;
142
- Ok ( WasiFd :: from_raw ( fd) )
142
+ ) . map ( |fd| unsafe { WasiFd :: from_raw ( fd) } ) . map_err ( err2io)
143
143
}
144
144
145
145
pub fn readdir ( & self , buf : & mut [ u8 ] , cookie : wasi:: DirCookie ) -> io:: Result < usize > {
146
- wasi:: fd_readdir ( self . fd , buf, cookie) . map_err ( From :: from )
146
+ wasi:: fd_readdir ( self . fd , buf, cookie) . map_err ( err2io )
147
147
}
148
148
149
149
pub fn readlink ( & self , path : & [ u8 ] , buf : & mut [ u8 ] ) -> io:: Result < usize > {
150
- wasi:: path_readlink ( self . fd , path, buf) . map_err ( From :: from )
150
+ wasi:: path_readlink ( self . fd , path, buf) . map_err ( err2io )
151
151
}
152
152
153
153
pub fn rename ( & self , old_path : & [ u8 ] , new_fd : & WasiFd , new_path : & [ u8 ] ) -> io:: Result < ( ) > {
154
154
wasi:: path_rename ( self . fd , old_path, new_fd. fd , new_path)
155
- . map_err ( From :: from )
155
+ . map_err ( err2io )
156
156
}
157
157
158
- pub fn filestat_get ( & self ) -> io:: Result < wasi:: Filestat > {
159
- wasi:: fd_filestat_get ( self . fd , buf ) . map_err ( From :: from )
158
+ pub fn filestat_get ( & self ) -> io:: Result < wasi:: FileStat > {
159
+ wasi:: fd_filestat_get ( self . fd ) . map_err ( err2io )
160
160
}
161
161
162
162
pub fn filestat_set_times (
@@ -166,19 +166,19 @@ impl WasiFd {
166
166
fstflags : wasi:: FstFlags ,
167
167
) -> io:: Result < ( ) > {
168
168
wasi:: fd_filestat_set_times ( self . fd , atim, mtim, fstflags)
169
- . map_err ( From :: from )
169
+ . map_err ( err2io )
170
170
}
171
171
172
172
pub fn filestat_set_size ( & self , size : u64 ) -> io:: Result < ( ) > {
173
- wasi:: fd_filestat_set_size ( self . fd , size) . map_err ( From :: from )
173
+ wasi:: fd_filestat_set_size ( self . fd , size) . map_err ( err2io )
174
174
}
175
175
176
176
pub fn path_filestat_get (
177
177
& self ,
178
178
flags : wasi:: LookupFlags ,
179
179
path : & [ u8 ] ,
180
180
) -> io:: Result < wasi:: FileStat > {
181
- wasi:: path_filestat_get ( self . fd , flags, path) . map_err ( From :: from )
181
+ wasi:: path_filestat_get ( self . fd , flags, path) . map_err ( err2io )
182
182
}
183
183
184
184
pub fn path_filestat_set_times (
@@ -196,40 +196,40 @@ impl WasiFd {
196
196
atim,
197
197
mtim,
198
198
fstflags,
199
- ) . map_err ( From :: from )
199
+ ) . map_err ( err2io )
200
200
}
201
201
202
202
pub fn symlink ( & self , old_path : & [ u8 ] , new_path : & [ u8 ] ) -> io:: Result < ( ) > {
203
- wasi:: path_symlink ( old_path, self . fd , new_path) . map_err ( From :: from )
203
+ wasi:: path_symlink ( old_path, self . fd , new_path) . map_err ( err2io )
204
204
}
205
205
206
206
pub fn unlink_file ( & self , path : & [ u8 ] ) -> io:: Result < ( ) > {
207
- wasi:: path_unlink_file ( self . fd , path) . map_err ( From :: from )
207
+ wasi:: path_unlink_file ( self . fd , path) . map_err ( err2io )
208
208
}
209
209
210
210
pub fn remove_directory ( & self , path : & [ u8 ] ) -> io:: Result < ( ) > {
211
- wasi:: path_remove_directory ( self . fd , path) . map_err ( From :: from )
211
+ wasi:: path_remove_directory ( self . fd , path) . map_err ( err2io )
212
212
}
213
213
214
214
pub fn sock_recv (
215
215
& self ,
216
216
ri_data : & mut [ IoSliceMut < ' _ > ] ,
217
217
ri_flags : wasi:: RiFlags ,
218
218
) -> io:: Result < ( usize , wasi:: RoFlags ) > {
219
- wasi:: sock_recv ( self . fd , iovec ( ri_data) , ri_flags) . map_err ( From :: from )
219
+ wasi:: sock_recv ( self . fd , iovec ( ri_data) , ri_flags) . map_err ( err2io )
220
220
}
221
221
222
222
pub fn sock_send ( & self , si_data : & [ IoSlice < ' _ > ] , si_flags : wasi:: SiFlags ) -> io:: Result < usize > {
223
- wasi:: sock_send ( self . fd , ciovec ( si_data) , si_flags) . map_err ( From :: from )
223
+ wasi:: sock_send ( self . fd , ciovec ( si_data) , si_flags) . map_err ( err2io )
224
224
}
225
225
226
226
pub fn sock_shutdown ( & self , how : Shutdown ) -> io:: Result < ( ) > {
227
227
let how = match how {
228
- Shutdown :: Read => WASI :: SHUT_RD ,
229
- Shutdown :: Write => WASI :: SHUT_WR ,
230
- Shutdown :: Both => WASI :: SHUT_WR | WASI :: SHUT_RD ,
228
+ Shutdown :: Read => wasi :: SHUT_RD ,
229
+ Shutdown :: Write => wasi :: SHUT_WR ,
230
+ Shutdown :: Both => wasi :: SHUT_WR | wasi :: SHUT_RD ,
231
231
} ;
232
- wasi:: sock_shutdown ( self . fd , how) . map_err ( From :: from )
232
+ wasi:: sock_shutdown ( self . fd , how) . map_err ( err2io )
233
233
}
234
234
}
235
235
0 commit comments