@@ -78,61 +78,10 @@ impl TcpStream {
7878 /// # Ok(()) }) }
7979 /// ```
8080 pub async fn connect < A : ToSocketAddrs > ( addrs : A ) -> io:: Result < TcpStream > {
81- enum State {
82- Waiting ( TcpStream ) ,
83- Error ( io:: Error ) ,
84- Done ,
85- }
86-
8781 let mut last_err = None ;
8882
8983 for addr in addrs. to_socket_addrs ( ) ? {
90- let mut state = {
91- match mio:: net:: TcpStream :: connect ( & addr) {
92- Ok ( mio_stream) => {
93- #[ cfg( unix) ]
94- let stream = TcpStream {
95- raw_fd : mio_stream. as_raw_fd ( ) ,
96- io_handle : IoHandle :: new ( mio_stream) ,
97- } ;
98-
99- #[ cfg( windows) ]
100- let stream = TcpStream {
101- // raw_socket: mio_stream.as_raw_socket(),
102- io_handle : IoHandle :: new ( mio_stream) ,
103- } ;
104-
105- State :: Waiting ( stream)
106- }
107- Err ( err) => State :: Error ( err) ,
108- }
109- } ;
110-
111- let res = future:: poll_fn ( |cx| {
112- match mem:: replace ( & mut state, State :: Done ) {
113- State :: Waiting ( stream) => {
114- // Once we've connected, wait for the stream to be writable as that's when
115- // the actual connection has been initiated. Once we're writable we check
116- // for `take_socket_error` to see if the connect actually hit an error or
117- // not.
118- //
119- // If all that succeeded then we ship everything on up.
120- if let Poll :: Pending = stream. io_handle . poll_writable ( cx) ? {
121- state = State :: Waiting ( stream) ;
122- return Poll :: Pending ;
123- }
124-
125- if let Some ( err) = stream. io_handle . get_ref ( ) . take_error ( ) ? {
126- return Poll :: Ready ( Err ( err) ) ;
127- }
128-
129- Poll :: Ready ( Ok ( stream) )
130- }
131- State :: Error ( err) => Poll :: Ready ( Err ( err) ) ,
132- State :: Done => panic ! ( "`TcpStream::connect()` future polled after completion" ) ,
133- }
134- } )
135- . await ;
84+ let res = Self :: connect_to ( addr) . await ;
13685
13786 match res {
13887 Ok ( stream) => return Ok ( stream) ,
@@ -148,6 +97,60 @@ impl TcpStream {
14897 } ) )
14998 }
15099
100+ /// Creates a new TCP stream connected to the specified address.
101+ async fn connect_to ( addr : SocketAddr ) -> io:: Result < TcpStream > {
102+ let stream = mio:: net:: TcpStream :: connect ( & addr) . map ( |mio_stream| {
103+ #[ cfg( unix) ]
104+ let stream = TcpStream {
105+ raw_fd : mio_stream. as_raw_fd ( ) ,
106+ io_handle : IoHandle :: new ( mio_stream) ,
107+ } ;
108+
109+ #[ cfg( windows) ]
110+ let stream = TcpStream {
111+ // raw_socket: mio_stream.as_raw_socket(),
112+ io_handle : IoHandle :: new ( mio_stream) ,
113+ } ;
114+
115+ stream
116+ } ) ;
117+
118+ enum State {
119+ Waiting ( TcpStream ) ,
120+ Error ( io:: Error ) ,
121+ Done ,
122+ }
123+ let mut state = match stream {
124+ Ok ( stream) => State :: Waiting ( stream) ,
125+ Err ( err) => State :: Error ( err) ,
126+ } ;
127+ future:: poll_fn ( |cx| {
128+ match mem:: replace ( & mut state, State :: Done ) {
129+ State :: Waiting ( stream) => {
130+ // Once we've connected, wait for the stream to be writable as that's when
131+ // the actual connection has been initiated. Once we're writable we check
132+ // for `take_socket_error` to see if the connect actually hit an error or
133+ // not.
134+ //
135+ // If all that succeeded then we ship everything on up.
136+ if let Poll :: Pending = stream. io_handle . poll_writable ( cx) ? {
137+ state = State :: Waiting ( stream) ;
138+ return Poll :: Pending ;
139+ }
140+
141+ if let Some ( err) = stream. io_handle . get_ref ( ) . take_error ( ) ? {
142+ return Poll :: Ready ( Err ( err) ) ;
143+ }
144+
145+ Poll :: Ready ( Ok ( stream) )
146+ }
147+ State :: Error ( err) => Poll :: Ready ( Err ( err) ) ,
148+ State :: Done => panic ! ( "`TcpStream::connect_to()` future polled after completion" ) ,
149+ }
150+ } )
151+ . await
152+ }
153+
151154 /// Returns the local address that this stream is connected to.
152155 ///
153156 /// ## Examples
0 commit comments