@@ -46,32 +46,6 @@ impl AsyncInputStream {
4646 async fn ready ( & mut self ) {
4747 poll_fn ( |cx| self . poll_ready ( cx) ) . await
4848 }
49- /// Asynchronously read from the input stream.
50- pub async fn read ( & mut self , buf : & mut [ u8 ] ) -> std:: io:: Result < usize > {
51- let read = loop {
52- self . ready ( ) . await ;
53- // Ideally, the ABI would be able to read directly into buf.
54- // However, with the default generated bindings, it returns a
55- // newly allocated vec, which we need to copy into buf.
56- match self . stream . read ( buf. len ( ) as u64 ) {
57- // A read of 0 bytes from WASI's `read` doesn't mean
58- // end-of-stream as it does in Rust. However, `self.ready()`
59- // cannot guarantee that at least one byte is ready for
60- // reading, so in this case we try again.
61- Ok ( r) if r. is_empty ( ) => continue ,
62- Ok ( r) => break r,
63- // 0 bytes from Rust's `read` means end-of-stream.
64- Err ( StreamError :: Closed ) => return Ok ( 0 ) ,
65- Err ( StreamError :: LastOperationFailed ( err) ) => {
66- return Err ( std:: io:: Error :: other ( err. to_debug_string ( ) ) ) ;
67- }
68- }
69- } ;
70- let len = read. len ( ) ;
71- buf[ 0 ..len] . copy_from_slice ( & read) ;
72- Ok ( len)
73- }
74-
7549 /// Move the entire contents of an input stream directly into an output
7650 /// stream, until the input stream has closed. This operation is optimized
7751 /// to avoid copying stream contents into and out of memory.
@@ -123,7 +97,28 @@ impl AsyncInputStream {
12397
12498impl AsyncRead for AsyncInputStream {
12599 async fn read ( & mut self , buf : & mut [ u8 ] ) -> std:: io:: Result < usize > {
126- Self :: read ( self , buf) . await
100+ let read = loop {
101+ self . ready ( ) . await ;
102+ // Ideally, the ABI would be able to read directly into buf.
103+ // However, with the default generated bindings, it returns a
104+ // newly allocated vec, which we need to copy into buf.
105+ match self . stream . read ( buf. len ( ) as u64 ) {
106+ // A read of 0 bytes from WASI's `read` doesn't mean
107+ // end-of-stream as it does in Rust. However, `self.ready()`
108+ // cannot guarantee that at least one byte is ready for
109+ // reading, so in this case we try again.
110+ Ok ( r) if r. is_empty ( ) => continue ,
111+ Ok ( r) => break r,
112+ // 0 bytes from Rust's `read` means end-of-stream.
113+ Err ( StreamError :: Closed ) => return Ok ( 0 ) ,
114+ Err ( StreamError :: LastOperationFailed ( err) ) => {
115+ return Err ( std:: io:: Error :: other ( err. to_debug_string ( ) ) ) ;
116+ }
117+ }
118+ } ;
119+ let len = read. len ( ) ;
120+ buf[ 0 ..len] . copy_from_slice ( & read) ;
121+ Ok ( len)
127122 }
128123
129124 #[ inline]
@@ -241,14 +236,11 @@ impl AsyncOutputStream {
241236 // Wait on readiness
242237 subscription. wait_for ( ) . await ;
243238 }
244- /// Asynchronously write to the output stream.
245- ///
246- /// Awaits for write readiness, and then performs at most one write to the
247- /// output stream. Returns how much of the argument `buf` was written, or
248- /// a `std::io::Error` indicating either an error returned by the stream write
249- /// using the debug string provided by the WASI error, or else that the,
250- /// indicated by `std::io::ErrorKind::ConnectionReset`.
251- pub async fn write ( & mut self , buf : & [ u8 ] ) -> std:: io:: Result < usize > {
239+ }
240+
241+ impl AsyncWrite for AsyncOutputStream {
242+ // Required methods
243+ async fn write ( & mut self , buf : & [ u8 ] ) -> std:: io:: Result < usize > {
252244 // Loops at most twice.
253245 loop {
254246 match self . stream . check_write ( ) {
@@ -278,28 +270,7 @@ impl AsyncOutputStream {
278270 }
279271 }
280272 }
281-
282- /// Asynchronously write to the output stream.
283- pub async fn write_all ( & mut self , buf : & [ u8 ] ) -> std:: io:: Result < ( ) > {
284- let mut to_write = & buf[ 0 ..] ;
285- loop {
286- let bytes_written = self . write ( to_write) . await ?;
287- to_write = & to_write[ bytes_written..] ;
288- if to_write. is_empty ( ) {
289- return Ok ( ( ) ) ;
290- }
291- }
292- }
293-
294- /// Asyncronously flush the output stream. Initiates a flush, and then
295- /// awaits until the flush is complete and the output stream is ready for
296- /// writing again.
297- ///
298- /// Fails with a `std::io::Error` indicating either an error returned by
299- /// the stream flush, using the debug string provided by the WASI error,
300- /// or else that the stream is closed, indicated by
301- /// `std::io::ErrorKind::ConnectionReset`.
302- pub async fn flush ( & mut self ) -> std:: io:: Result < ( ) > {
273+ async fn flush ( & mut self ) -> std:: io:: Result < ( ) > {
303274 match self . stream . flush ( ) {
304275 Ok ( ( ) ) => {
305276 self . ready ( ) . await ;
@@ -313,16 +284,6 @@ impl AsyncOutputStream {
313284 }
314285 }
315286 }
316- }
317-
318- impl AsyncWrite for AsyncOutputStream {
319- // Required methods
320- async fn write ( & mut self , buf : & [ u8 ] ) -> std:: io:: Result < usize > {
321- Self :: write ( self , buf) . await
322- }
323- async fn flush ( & mut self ) -> std:: io:: Result < ( ) > {
324- Self :: flush ( self ) . await
325- }
326287
327288 #[ inline]
328289 fn as_async_output_stream ( & mut self ) -> Option < & mut AsyncOutputStream > {
0 commit comments