1
- use std:: cell:: RefCell ;
2
-
3
1
use wasi:: {
4
2
io:: streams:: { InputStream , OutputStream } ,
5
3
sockets:: tcp:: TcpSocket ,
6
4
} ;
7
5
8
- use crate :: io:: { self , AsyncInputStream , AsyncOutputStream , AsyncRead , AsyncWrite } ;
6
+ use crate :: io:: { self , AsyncInputStream , AsyncOutputStream } ;
9
7
10
8
/// A TCP stream between a local and a remote socket.
11
9
pub struct TcpStream {
12
- input : RefCell < AsyncInputStream > ,
13
- output : RefCell < AsyncOutputStream > ,
10
+ input : AsyncInputStream ,
11
+ output : AsyncOutputStream ,
14
12
socket : TcpSocket ,
15
13
}
16
14
17
15
impl TcpStream {
18
16
pub ( crate ) fn new ( input : InputStream , output : OutputStream , socket : TcpSocket ) -> Self {
19
17
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) ,
22
20
socket,
23
21
}
24
22
}
@@ -42,43 +40,63 @@ impl Drop for TcpStream {
42
40
}
43
41
}
44
42
45
- impl AsyncRead for TcpStream {
43
+ impl io :: AsyncRead for TcpStream {
46
44
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 )
48
50
}
49
51
}
50
52
51
- impl AsyncRead for & TcpStream {
53
+ impl io :: AsyncRead for & TcpStream {
52
54
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 ( )
54
60
}
55
61
}
56
62
57
- impl AsyncWrite for TcpStream {
63
+ impl io :: AsyncWrite for TcpStream {
58
64
async fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
59
- self . output . borrow_mut ( ) . write ( buf) . await
65
+ self . output . write ( buf) . await
60
66
}
61
67
62
68
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 )
64
74
}
65
75
}
66
76
67
- impl AsyncWrite for & TcpStream {
77
+ impl io :: AsyncWrite for & TcpStream {
68
78
async fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
69
- self . output . borrow_mut ( ) . write ( buf) . await
79
+ self . output . write ( buf) . await
70
80
}
71
81
72
82
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 ( )
74
88
}
75
89
}
76
90
77
91
pub struct ReadHalf < ' a > ( & ' a TcpStream ) ;
78
- impl < ' a > AsyncRead for ReadHalf < ' a > {
92
+ impl < ' a > io :: AsyncRead for ReadHalf < ' a > {
79
93
async fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
80
94
self . 0 . read ( buf) . await
81
95
}
96
+
97
+ fn as_async_input_stream ( & self ) -> Option < & AsyncInputStream > {
98
+ self . 0 . as_async_input_stream ( )
99
+ }
82
100
}
83
101
84
102
impl < ' a > Drop for ReadHalf < ' a > {
@@ -91,14 +109,18 @@ impl<'a> Drop for ReadHalf<'a> {
91
109
}
92
110
93
111
pub struct WriteHalf < ' a > ( & ' a TcpStream ) ;
94
- impl < ' a > AsyncWrite for WriteHalf < ' a > {
112
+ impl < ' a > io :: AsyncWrite for WriteHalf < ' a > {
95
113
async fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
96
114
self . 0 . write ( buf) . await
97
115
}
98
116
99
117
async fn flush ( & mut self ) -> io:: Result < ( ) > {
100
118
self . 0 . flush ( ) . await
101
119
}
120
+
121
+ fn as_async_output_stream ( & self ) -> Option < & AsyncOutputStream > {
122
+ self . 0 . as_async_output_stream ( )
123
+ }
102
124
}
103
125
104
126
impl < ' a > Drop for WriteHalf < ' a > {
0 commit comments