Skip to content

Commit 1cb47c1

Browse files
committed
add docs to the AsyncInputStream and AsyncOutputStream types
1 parent 95bbcd6 commit 1cb47c1

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/io/streams.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use std::cell::OnceCell;
33
use std::io::Result;
44
use wasi::io::streams::{InputStream, OutputStream, StreamError};
55

6+
/// A wrapper for WASI's `InputStream` resource that provides implementations of `AsyncRead` and
7+
/// `AsyncPollable`.
68
#[derive(Debug)]
79
pub struct AsyncInputStream {
810
// Lazily initialized pollable, used for lifetime of stream to check readiness.
@@ -12,12 +14,14 @@ pub struct AsyncInputStream {
1214
}
1315

1416
impl AsyncInputStream {
17+
/// Construct an `AsyncInputStream` from a WASI `InputStream` resource.
1518
pub fn new(stream: InputStream) -> Self {
1619
Self {
1720
subscription: OnceCell::new(),
1821
stream,
1922
}
2023
}
24+
/// Await for read readiness.
2125
async fn ready(&self) {
2226
// Lazily initialize the AsyncPollable
2327
let subscription = self
@@ -26,7 +30,8 @@ impl AsyncInputStream {
2630
// Wait on readiness
2731
subscription.wait_for().await;
2832
}
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`.
3035
pub async fn read(&self, buf: &mut [u8]) -> Result<usize> {
3136
let read = loop {
3237
self.ready().await;
@@ -64,6 +69,8 @@ impl AsyncRead for AsyncInputStream {
6469
}
6570
}
6671

72+
/// A wrapper for WASI's `output-stream` resource that provides implementations of `AsyncWrite` and
73+
/// `AsyncPollable`.
6774
#[derive(Debug)]
6875
pub struct AsyncOutputStream {
6976
// Lazily initialized pollable, used for lifetime of stream to check readiness.
@@ -73,12 +80,14 @@ pub struct AsyncOutputStream {
7380
}
7481

7582
impl AsyncOutputStream {
83+
/// Construct an `AsyncOutputStream` from a WASI `OutputStream` resource.
7684
pub fn new(stream: OutputStream) -> Self {
7785
Self {
7886
subscription: OnceCell::new(),
7987
stream,
8088
}
8189
}
90+
/// Await write readiness.
8291
async fn ready(&self) {
8392
// Lazily initialize the AsyncPollable
8493
let subscription = self
@@ -87,7 +96,14 @@ impl AsyncOutputStream {
8796
// Wait on readiness
8897
subscription.wait_for().await;
8998
}
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`.
91107
pub async fn write(&self, buf: &[u8]) -> Result<usize> {
92108
// Loops at most twice.
93109
loop {
@@ -118,7 +134,17 @@ impl AsyncOutputStream {
118134
}
119135
}
120136
}
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`.
122148
pub async fn flush(&self) -> Result<()> {
123149
match self.stream.flush() {
124150
Ok(()) => {

0 commit comments

Comments
 (0)