@@ -160,11 +160,18 @@ struct CustomOutputStreamInner {
160160}
161161
162162impl CustomOutputStreamInner {
163- pub fn write ( & self , buf : & [ u8 ] ) -> isize {
164- ( self . callback ) ( self . foreign_data . data , buf. as_ptr ( ) , buf. len ( ) )
163+ pub fn raw_write ( & self , buf : & [ u8 ] ) -> io:: Result < usize > {
164+ let wrote = ( self . callback ) ( self . foreign_data . data , buf. as_ptr ( ) , buf. len ( ) ) ;
165+
166+ if wrote >= 0 {
167+ Ok ( wrote as _ )
168+ } else {
169+ Err ( io:: Error :: from_raw_os_error ( wrote. abs ( ) as _ ) )
170+ }
165171 }
166172}
167173
174+ #[ derive( Clone ) ]
168175pub struct CustomOutputStream {
169176 inner : std:: sync:: Arc < CustomOutputStreamInner > ,
170177}
@@ -183,14 +190,6 @@ impl CustomOutputStream {
183190 }
184191}
185192
186- impl Clone for CustomOutputStream {
187- fn clone ( & self ) -> Self {
188- Self {
189- inner : self . inner . clone ( ) ,
190- }
191- }
192- }
193-
194193#[ async_trait:: async_trait]
195194impl wasmtime_wasi:: p2:: Pollable for CustomOutputStream {
196195 async fn ready ( & mut self ) { }
@@ -199,15 +198,12 @@ impl wasmtime_wasi::p2::Pollable for CustomOutputStream {
199198#[ async_trait:: async_trait]
200199impl wasmtime_wasi:: p2:: OutputStream for CustomOutputStream {
201200 fn write ( & mut self , bytes : Bytes ) -> Result < ( ) , StreamError > {
202- let wrote = self . inner . write ( & bytes) ;
203-
204- if wrote < 0 {
205- return Err ( StreamError :: Trap (
206- io:: Error :: from_raw_os_error ( wrote. abs ( ) as _ ) . into ( ) ,
207- ) ) ;
208- }
201+ let wrote = self
202+ . inner
203+ . raw_write ( & bytes)
204+ . map_err ( |e| StreamError :: LastOperationFailed ( e. into ( ) ) ) ?;
209205
210- if wrote as usize != bytes. len ( ) {
206+ if wrote != bytes. len ( ) {
211207 return Err ( StreamError :: Trap ( anyhow:: anyhow!(
212208 "Partial writes in wasip2 implementation are not allowed"
213209 ) ) ) ;
@@ -229,12 +225,7 @@ impl AsyncWrite for CustomOutputStream {
229225 _cx : & mut Context < ' _ > ,
230226 buf : & [ u8 ] ,
231227 ) -> Poll < io:: Result < usize > > {
232- let wrote = self . inner . write ( buf) ;
233- Poll :: Ready ( if wrote >= 0 {
234- Ok ( wrote as _ )
235- } else {
236- Err ( io:: Error :: from_raw_os_error ( wrote. abs ( ) as _ ) )
237- } )
228+ Poll :: Ready ( self . inner . raw_write ( buf) )
238229 }
239230 fn poll_flush ( self : Pin < & mut Self > , _cx : & mut Context < ' _ > ) -> Poll < io:: Result < ( ) > > {
240231 Poll :: Ready ( Ok ( ( ) ) )
0 commit comments