1- use std:: cell:: RefCell ;
2-
31use wasi:: {
42 io:: streams:: { InputStream , OutputStream } ,
53 sockets:: tcp:: TcpSocket ,
64} ;
75
8- use crate :: io:: { self , AsyncInputStream , AsyncOutputStream , AsyncRead , AsyncWrite } ;
6+ use crate :: io:: { self , AsyncInputStream , AsyncOutputStream } ;
97
108/// A TCP stream between a local and a remote socket.
119pub struct TcpStream {
12- input : RefCell < AsyncInputStream > ,
13- output : RefCell < AsyncOutputStream > ,
10+ input : AsyncInputStream ,
11+ output : AsyncOutputStream ,
1412 socket : TcpSocket ,
1513}
1614
1715impl TcpStream {
1816 pub ( crate ) fn new ( input : InputStream , output : OutputStream , socket : TcpSocket ) -> Self {
1917 TcpStream {
20- input : RefCell :: new ( AsyncInputStream :: new ( input) ) ,
21- output : RefCell :: new ( AsyncOutputStream :: new ( output) ) ,
18+ input : AsyncInputStream :: new ( input) ,
19+ output : AsyncOutputStream :: new ( output) ,
2220 socket,
2321 }
2422 }
@@ -42,43 +40,63 @@ impl Drop for TcpStream {
4240 }
4341}
4442
45- impl AsyncRead for TcpStream {
43+ impl io :: AsyncRead for TcpStream {
4644 async fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
47- self . input . borrow_mut ( ) . read ( buf) . await
45+ self . input . read ( buf) . await
46+ }
47+
48+ fn as_async_input_stream ( & self ) -> Option < & AsyncInputStream > {
49+ Some ( & self . input )
4850 }
4951}
5052
51- impl AsyncRead for & TcpStream {
53+ impl io :: AsyncRead for & TcpStream {
5254 async fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
53- self . input . borrow_mut ( ) . read ( buf) . await
55+ self . input . read ( buf) . await
56+ }
57+
58+ fn as_async_input_stream ( & self ) -> Option < & AsyncInputStream > {
59+ ( * * self ) . as_async_input_stream ( )
5460 }
5561}
5662
57- impl AsyncWrite for TcpStream {
63+ impl io :: AsyncWrite for TcpStream {
5864 async fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
59- self . output . borrow_mut ( ) . write ( buf) . await
65+ self . output . write ( buf) . await
6066 }
6167
6268 async fn flush ( & mut self ) -> io:: Result < ( ) > {
63- self . output . borrow_mut ( ) . flush ( ) . await
69+ self . output . flush ( ) . await
70+ }
71+
72+ fn as_async_output_stream ( & self ) -> Option < & AsyncOutputStream > {
73+ Some ( & self . output )
6474 }
6575}
6676
67- impl AsyncWrite for & TcpStream {
77+ impl io :: AsyncWrite for & TcpStream {
6878 async fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
69- self . output . borrow_mut ( ) . write ( buf) . await
79+ self . output . write ( buf) . await
7080 }
7181
7282 async fn flush ( & mut self ) -> io:: Result < ( ) > {
73- self . output . borrow_mut ( ) . flush ( ) . await
83+ self . output . flush ( ) . await
84+ }
85+
86+ fn as_async_output_stream ( & self ) -> Option < & AsyncOutputStream > {
87+ ( * * self ) . as_async_output_stream ( )
7488 }
7589}
7690
7791pub struct ReadHalf < ' a > ( & ' a TcpStream ) ;
78- impl < ' a > AsyncRead for ReadHalf < ' a > {
92+ impl < ' a > io :: AsyncRead for ReadHalf < ' a > {
7993 async fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
8094 self . 0 . read ( buf) . await
8195 }
96+
97+ fn as_async_input_stream ( & self ) -> Option < & AsyncInputStream > {
98+ self . 0 . as_async_input_stream ( )
99+ }
82100}
83101
84102impl < ' a > Drop for ReadHalf < ' a > {
@@ -91,14 +109,18 @@ impl<'a> Drop for ReadHalf<'a> {
91109}
92110
93111pub struct WriteHalf < ' a > ( & ' a TcpStream ) ;
94- impl < ' a > AsyncWrite for WriteHalf < ' a > {
112+ impl < ' a > io :: AsyncWrite for WriteHalf < ' a > {
95113 async fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
96114 self . 0 . write ( buf) . await
97115 }
98116
99117 async fn flush ( & mut self ) -> io:: Result < ( ) > {
100118 self . 0 . flush ( ) . await
101119 }
120+
121+ fn as_async_output_stream ( & self ) -> Option < & AsyncOutputStream > {
122+ self . 0 . as_async_output_stream ( )
123+ }
102124}
103125
104126impl < ' a > Drop for WriteHalf < ' a > {
0 commit comments