diff options
author | David Barsky <me@davidbarsky.com> | 2019-12-21 11:30:00 -0500 |
---|---|---|
committer | Carl Lerche <me@carllerche.com> | 2019-12-21 08:30:00 -0800 |
commit | de5ec6e1bcbb428ee672d7965c0656e628adde46 (patch) | |
tree | 7b3ed0002fb0f2a332639b4ab8e1584501f6c115 /tokio/src/net | |
parent | 3dcd76a38feff3188e1a94f39c4f5feaaa5bdf61 (diff) |
dns: provide `lookup_host` function (#1870)
`ToSocketAddrs` is a sealed trait pending changes in Rust that will allow
defining async trait fns. Until then, `net::lookup_host` is provided as a way
to convert a `T: ToSocketAddrs` into `SocketAddr`s.
Diffstat (limited to 'tokio/src/net')
-rw-r--r-- | tokio/src/net/lookup_host.rs | 38 | ||||
-rw-r--r-- | tokio/src/net/mod.rs | 5 |
2 files changed, 43 insertions, 0 deletions
diff --git a/tokio/src/net/lookup_host.rs b/tokio/src/net/lookup_host.rs new file mode 100644 index 00000000..3098b463 --- /dev/null +++ b/tokio/src/net/lookup_host.rs @@ -0,0 +1,38 @@ +cfg_dns! { + use crate::net::addr::ToSocketAddrs; + + use std::io; + use std::net::SocketAddr; + + /// Performs a DNS resolution. + /// + /// The returned iterator may not actually yield any values depending on the + /// outcome of any resolution performed. + /// + /// This API is not intended to cover all DNS use cases. Anything beyond the + /// basic use case should be done with a specialized library. + /// + /// # Examples + /// + /// To resolve a DNS entry: + /// + /// ```no_run + /// use tokio::net; + /// use std::io; + /// + /// #[tokio::main] + /// async fn main() -> io::Result<()> { + /// for addr in net::lookup_host("localhost:3000").await? { + /// println!("socket address is {}", addr); + /// } + /// + /// Ok(()) + /// } + /// ``` + pub async fn lookup_host<T>(host: T) -> io::Result<impl Iterator<Item = SocketAddr>> + where + T: ToSocketAddrs + { + host.to_socket_addrs().await + } +} diff --git a/tokio/src/net/mod.rs b/tokio/src/net/mod.rs index 2be8f34e..83b12fd2 100644 --- a/tokio/src/net/mod.rs +++ b/tokio/src/net/mod.rs @@ -25,6 +25,11 @@ mod addr; pub use addr::ToSocketAddrs; +cfg_dns! { + mod lookup_host; + pub use lookup_host::lookup_host; +} + cfg_tcp! { pub mod tcp; pub use tcp::listener::TcpListener; |