@@ -24,90 +24,63 @@ use io_lifetimes::{AsHandle, BorrowedHandle};
2424#[ cfg( windows) ]
2525use io_extras:: os:: windows:: { AsRawHandleOrSocket , RawHandleOrSocket } ;
2626
27- pub struct BorrowedFile < ' a > ( RwLockReadGuard < ' a , cap_std:: fs:: File > ) ;
28-
29- #[ cfg( unix) ]
30- impl AsFd for BorrowedFile < ' _ > {
31- fn as_fd ( & self ) -> BorrowedFd < ' _ > {
32- self . 0 . as_fd ( )
33- }
34- }
35-
36- #[ cfg( windows) ]
37- impl AsHandle for BorrowedFile < ' _ > {
38- fn as_handle ( & self ) -> BorrowedHandle < ' _ > {
39- self . 0 . as_handle ( )
40- }
41- }
42-
43- #[ cfg( windows) ]
44- impl AsRawHandleOrSocket for BorrowedFile < ' _ > {
45- #[ inline]
46- fn as_raw_handle_or_socket ( & self ) -> RawHandleOrSocket {
47- self . 0 . as_raw_handle_or_socket ( )
48- }
49- }
50-
51- pub struct File ( RwLock < cap_std:: fs:: File > ) ;
27+ pub struct File ( cap_std:: fs:: File ) ;
5228
5329impl File {
5430 pub fn from_cap_std ( file : cap_std:: fs:: File ) -> Self {
55- File ( RwLock :: new ( file) )
56- }
57-
58- pub fn borrow ( & self ) -> BorrowedFile {
59- BorrowedFile ( self . 0 . read ( ) . unwrap ( ) )
31+ File ( file)
6032 }
6133}
6234
6335#[ async_trait:: async_trait]
6436impl WasiFile for File {
65- fn as_any ( & self ) -> & dyn Any {
37+ fn as_any ( self : Arc < Self > ) -> Arc < dyn Any > {
6638 self
6739 }
6840
6941 #[ cfg( unix) ]
70- fn pollable ( & self ) -> Option < Arc < dyn AsFd + ' _ > > {
71- Some ( Arc :: new ( self . borrow ( ) ) )
42+ fn pollable ( self : Arc < Self > ) -> Option < Arc < dyn AsFd > > {
43+ Some ( Arc :: new ( self . 0 . as_fd ( ) ) )
7244 }
7345
7446 #[ cfg( windows) ]
75- fn pollable ( & self ) -> Option < Arc < dyn AsRawHandleOrSocket + ' _ > > {
76- Some ( Arc :: new ( BorrowedFile ( self . 0 . read ( ) . unwrap ( ) ) ) )
47+ fn pollable ( self : Arc < Self > ) -> Option < Arc < dyn AsRawHandleOrSocket > > {
48+ Some ( Arc :: new ( BorrowedFile ( self . 0 ) ) )
7749 }
7850
79- async fn datasync ( & self ) -> Result < ( ) , Error > {
80- self . 0 . read ( ) . unwrap ( ) . sync_data ( ) ?;
51+ async fn datasync ( self : Arc < Self > ) -> Result < ( ) , Error > {
52+ self . 0 . sync_data ( ) ?;
8153 Ok ( ( ) )
8254 }
83- async fn sync ( & self ) -> Result < ( ) , Error > {
84- self . 0 . read ( ) . unwrap ( ) . sync_all ( ) ?;
55+ async fn sync ( self : Arc < Self > ) -> Result < ( ) , Error > {
56+ self . 0 . sync_all ( ) ?;
8557 Ok ( ( ) )
8658 }
87- async fn get_filetype ( & self ) -> Result < FileType , Error > {
88- let meta = self . 0 . read ( ) . unwrap ( ) . metadata ( ) ?;
59+ async fn get_filetype ( self : Arc < Self > ) -> Result < FileType , Error > {
60+ let meta = self . 0 . metadata ( ) ?;
8961 Ok ( filetype_from ( & meta. file_type ( ) ) )
9062 }
91- async fn get_fdflags ( & self ) -> Result < FdFlags , Error > {
92- let file = self . 0 . read ( ) . unwrap ( ) ;
93- let fdflags = get_fd_flags ( & * file) ?;
63+ async fn get_fdflags ( self : Arc < Self > ) -> Result < FdFlags , Error > {
64+ let fdflags = get_fd_flags ( self . 0 ) ?;
9465 Ok ( fdflags)
9566 }
96- async fn set_fdflags ( & self , fdflags : FdFlags ) -> Result < ( ) , Error > {
67+ async fn set_fdflags ( mut self : Arc < Self > , fdflags : FdFlags ) -> Result < ( ) , Error > {
9768 if fdflags. intersects (
9869 wasi_common:: file:: FdFlags :: DSYNC
9970 | wasi_common:: file:: FdFlags :: SYNC
10071 | wasi_common:: file:: FdFlags :: RSYNC ,
10172 ) {
10273 return Err ( Error :: invalid_argument ( ) . context ( "cannot set DSYNC, SYNC, or RSYNC flag" ) ) ;
10374 }
104- let mut file = self . 0 . write ( ) . unwrap ( ) ;
105- let set_fd_flags = ( * file) . new_set_fd_flags ( to_sysif_fdflags ( fdflags) ) ?;
106- ( * file) . set_fd_flags ( set_fd_flags) ?;
75+ let set_fd_flags = self . 0 . new_set_fd_flags ( to_sysif_fdflags ( fdflags) ) ?;
76+ Arc :: get_mut ( & mut self )
77+ . unwrap ( )
78+ . 0
79+ . set_fd_flags ( set_fd_flags) ?;
10780 Ok ( ( ) )
10881 }
109- async fn get_filestat ( & self ) -> Result < Filestat , Error > {
110- let meta = self . 0 . read ( ) . unwrap ( ) . metadata ( ) ?;
82+ async fn get_filestat ( self : Arc < Self > ) -> Result < Filestat , Error > {
83+ let meta = self . 0 . metadata ( ) ?;
11184 Ok ( Filestat {
11285 device_id : meta. dev ( ) ,
11386 inode : meta. ino ( ) ,
@@ -119,68 +92,66 @@ impl WasiFile for File {
11992 ctim : meta. created ( ) . map ( |t| Some ( t. into_std ( ) ) ) . unwrap_or ( None ) ,
12093 } )
12194 }
122- async fn set_filestat_size ( & self , size : u64 ) -> Result < ( ) , Error > {
123- self . 0 . read ( ) . unwrap ( ) . set_len ( size) ?;
95+ async fn set_filestat_size ( self : Arc < Self > , size : u64 ) -> Result < ( ) , Error > {
96+ self . 0 . set_len ( size) ?;
12497 Ok ( ( ) )
12598 }
126- async fn advise ( & self , offset : u64 , len : u64 , advice : Advice ) -> Result < ( ) , Error > {
127- self . 0
128- . read ( )
129- . unwrap ( )
130- . advise ( offset, len, convert_advice ( advice) ) ?;
99+ async fn advise ( self : Arc < Self > , offset : u64 , len : u64 , advice : Advice ) -> Result < ( ) , Error > {
100+ self . 0 . advise ( offset, len, convert_advice ( advice) ) ?;
131101 Ok ( ( ) )
132102 }
133- async fn allocate ( & self , offset : u64 , len : u64 ) -> Result < ( ) , Error > {
134- self . 0 . read ( ) . unwrap ( ) . allocate ( offset, len) ?;
103+ async fn allocate ( self : Arc < Self > , offset : u64 , len : u64 ) -> Result < ( ) , Error > {
104+ self . 0 . allocate ( offset, len) ?;
135105 Ok ( ( ) )
136106 }
137107 async fn set_times (
138- & self ,
108+ self : Arc < Self > ,
139109 atime : Option < wasi_common:: SystemTimeSpec > ,
140110 mtime : Option < wasi_common:: SystemTimeSpec > ,
141111 ) -> Result < ( ) , Error > {
142112 self . 0
143- . read ( )
144- . unwrap ( )
145113 . set_times ( convert_systimespec ( atime) , convert_systimespec ( mtime) ) ?;
146114 Ok ( ( ) )
147115 }
148- async fn read_vectored < ' a > ( & self , bufs : & mut [ io:: IoSliceMut < ' a > ] ) -> Result < u64 , Error > {
149- let n = self . 0 . read ( ) . unwrap ( ) . read_vectored ( bufs) ?;
116+ async fn read_vectored < ' a > (
117+ self : Arc < Self > ,
118+ bufs : & mut [ io:: IoSliceMut < ' a > ] ,
119+ ) -> Result < u64 , Error > {
120+ let n = self . 0 . read_vectored ( bufs) ?;
150121 Ok ( n. try_into ( ) ?)
151122 }
152123 async fn read_vectored_at < ' a > (
153- & self ,
124+ self : Arc < Self > ,
154125 bufs : & mut [ io:: IoSliceMut < ' a > ] ,
155126 offset : u64 ,
156127 ) -> Result < u64 , Error > {
157- let n = self . 0 . read ( ) . unwrap ( ) . read_vectored_at ( bufs, offset) ?;
128+ let n = self . 0 . read_vectored_at ( bufs, offset) ?;
158129 Ok ( n. try_into ( ) ?)
159130 }
160- async fn write_vectored < ' a > ( & self , bufs : & [ io:: IoSlice < ' a > ] ) -> Result < u64 , Error > {
161- let n = self . 0 . read ( ) . unwrap ( ) . write_vectored ( bufs) ?;
131+ async fn write_vectored < ' a > ( self : Arc < Self > , bufs : & [ io:: IoSlice < ' a > ] ) -> Result < u64 , Error > {
132+ let n = self . 0 . write_vectored ( bufs) ?;
162133 Ok ( n. try_into ( ) ?)
163134 }
164135 async fn write_vectored_at < ' a > (
165- & self ,
136+ self : Arc < Self > ,
166137 bufs : & [ io:: IoSlice < ' a > ] ,
167138 offset : u64 ,
168139 ) -> Result < u64 , Error > {
169- let n = self . 0 . read ( ) . unwrap ( ) . write_vectored_at ( bufs, offset) ?;
140+ let n = self . 0 . write_vectored_at ( bufs, offset) ?;
170141 Ok ( n. try_into ( ) ?)
171142 }
172- async fn seek ( & self , pos : std:: io:: SeekFrom ) -> Result < u64 , Error > {
173- Ok ( self . 0 . read ( ) . unwrap ( ) . seek ( pos) ?)
143+ async fn seek ( self : Arc < Self > , pos : std:: io:: SeekFrom ) -> Result < u64 , Error > {
144+ Ok ( self . 0 . seek ( pos) ?)
174145 }
175- async fn peek ( & self , buf : & mut [ u8 ] ) -> Result < u64 , Error > {
176- let n = self . 0 . read ( ) . unwrap ( ) . peek ( buf) ?;
146+ async fn peek ( self : Arc < Self > , buf : & mut [ u8 ] ) -> Result < u64 , Error > {
147+ let n = self . 0 . peek ( buf) ?;
177148 Ok ( n. try_into ( ) ?)
178149 }
179- fn num_ready_bytes ( & self ) -> Result < u64 , Error > {
180- Ok ( self . 0 . read ( ) . unwrap ( ) . num_ready_bytes ( ) ?)
150+ fn num_ready_bytes ( self : Arc < Self > ) -> Result < u64 , Error > {
151+ Ok ( self . 0 . num_ready_bytes ( ) ?)
181152 }
182- fn isatty ( & self ) -> bool {
183- self . 0 . read ( ) . unwrap ( ) . is_terminal ( )
153+ fn isatty ( self : Arc < Self > ) -> bool {
154+ self . 0 . is_terminal ( )
184155 }
185156}
186157
0 commit comments