From ff914f79fcb3b60f312b00cdf18f225a3fe75db6 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Tue, 23 Oct 2018 22:40:56 -0700 Subject: [PATCH] make port required --- src/connector.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/connector.rs b/src/connector.rs index 638da31b..96570854 100644 --- a/src/connector.rs +++ b/src/connector.rs @@ -43,23 +43,15 @@ impl From for ConnectorError { #[derive(Eq, PartialEq, Debug)] pub struct Connect { pub host: String, - pub port: Option, + pub port: u16, pub timeout: Duration, } impl Connect { - pub fn host>(host: T) -> Connect { + pub fn new>(host: T, port: u16) -> Connect { Connect { + port, host: host.as_ref().to_owned(), - port: None, - timeout: Duration::from_secs(1), - } - } - - pub fn host_and_port>(host: T, port: u16) -> Connect { - Connect { - host: host.as_ref().to_owned(), - port: Some(port), timeout: Duration::from_secs(1), } } @@ -171,10 +163,21 @@ impl Future for ConnectorFuture { return fut.poll(); } match self.fut.poll().map_err(ConnectorError::from)? { - Async::Ready((req, _, addrs)) => { + Async::Ready((req, _, mut addrs)) => { if addrs.is_empty() { Err(ConnectorError::NoRecords) } else { + for addr in &mut addrs { + match addr { + SocketAddr::V4(ref mut addr) if addr.port() == 0 => { + addr.set_port(req.port) + } + SocketAddr::V6(ref mut addr) if addr.port() == 0 => { + addr.set_port(req.port) + } + _ => (), + } + } self.fut2 = Some(TcpConnector::new(req, addrs)); self.poll() }