11use std:: io;
2- use std:: mem :: MaybeUninit ;
2+ use std:: io :: IoSlice ;
33use std:: pin:: Pin ;
44use std:: task:: { Context , Poll } ;
55use std:: time:: { Duration } ;
66
7- use bytes:: { Buf , BufMut } ;
87use hyper:: client:: connect:: { Connected , Connection } ;
9- use tokio:: io:: { AsyncRead , AsyncWrite } ;
8+ use pin_project_lite:: pin_project;
9+ use tokio:: io:: { AsyncRead , AsyncWrite , ReadBuf } ;
1010use tokio_io_timeout:: TimeoutStream ;
1111
12- /// A timeout stream that implements required traits to be a Connector
13- #[ derive( Debug ) ]
14- pub struct TimeoutConnectorStream < S > ( TimeoutStream < S > ) ;
12+ pin_project ! {
13+ /// A timeout stream that implements required traits to be a Connector
14+ #[ derive( Debug ) ]
15+ pub struct TimeoutConnectorStream <S > {
16+ #[ pin]
17+ stream: TimeoutStream <S >
18+ }
19+ }
1520
1621impl < S > TimeoutConnectorStream < S >
1722where
@@ -21,74 +26,59 @@ where
2126 ///
2227 /// There is initially no read or write timeout.
2328 pub fn new ( stream : TimeoutStream < S > ) -> TimeoutConnectorStream < S > {
24- TimeoutConnectorStream ( stream)
29+ TimeoutConnectorStream { stream }
2530 }
2631
2732 /// Returns the current read timeout.
2833 pub fn read_timeout ( & self ) -> Option < Duration > {
29- self . 0 . read_timeout ( )
34+ self . stream . read_timeout ( )
3035 }
3136
3237 /// Sets the read timeout.
3338 ///
3439 /// This will reset any pending read timeout.
3540 pub fn set_read_timeout ( & mut self , timeout : Option < Duration > ) {
36- self . 0 . set_read_timeout ( timeout)
41+ self . stream . set_read_timeout ( timeout)
3742 }
3843
3944 /// Returns the current write timeout.
4045 pub fn write_timeout ( & self ) -> Option < Duration > {
41- self . 0 . write_timeout ( )
46+ self . stream . write_timeout ( )
4247 }
4348
4449 /// Sets the write timeout.
4550 ///
4651 /// This will reset any pending write timeout.
4752 pub fn set_write_timeout ( & mut self , timeout : Option < Duration > ) {
48- self . 0 . set_write_timeout ( timeout)
53+ self . stream . set_write_timeout ( timeout)
4954 }
5055
5156 /// Returns a shared reference to the inner stream.
5257 pub fn get_ref ( & self ) -> & S {
53- self . 0 . get_ref ( )
58+ self . stream . get_ref ( )
5459 }
5560
5661 /// Returns a mutable reference to the inner stream.
5762 pub fn get_mut ( & mut self ) -> & mut S {
58- self . 0 . get_mut ( )
63+ self . stream . get_mut ( )
5964 }
6065
6166 /// Consumes the stream, returning the inner stream.
6267 pub fn into_inner ( self ) -> S {
63- self . 0 . into_inner ( )
68+ self . stream . into_inner ( )
6469 }
6570}
6671
6772impl < S > AsyncRead for TimeoutConnectorStream < S >
6873where
6974 S : AsyncRead + AsyncWrite + Unpin ,
7075{
71- unsafe fn prepare_uninitialized_buffer ( & self , buf : & mut [ MaybeUninit < u8 > ] ) -> bool {
72- self . 0 . prepare_uninitialized_buffer ( buf)
73- }
74-
7576 fn poll_read (
76- mut self : Pin < & mut Self > ,
77- cx : & mut Context ,
78- buf : & mut [ u8 ] ,
79- ) -> Poll < Result < usize , io:: Error > > {
80- Pin :: new ( & mut self . 0 ) . poll_read ( cx, buf)
81- }
82-
83- fn poll_read_buf < B > (
84- mut self : Pin < & mut Self > ,
77+ self : Pin < & mut Self > ,
8578 cx : & mut Context ,
86- buf : & mut B ,
87- ) -> Poll < Result < usize , io:: Error > >
88- where
89- B : BufMut ,
90- {
91- Pin :: new ( & mut self . 0 ) . poll_read_buf ( cx, buf)
79+ buf : & mut ReadBuf ,
80+ ) -> Poll < Result < ( ) , io:: Error > > {
81+ self . project ( ) . stream . poll_read ( cx, buf)
9282 }
9383}
9484
@@ -97,30 +87,31 @@ where
9787 S : AsyncRead + AsyncWrite + Unpin ,
9888{
9989 fn poll_write (
100- mut self : Pin < & mut Self > ,
101- cx : & mut Context ,
90+ self : Pin < & mut Self > ,
91+ cx : & mut Context < ' _ > ,
10292 buf : & [ u8 ] ,
10393 ) -> Poll < Result < usize , io:: Error > > {
104- Pin :: new ( & mut self . 0 ) . poll_write ( cx, buf)
94+ self . project ( ) . stream . poll_write ( cx, buf)
95+ }
96+
97+ fn poll_write_vectored (
98+ self : Pin < & mut Self > ,
99+ cx : & mut Context < ' _ > ,
100+ bufs : & [ IoSlice < ' _ > ] ,
101+ ) -> Poll < Result < usize , io:: Error > > {
102+ self . project ( ) . stream . poll_write_vectored ( cx, bufs)
105103 }
106104
107- fn poll_flush ( mut self : Pin < & mut Self > , cx : & mut Context ) -> Poll < Result < ( ) , io :: Error > > {
108- Pin :: new ( & mut self . 0 ) . poll_flush ( cx )
105+ fn is_write_vectored ( & self ) -> bool {
106+ self . stream . is_write_vectored ( )
109107 }
110108
111- fn poll_shutdown ( mut self : Pin < & mut Self > , cx : & mut Context ) -> Poll < Result < ( ) , io:: Error > > {
112- Pin :: new ( & mut self . 0 ) . poll_shutdown ( cx)
109+ fn poll_flush ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , io:: Error > > {
110+ self . project ( ) . stream . poll_flush ( cx)
113111 }
114112
115- fn poll_write_buf < B > (
116- mut self : Pin < & mut Self > ,
117- cx : & mut Context ,
118- buf : & mut B ,
119- ) -> Poll < Result < usize , io:: Error > >
120- where
121- B : Buf ,
122- {
123- Pin :: new ( & mut self . 0 ) . poll_write_buf ( cx, buf)
113+ fn poll_shutdown ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , io:: Error > > {
114+ self . project ( ) . stream . poll_shutdown ( cx)
124115 }
125116}
126117
@@ -129,6 +120,15 @@ where
129120 S : AsyncRead + AsyncWrite + Connection + Unpin ,
130121{
131122 fn connected ( & self ) -> Connected {
132- self . 0 . get_ref ( ) . connected ( )
123+ self . stream . get_ref ( ) . connected ( )
124+ }
125+ }
126+
127+ impl < S > Connection for Pin < Box < TimeoutConnectorStream < S > > >
128+ where
129+ S : AsyncRead + AsyncWrite + Connection + Unpin ,
130+ {
131+ fn connected ( & self ) -> Connected {
132+ self . stream . get_ref ( ) . connected ( )
133133 }
134134}
0 commit comments