From f402e31ffca0d5ae89c9716b8c0c44f0c47aa248 Mon Sep 17 00:00:00 2001 From: jtnunley Date: Mon, 13 Mar 2023 09:31:25 -0700 Subject: [PATCH] Avoid allocation in nb_connect --- x11rb-async/src/rust_connection/nb_connect.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/x11rb-async/src/rust_connection/nb_connect.rs b/x11rb-async/src/rust_connection/nb_connect.rs index 69568e15..68f461d3 100644 --- a/x11rb-async/src/rust_connection/nb_connect.rs +++ b/x11rb-async/src/rust_connection/nb_connect.rs @@ -8,6 +8,7 @@ use std::os::unix::net::UnixStream; use async_io::Async; +use futures_lite::pin; use futures_lite::prelude::*; use futures_lite::stream; @@ -20,8 +21,12 @@ pub(super) async fn connect(addrs: &ParsedDisplay) -> Result<(DefaultStream, usi let screen = addrs.screen as usize; let mut err = None; - stream::iter(addrs.connect_instruction()) - .then(|addr| Box::pin(connect_to_addr(addr))) + let connections = stream::iter(addrs.connect_instruction()).then(connect_to_addr); + + // Pinning the stream lets us use `find_map` below without boxing. + pin!(connections); + + connections .find_map(|result| match result { Ok(stream) => Some(stream), Err(e) => { @@ -46,12 +51,16 @@ async fn connect_to_addr(addr: ConnectAddress<'_>) -> io::Result let mut err = None; // Resolve the hostname. - resolve_host(host) + let streams = resolve_host(host) .await? - .then(|ip_addr| Box::pin(Async::::connect((ip_addr, port)))) + .then(|ip_addr| Async::::connect((ip_addr, port))) .map(|result| { result.and_then(|stream| DefaultStream::from_tcp_stream(stream.into_inner()?)) - }) + }); + + pin!(streams); + + streams .find_map(|result| match result { Ok(stream) => Some(stream), Err(e) => {