@@ -3,6 +3,8 @@ use std::cell::OnceCell;
3
3
use std:: io:: Result ;
4
4
use wasi:: io:: streams:: { InputStream , OutputStream , StreamError } ;
5
5
6
+ /// A wrapper for WASI's `InputStream` resource that provides implementations of `AsyncRead` and
7
+ /// `AsyncPollable`.
6
8
#[ derive( Debug ) ]
7
9
pub struct AsyncInputStream {
8
10
// Lazily initialized pollable, used for lifetime of stream to check readiness.
@@ -12,12 +14,14 @@ pub struct AsyncInputStream {
12
14
}
13
15
14
16
impl AsyncInputStream {
17
+ /// Construct an `AsyncInputStream` from a WASI `InputStream` resource.
15
18
pub fn new ( stream : InputStream ) -> Self {
16
19
Self {
17
20
subscription : OnceCell :: new ( ) ,
18
21
stream,
19
22
}
20
23
}
24
+ /// Await for read readiness.
21
25
async fn ready ( & self ) {
22
26
// Lazily initialize the AsyncPollable
23
27
let subscription = self
@@ -26,7 +30,8 @@ impl AsyncInputStream {
26
30
// Wait on readiness
27
31
subscription. wait_for ( ) . await ;
28
32
}
29
- /// Like [`AsyncRead::read`], but doesn't require a `&mut self`.
33
+ /// Asynchronously read from the input stream.
34
+ /// This method is the same as [`AsyncRead::read`], but doesn't require a `&mut self`.
30
35
pub async fn read ( & self , buf : & mut [ u8 ] ) -> Result < usize > {
31
36
let read = loop {
32
37
self . ready ( ) . await ;
@@ -64,6 +69,8 @@ impl AsyncRead for AsyncInputStream {
64
69
}
65
70
}
66
71
72
+ /// A wrapper for WASI's `output-stream` resource that provides implementations of `AsyncWrite` and
73
+ /// `AsyncPollable`.
67
74
#[ derive( Debug ) ]
68
75
pub struct AsyncOutputStream {
69
76
// Lazily initialized pollable, used for lifetime of stream to check readiness.
@@ -73,12 +80,14 @@ pub struct AsyncOutputStream {
73
80
}
74
81
75
82
impl AsyncOutputStream {
83
+ /// Construct an `AsyncOutputStream` from a WASI `OutputStream` resource.
76
84
pub fn new ( stream : OutputStream ) -> Self {
77
85
Self {
78
86
subscription : OnceCell :: new ( ) ,
79
87
stream,
80
88
}
81
89
}
90
+ /// Await write readiness.
82
91
async fn ready ( & self ) {
83
92
// Lazily initialize the AsyncPollable
84
93
let subscription = self
@@ -87,7 +96,14 @@ impl AsyncOutputStream {
87
96
// Wait on readiness
88
97
subscription. wait_for ( ) . await ;
89
98
}
90
- /// Like [`AsyncWrite::write`], but doesn't require a `&mut self`.
99
+ /// Asynchronously write to the output stream. This method is the same as
100
+ /// [`AsyncWrite::write`], but doesn't require a `&mut self`.
101
+ ///
102
+ /// Awaits for write readiness, and then performs at most one write to the
103
+ /// output stream. Returns how much of the argument `buf` was written, or
104
+ /// a `std::io::Error` indicating either an error returned by the stream write
105
+ /// using the debug string provided by the WASI error, or else that the,
106
+ /// indicated by `std::io::ErrorKind::ConnectionReset`.
91
107
pub async fn write ( & self , buf : & [ u8 ] ) -> Result < usize > {
92
108
// Loops at most twice.
93
109
loop {
@@ -118,7 +134,17 @@ impl AsyncOutputStream {
118
134
}
119
135
}
120
136
}
121
- /// Like [`AsyncWrite::flush`], but doesn't require a `&mut self`.
137
+ /// Asyncronously flush the output stream. Initiates a flush, and then
138
+ /// awaits until the flush is complete and the output stream is ready for
139
+ /// writing again.
140
+ ///
141
+ /// This method is the same as [`AsyncWrite::flush`], but doesn't require
142
+ /// a `&mut self`.
143
+ ///
144
+ /// Fails with a `std::io::Error` indicating either an error returned by
145
+ /// the stream flush, using the debug string provided by the WASI error,
146
+ /// or else that the stream is closed, indicated by
147
+ /// `std::io::ErrorKind::ConnectionReset`.
122
148
pub async fn flush ( & self ) -> Result < ( ) > {
123
149
match self . stream . flush ( ) {
124
150
Ok ( ( ) ) => {
0 commit comments