1
+ use std:: pin:: Pin ;
2
+ use std:: sync:: Arc ;
3
+
1
4
use async_h1:: server;
2
- use async_std:: net;
5
+ use async_std:: io:: { self , Read , Write } ;
6
+ use async_std:: net:: { self , TcpStream } ;
3
7
use async_std:: prelude:: * ;
4
- use async_std:: task;
8
+ use async_std:: task:: { self , Context , Poll } ;
5
9
use http_types:: { Response , StatusCode } ;
6
10
7
11
fn main ( ) -> Result < ( ) , async_h1:: Exception > {
@@ -15,7 +19,9 @@ fn main() -> Result<(), async_h1::Exception> {
15
19
let stream = stream?;
16
20
println ! ( "starting new connection from {}" , stream. peer_addr( ) ?) ;
17
21
18
- let stream = Stream :: new ( stream) ;
22
+ // TODO: Delete this line when we implement `Clone` for `TcpStream`.
23
+ let stream = Stream ( Arc :: new ( stream) ) ;
24
+
19
25
server:: connect ( stream. clone ( ) , stream, |_| {
20
26
async { Ok ( Response :: new ( StatusCode :: Ok ) ) }
21
27
} )
@@ -26,59 +32,29 @@ fn main() -> Result<(), async_h1::Exception> {
26
32
} )
27
33
}
28
34
29
- use async_std:: {
30
- io:: { self , Read , Write } ,
31
- net:: TcpStream ,
32
- task:: { Context , Poll } ,
33
- } ;
34
- use std:: {
35
- pin:: Pin ,
36
- sync:: { Arc , Mutex } ,
37
- } ;
38
-
39
- struct Stream {
40
- internal : Arc < Mutex < TcpStream > > ,
41
- }
42
-
43
- impl Stream {
44
- fn new ( internal : TcpStream ) -> Self {
45
- Stream {
46
- internal : Arc :: new ( Mutex :: new ( internal) ) ,
47
- }
48
- }
49
- }
50
-
51
- impl Clone for Stream {
52
- fn clone ( & self ) -> Self {
53
- Stream {
54
- internal : self . internal . clone ( ) ,
55
- }
56
- }
57
- }
35
+ #[ derive( Clone ) ]
36
+ struct Stream ( Arc < TcpStream > ) ;
58
37
59
38
impl Read for Stream {
60
39
fn poll_read (
61
- mut self : Pin < & mut Self > ,
40
+ self : Pin < & mut Self > ,
62
41
cx : & mut Context ,
63
42
buf : & mut [ u8 ] ,
64
43
) -> Poll < io:: Result < usize > > {
65
- < TcpStream as Read > :: poll_read ( Pin :: new ( & mut self . internal . lock ( ) . unwrap ( ) ) , cx, buf)
44
+ Pin :: new ( & mut & * self . 0 ) . poll_read ( cx, buf)
66
45
}
67
46
}
47
+
68
48
impl Write for Stream {
69
- fn poll_write (
70
- mut self : Pin < & mut Self > ,
71
- cx : & mut Context ,
72
- buf : & [ u8 ] ,
73
- ) -> Poll < io:: Result < usize > > {
74
- <TcpStream as Write >:: poll_write ( Pin :: new ( & mut self . internal . lock ( ) . unwrap ( ) ) , cx, buf)
49
+ fn poll_write ( self : Pin < & mut Self > , cx : & mut Context , buf : & [ u8 ] ) -> Poll < io:: Result < usize > > {
50
+ Pin :: new ( & mut & * self . 0 ) . poll_write ( cx, buf)
75
51
}
76
52
77
- fn poll_flush ( mut self : Pin < & mut Self > , cx : & mut Context ) -> Poll < io:: Result < ( ) > > {
78
- < TcpStream as Write > :: poll_flush ( Pin :: new ( & mut self . internal . lock ( ) . unwrap ( ) ) , cx)
53
+ fn poll_flush ( self : Pin < & mut Self > , cx : & mut Context ) -> Poll < io:: Result < ( ) > > {
54
+ Pin :: new ( & mut & * self . 0 ) . poll_flush ( cx)
79
55
}
80
56
81
- fn poll_close ( mut self : Pin < & mut Self > , cx : & mut Context ) -> Poll < io:: Result < ( ) > > {
82
- < TcpStream as Write > :: poll_close ( Pin :: new ( & mut self . internal . lock ( ) . unwrap ( ) ) , cx)
57
+ fn poll_close ( self : Pin < & mut Self > , cx : & mut Context ) -> Poll < io:: Result < ( ) > > {
58
+ Pin :: new ( & mut & * self . 0 ) . poll_close ( cx)
83
59
}
84
60
}
0 commit comments