diff options
Diffstat (limited to 'cli/resolve_addr.rs')
-rw-r--r-- | cli/resolve_addr.rs | 34 |
1 files changed, 18 insertions, 16 deletions
diff --git a/cli/resolve_addr.rs b/cli/resolve_addr.rs index 5a4c9d54b..a0ff59b76 100644 --- a/cli/resolve_addr.rs +++ b/cli/resolve_addr.rs @@ -1,10 +1,11 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. use deno::ErrBox; -use futures::Async; -use futures::Future; -use futures::Poll; +use std::future::Future; use std::net::SocketAddr; use std::net::ToSocketAddrs; +use std::pin::Pin; +use std::task::Context; +use std::task::Poll; /// Resolve network address. Returns a future. pub fn resolve_addr(hostname: &str, port: u16) -> ResolveAddrFuture { @@ -20,17 +21,17 @@ pub struct ResolveAddrFuture { } impl Future for ResolveAddrFuture { - type Item = SocketAddr; - type Error = ErrBox; + type Output = Result<SocketAddr, ErrBox>; - fn poll(&mut self) -> Poll<Self::Item, Self::Error> { + fn poll(self: Pin<&mut Self>, _cx: &mut Context) -> Poll<Self::Output> { + let inner = self.get_mut(); // The implementation of this is not actually async at the moment, // however we intend to use async DNS resolution in the future and // so we expose this as a future instead of Result. // Default to localhost if given just the port. Example: ":80" - let addr: &str = if !self.hostname.is_empty() { - &self.hostname + let addr: &str = if !inner.hostname.is_empty() { + &inner.hostname } else { "0.0.0.0" }; @@ -43,19 +44,20 @@ impl Future for ResolveAddrFuture { } else { addr }; - let addr_port_pair = (addr, self.port); + let addr_port_pair = (addr, inner.port); let r = addr_port_pair.to_socket_addrs().map_err(ErrBox::from); - r.and_then(|mut iter| match iter.next() { - Some(a) => Ok(Async::Ready(a)), + Poll::Ready(r.and_then(|mut iter| match iter.next() { + Some(a) => Ok(a), None => panic!("There should be at least one result"), - }) + })) } } #[cfg(test)] mod tests { use super::*; + use futures::executor::block_on; use std::net::Ipv4Addr; use std::net::Ipv6Addr; use std::net::SocketAddrV4; @@ -65,7 +67,7 @@ mod tests { fn resolve_addr1() { let expected = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 80)); - let actual = resolve_addr("127.0.0.1", 80).wait().unwrap(); + let actual = block_on(resolve_addr("127.0.0.1", 80)).unwrap(); assert_eq!(actual, expected); } @@ -73,7 +75,7 @@ mod tests { fn resolve_addr2() { let expected = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 0), 80)); - let actual = resolve_addr("", 80).wait().unwrap(); + let actual = block_on(resolve_addr("", 80)).unwrap(); assert_eq!(actual, expected); } @@ -81,7 +83,7 @@ mod tests { fn resolve_addr3() { let expected = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(192, 0, 2, 1), 25)); - let actual = resolve_addr("192.0.2.1", 25).wait().unwrap(); + let actual = block_on(resolve_addr("192.0.2.1", 25)).unwrap(); assert_eq!(actual, expected); } @@ -93,7 +95,7 @@ mod tests { 0, 0, )); - let actual = resolve_addr("[2001:db8::1]", 8080).wait().unwrap(); + let actual = block_on(resolve_addr("[2001:db8::1]", 8080)).unwrap(); assert_eq!(actual, expected); } } |