@@ -26,6 +26,16 @@ impl TcpStream {
26
26
. map_err ( super :: tcp_listener:: to_io_err) ?;
27
27
Ok ( format ! ( "{addr:?}" ) )
28
28
}
29
+
30
+ pub fn split ( & self ) -> ( ReadHalf < ' _ > , WriteHalf < ' _ > ) {
31
+ ( ReadHalf ( self ) , WriteHalf ( self ) )
32
+ }
33
+ }
34
+
35
+ impl Drop for TcpStream {
36
+ fn drop ( & mut self ) {
37
+ let _ = self . socket . shutdown ( wasi:: sockets:: tcp:: ShutdownType :: Both ) ;
38
+ }
29
39
}
30
40
31
41
impl AsyncRead for TcpStream {
@@ -80,6 +90,42 @@ impl AsyncWrite for &TcpStream {
80
90
}
81
91
}
82
92
93
+ pub struct ReadHalf < ' a > ( & ' a TcpStream ) ;
94
+ impl < ' a > AsyncRead for ReadHalf < ' a > {
95
+ async fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
96
+ self . 0 . read ( buf) . await
97
+ }
98
+ }
99
+
100
+ impl < ' a > Drop for ReadHalf < ' a > {
101
+ fn drop ( & mut self ) {
102
+ let _ = self
103
+ . 0
104
+ . socket
105
+ . shutdown ( wasi:: sockets:: tcp:: ShutdownType :: Receive ) ;
106
+ }
107
+ }
108
+
109
+ pub struct WriteHalf < ' a > ( & ' a TcpStream ) ;
110
+ impl < ' a > AsyncWrite for WriteHalf < ' a > {
111
+ async fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
112
+ self . 0 . write ( buf) . await
113
+ }
114
+
115
+ async fn flush ( & mut self ) -> io:: Result < ( ) > {
116
+ self . 0 . flush ( ) . await
117
+ }
118
+ }
119
+
120
+ impl < ' a > Drop for WriteHalf < ' a > {
121
+ fn drop ( & mut self ) {
122
+ let _ = self
123
+ . 0
124
+ . socket
125
+ . shutdown ( wasi:: sockets:: tcp:: ShutdownType :: Send ) ;
126
+ }
127
+ }
128
+
83
129
fn to_io_err ( err : StreamError ) -> std:: io:: Error {
84
130
match err {
85
131
StreamError :: LastOperationFailed ( err) => Error :: other ( err. to_debug_string ( ) ) ,
0 commit comments