33use crate :: io:: { self , IoSlice , IoSliceMut , SeekFrom } ;
44use crate :: mem;
55use crate :: net:: Shutdown ;
6- use wasi:: wasi_unstable as wasi;
6+ use super :: err2io;
7+ use :: wasi:: wasi_unstable as wasi;
78
89#[ derive( Debug ) ]
910pub struct WasiFd {
1011 fd : wasi:: Fd ,
1112}
1213
13- fn iovec ( a : & mut [ IoSliceMut < ' _ > ] ) -> & [ wasi:: IoVec ] {
14+ fn iovec < ' a > ( a : & ' a mut [ IoSliceMut < ' _ > ] ) -> & ' a [ wasi:: IoVec ] {
1415 assert_eq ! (
1516 mem:: size_of:: <IoSliceMut <' _>>( ) ,
1617 mem:: size_of:: <wasi:: IoVec >( )
@@ -23,7 +24,7 @@ fn iovec(a: &mut [IoSliceMut<'_>]) -> &[wasi::IoVec] {
2324 unsafe { mem:: transmute ( a) }
2425}
2526
26- fn ciovec ( a : & [ IoSlice < ' _ > ] ) -> & [ wasi:: CIoVec ] {
27+ fn ciovec < ' a > ( a : & ' a [ IoSlice < ' _ > ] ) -> & ' a [ wasi:: CIoVec ] {
2728 assert_eq ! (
2829 mem:: size_of:: <IoSlice <' _>>( ) ,
2930 mem:: size_of:: <wasi:: CIoVec >( )
@@ -52,23 +53,23 @@ impl WasiFd {
5253 }
5354
5455 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 )
5657 }
5758
5859 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 )
6061 }
6162
6263 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 )
6465 }
6566
6667 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 )
6869 }
6970
7071 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 )
7273 }
7374
7475 pub fn seek ( & self , pos : SeekFrom ) -> io:: Result < u64 > {
@@ -77,37 +78,37 @@ impl WasiFd {
7778 SeekFrom :: End ( pos) => ( wasi:: WHENCE_END , pos) ,
7879 SeekFrom :: Current ( pos) => ( wasi:: WHENCE_CUR , pos) ,
7980 } ;
80- wasi:: fd_seek ( self . fd , offset, whence) . map_err ( From :: from )
81+ wasi:: fd_seek ( self . fd , offset, whence) . map_err ( err2io )
8182 }
8283
8384 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 )
8586 }
8687
8788 // FIXME: __wasi_fd_fdstat_get
8889
8990 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 )
9192 }
9293
9394 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 )
9596 }
9697
9798 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 )
99100 }
100101
101102 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 )
103104 }
104105
105106 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 )
107108 }
108109
109110 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 )
111112 }
112113
113114 pub fn link (
@@ -118,7 +119,7 @@ impl WasiFd {
118119 new_path : & [ u8 ] ,
119120 ) -> io:: Result < ( ) > {
120121 wasi:: path_link ( self . fd , old_flags, old_path, new_fd. fd , new_path)
121- . map_err ( From :: from )
122+ . map_err ( err2io )
122123 }
123124
124125 pub fn open (
@@ -130,33 +131,32 @@ impl WasiFd {
130131 fs_rights_inheriting : wasi:: Rights ,
131132 fs_flags : wasi:: FdFlags ,
132133 ) -> io:: Result < WasiFd > {
133- let fd = wasi_path_open (
134+ wasi :: path_open (
134135 self . fd ,
135136 dirflags,
136137 path,
137138 oflags,
138139 fs_rights_base,
139140 fs_rights_inheriting,
140141 fs_flags,
141- ) ?;
142- Ok ( WasiFd :: from_raw ( fd) )
142+ ) . map ( |fd| unsafe { WasiFd :: from_raw ( fd) } ) . map_err ( err2io)
143143 }
144144
145145 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 )
147147 }
148148
149149 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 )
151151 }
152152
153153 pub fn rename ( & self , old_path : & [ u8 ] , new_fd : & WasiFd , new_path : & [ u8 ] ) -> io:: Result < ( ) > {
154154 wasi:: path_rename ( self . fd , old_path, new_fd. fd , new_path)
155- . map_err ( From :: from )
155+ . map_err ( err2io )
156156 }
157157
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 )
160160 }
161161
162162 pub fn filestat_set_times (
@@ -166,19 +166,19 @@ impl WasiFd {
166166 fstflags : wasi:: FstFlags ,
167167 ) -> io:: Result < ( ) > {
168168 wasi:: fd_filestat_set_times ( self . fd , atim, mtim, fstflags)
169- . map_err ( From :: from )
169+ . map_err ( err2io )
170170 }
171171
172172 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 )
174174 }
175175
176176 pub fn path_filestat_get (
177177 & self ,
178178 flags : wasi:: LookupFlags ,
179179 path : & [ u8 ] ,
180180 ) -> 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 )
182182 }
183183
184184 pub fn path_filestat_set_times (
@@ -196,40 +196,40 @@ impl WasiFd {
196196 atim,
197197 mtim,
198198 fstflags,
199- ) . map_err ( From :: from )
199+ ) . map_err ( err2io )
200200 }
201201
202202 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 )
204204 }
205205
206206 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 )
208208 }
209209
210210 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 )
212212 }
213213
214214 pub fn sock_recv (
215215 & self ,
216216 ri_data : & mut [ IoSliceMut < ' _ > ] ,
217217 ri_flags : wasi:: RiFlags ,
218218 ) -> 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 )
220220 }
221221
222222 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 )
224224 }
225225
226226 pub fn sock_shutdown ( & self , how : Shutdown ) -> io:: Result < ( ) > {
227227 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 ,
231231 } ;
232- wasi:: sock_shutdown ( self . fd , how) . map_err ( From :: from )
232+ wasi:: sock_shutdown ( self . fd , how) . map_err ( err2io )
233233 }
234234}
235235
0 commit comments