1
1
use std:: io;
2
- use std:: mem :: MaybeUninit ;
2
+ use std:: io :: IoSlice ;
3
3
use std:: pin:: Pin ;
4
4
use std:: task:: { Context , Poll } ;
5
5
use std:: time:: { Duration } ;
6
6
7
- use bytes:: { Buf , BufMut } ;
8
7
use 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 } ;
10
10
use tokio_io_timeout:: TimeoutStream ;
11
11
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
+ }
15
20
16
21
impl < S > TimeoutConnectorStream < S >
17
22
where
@@ -21,74 +26,59 @@ where
21
26
///
22
27
/// There is initially no read or write timeout.
23
28
pub fn new ( stream : TimeoutStream < S > ) -> TimeoutConnectorStream < S > {
24
- TimeoutConnectorStream ( stream)
29
+ TimeoutConnectorStream { stream }
25
30
}
26
31
27
32
/// Returns the current read timeout.
28
33
pub fn read_timeout ( & self ) -> Option < Duration > {
29
- self . 0 . read_timeout ( )
34
+ self . stream . read_timeout ( )
30
35
}
31
36
32
37
/// Sets the read timeout.
33
38
///
34
39
/// This will reset any pending read timeout.
35
40
pub fn set_read_timeout ( & mut self , timeout : Option < Duration > ) {
36
- self . 0 . set_read_timeout ( timeout)
41
+ self . stream . set_read_timeout ( timeout)
37
42
}
38
43
39
44
/// Returns the current write timeout.
40
45
pub fn write_timeout ( & self ) -> Option < Duration > {
41
- self . 0 . write_timeout ( )
46
+ self . stream . write_timeout ( )
42
47
}
43
48
44
49
/// Sets the write timeout.
45
50
///
46
51
/// This will reset any pending write timeout.
47
52
pub fn set_write_timeout ( & mut self , timeout : Option < Duration > ) {
48
- self . 0 . set_write_timeout ( timeout)
53
+ self . stream . set_write_timeout ( timeout)
49
54
}
50
55
51
56
/// Returns a shared reference to the inner stream.
52
57
pub fn get_ref ( & self ) -> & S {
53
- self . 0 . get_ref ( )
58
+ self . stream . get_ref ( )
54
59
}
55
60
56
61
/// Returns a mutable reference to the inner stream.
57
62
pub fn get_mut ( & mut self ) -> & mut S {
58
- self . 0 . get_mut ( )
63
+ self . stream . get_mut ( )
59
64
}
60
65
61
66
/// Consumes the stream, returning the inner stream.
62
67
pub fn into_inner ( self ) -> S {
63
- self . 0 . into_inner ( )
68
+ self . stream . into_inner ( )
64
69
}
65
70
}
66
71
67
72
impl < S > AsyncRead for TimeoutConnectorStream < S >
68
73
where
69
74
S : AsyncRead + AsyncWrite + Unpin ,
70
75
{
71
- unsafe fn prepare_uninitialized_buffer ( & self , buf : & mut [ MaybeUninit < u8 > ] ) -> bool {
72
- self . 0 . prepare_uninitialized_buffer ( buf)
73
- }
74
-
75
76
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 > ,
85
78
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)
92
82
}
93
83
}
94
84
@@ -97,30 +87,31 @@ where
97
87
S : AsyncRead + AsyncWrite + Unpin ,
98
88
{
99
89
fn poll_write (
100
- mut self : Pin < & mut Self > ,
101
- cx : & mut Context ,
90
+ self : Pin < & mut Self > ,
91
+ cx : & mut Context < ' _ > ,
102
92
buf : & [ u8 ] ,
103
93
) -> 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)
105
103
}
106
104
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 ( )
109
107
}
110
108
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)
113
111
}
114
112
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)
124
115
}
125
116
}
126
117
@@ -129,6 +120,15 @@ where
129
120
S : AsyncRead + AsyncWrite + Connection + Unpin ,
130
121
{
131
122
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 ( )
133
133
}
134
134
}
0 commit comments