Re-add temporarily TcpStream::connect_std (#1508)

This commit is contained in:
Sean McArthur 2019-08-29 11:45:17 -07:00 committed by GitHub
parent a59e096c47
commit 4e26258ac3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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<TcpStream> {
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