From 4e26258ac340b76ef27f1d38d967cfb2b682c2a4 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Thu, 29 Aug 2019 11:45:17 -0700 Subject: [PATCH] Re-add temporarily TcpStream::connect_std (#1508) --- tokio-net/src/tcp/stream.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tokio-net/src/tcp/stream.rs b/tokio-net/src/tcp/stream.rs index 9edde0273..7996d285c 100644 --- a/tokio-net/src/tcp/stream.rs +++ b/tokio-net/src/tcp/stream.rs @@ -151,6 +151,34 @@ impl TcpStream { Ok(TcpStream { io }) } + // Connect a TcpStream asynchronously that may be built with a net2 TcpBuilder. + // + // This should be removed in favor of some in-crate TcpSocket builder API. + #[doc(hidden)] + pub async fn connect_std( + stream: net::TcpStream, + addr: &SocketAddr, + handle: &Handle, + ) -> io::Result { + let io = mio::net::TcpStream::connect_stream(stream, addr)?; + let io = PollEvented::new_with_handle(io, handle)?; + let stream = TcpStream { io }; + + // Once we've connected, wait for the stream to be writable as + // that's when the actual connection has been initiated. Once we're + // writable we check for `take_socket_error` to see if the connect + // actually hit an error or not. + // + // If all that succeeded then we ship everything on up. + poll_fn(|cx| stream.io.poll_write_ready(cx)).await?; + + if let Some(e) = stream.io.get_ref().take_error()? { + return Err(e); + } + + Ok(stream) + } + /// Returns the local address that this stream is bound to. /// /// # Examples