From fb95de99a617f6ef772a0e684b28de800064b17a Mon Sep 17 00:00:00 2001 From: teor Date: Tue, 13 Apr 2021 17:46:17 +1000 Subject: [PATCH] Refactor the dial result into a From impl --- zebra-network/src/peer_set/initialize.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/zebra-network/src/peer_set/initialize.rs b/zebra-network/src/peer_set/initialize.rs index 9ddadc46..fef16bd6 100644 --- a/zebra-network/src/peer_set/initialize.rs +++ b/zebra-network/src/peer_set/initialize.rs @@ -13,6 +13,7 @@ use futures::{ future::{self, FutureExt}, sink::SinkExt, stream::{FuturesUnordered, StreamExt}, + TryFutureExt, }; use tokio::{ net::{TcpListener, TcpStream}, @@ -445,8 +446,6 @@ where + 'static, C::Future: Send + 'static, { - use CrawlerAction::*; - // CORRECTNESS // // To avoid hangs, the dialer must only await: @@ -461,14 +460,22 @@ where // the handshake has timeouts, so it shouldn't hang connector .call(candidate.addr) - .map(move |res| match res { + .map_err(|e| (candidate, e)) + .map(Into::into) + .await +} + +impl From, (MetaAddr, BoxError)>> for CrawlerAction { + fn from(dial_result: Result, (MetaAddr, BoxError)>) -> Self { + use CrawlerAction::*; + match dial_result { Ok(peer_set_change) => HandshakeConnected { peer_set_change }, - Err(e) => { + Err((candidate, e)) => { debug!(?candidate.addr, ?e, "failed to connect to candidate"); HandshakeFailed { failed_addr: candidate, } } - }) - .await + } + } }