Skip to content

Commit 94a0844

Browse files
authored
Merge pull request #51 from sunfishcode/sunfishcode/stdio-impls
Implement `AsyncRead`/`AsyncWrite` for `Stdin`/`Stdout`/`Stderr`.
2 parents c276fdb + fcda334 commit 94a0844

File tree

1 file changed

+54
-29
lines changed

1 file changed

+54
-29
lines changed

src/io/stdio.rs

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{AsyncInputStream, AsyncOutputStream};
1+
use super::{AsyncInputStream, AsyncOutputStream, AsyncRead, AsyncWrite, Result};
22
use std::cell::LazyCell;
33
use wasi::cli::terminal_input::TerminalInput;
44
use wasi::cli::terminal_output::TerminalOutput;
@@ -19,25 +19,30 @@ pub fn stdin() -> Stdin {
1919
}
2020
}
2121

22-
impl std::ops::Deref for Stdin {
23-
type Target = AsyncInputStream;
24-
fn deref(&self) -> &AsyncInputStream {
25-
&self.stream
26-
}
27-
}
28-
impl std::ops::DerefMut for Stdin {
29-
fn deref_mut(&mut self) -> &mut AsyncInputStream {
30-
&mut self.stream
31-
}
32-
}
33-
3422
impl Stdin {
3523
/// Check if stdin is a terminal.
3624
pub fn is_terminal(&self) -> bool {
3725
LazyCell::force(&self.terminput).is_some()
3826
}
3927
}
4028

29+
impl AsyncRead for Stdin {
30+
#[inline]
31+
async fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
32+
self.stream.read(buf).await
33+
}
34+
35+
#[inline]
36+
async fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
37+
self.stream.read_to_end(buf).await
38+
}
39+
40+
#[inline]
41+
fn as_async_input_stream(&self) -> Option<&AsyncInputStream> {
42+
Some(&self.stream)
43+
}
44+
}
45+
4146
/// Use the program's stdout as an `AsyncOutputStream`.
4247
#[derive(Debug)]
4348
pub struct Stdout {
@@ -61,15 +66,25 @@ impl Stdout {
6166
}
6267
}
6368

64-
impl std::ops::Deref for Stdout {
65-
type Target = AsyncOutputStream;
66-
fn deref(&self) -> &AsyncOutputStream {
67-
&self.stream
69+
impl AsyncWrite for Stdout {
70+
#[inline]
71+
async fn write(&mut self, buf: &[u8]) -> Result<usize> {
72+
self.stream.write(buf).await
6873
}
69-
}
70-
impl std::ops::DerefMut for Stdout {
71-
fn deref_mut(&mut self) -> &mut AsyncOutputStream {
72-
&mut self.stream
74+
75+
#[inline]
76+
async fn flush(&mut self) -> Result<()> {
77+
self.stream.flush().await
78+
}
79+
80+
#[inline]
81+
async fn write_all(&mut self, buf: &[u8]) -> Result<()> {
82+
self.stream.write_all(buf).await
83+
}
84+
85+
#[inline]
86+
fn as_async_output_stream(&self) -> Option<&AsyncOutputStream> {
87+
self.stream.as_async_output_stream()
7388
}
7489
}
7590

@@ -96,15 +111,25 @@ impl Stderr {
96111
}
97112
}
98113

99-
impl std::ops::Deref for Stderr {
100-
type Target = AsyncOutputStream;
101-
fn deref(&self) -> &AsyncOutputStream {
102-
&self.stream
114+
impl AsyncWrite for Stderr {
115+
#[inline]
116+
async fn write(&mut self, buf: &[u8]) -> Result<usize> {
117+
self.stream.write(buf).await
103118
}
104-
}
105-
impl std::ops::DerefMut for Stderr {
106-
fn deref_mut(&mut self) -> &mut AsyncOutputStream {
107-
&mut self.stream
119+
120+
#[inline]
121+
async fn flush(&mut self) -> Result<()> {
122+
self.stream.flush().await
123+
}
124+
125+
#[inline]
126+
async fn write_all(&mut self, buf: &[u8]) -> Result<()> {
127+
self.stream.write_all(buf).await
128+
}
129+
130+
#[inline]
131+
fn as_async_output_stream(&self) -> Option<&AsyncOutputStream> {
132+
self.stream.as_async_output_stream()
108133
}
109134
}
110135

0 commit comments

Comments
 (0)