Stylistic changes

tosocketaddrs
Stjepan Glavina 5 years ago
parent 144e1c4c1b
commit ddb4dc87a1

@ -1,18 +1,22 @@
use crate::future::Future;
use crate::task::blocking::JoinHandle;
use cfg_if::cfg_if;
use futures::future::{ready, Ready};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
pub use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6}; use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6};
use std::pin::Pin; use std::pin::Pin;
use cfg_if::cfg_if;
use futures::future::{ready, Ready};
use crate::future::Future;
use crate::io;
use crate::task::blocking;
use crate::task::{Context, Poll};
cfg_if! { cfg_if! {
if #[cfg(feature = "docs")] { if #[cfg(feature = "docs")] {
#[doc(hidden)] #[doc(hidden)]
pub struct ImplFuture<T>(std::marker::PhantomData<T>); pub struct ImplFuture<T>(std::marker::PhantomData<T>);
macro_rules! ret { macro_rules! ret {
($f:tt, $i:ty) => (ImplFuture<crate::io::Result<$i>>); ($f:tt, $i:ty) => (ImplFuture<io::Result<$i>>);
} }
} else { } else {
macro_rules! ret { macro_rules! ret {
@ -21,40 +25,35 @@ cfg_if! {
} }
} }
/// A trait for objects which can be converted or resolved to one or more /// A trait for objects which can be converted or resolved to one or more [`SocketAddr`] values.
/// [`SocketAddr`] values.
/// ///
/// This trait is an async version of [`std::net::ToSocketAddrs`]. /// This trait is an async version of [`std::net::ToSocketAddrs`].
/// ///
/// [`std::net::ToSocketAddrs`]: https://doc.rust-lang.org/std/net/trait.ToSocketAddrs.html /// [`std::net::ToSocketAddrs`]: https://doc.rust-lang.org/std/net/trait.ToSocketAddrs.html
pub trait ToSocketAddrs { pub trait ToSocketAddrs {
/// Returned iterator over socket addresses which this type may correspond /// Returned iterator over socket addresses which this type may correspond to.
/// to.
type Iter: Iterator<Item = SocketAddr> + Send; type Iter: Iterator<Item = SocketAddr> + Send;
/// Converts this object to an iterator of resolved `SocketAddr`s. /// Converts this object to an iterator of resolved `SocketAddr`s.
/// ///
/// The returned iterator may not actually yield any values depending on the /// The returned iterator may not actually yield any values depending on the outcome of any
/// outcome of any resolution performed. /// resolution performed.
/// ///
/// Note that this function may block a backend thread while resolution is /// Note that this function may block a backend thread while resolution is performed.
/// performed.
fn to_socket_addrs(&self) -> ret!(ToSocketAddrsFuture, Self::Iter); fn to_socket_addrs(&self) -> ret!(ToSocketAddrsFuture, Self::Iter);
} }
#[doc(hidden)] #[doc(hidden)]
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub enum ToSocketAddrsFuture<I: Iterator<Item = SocketAddr>> { pub enum ToSocketAddrsFuture<I: Iterator<Item = SocketAddr>> {
Join(JoinHandle<crate::io::Result<I>>), Join(blocking::JoinHandle<io::Result<I>>),
Ready(Ready<crate::io::Result<I>>), Ready(Ready<io::Result<I>>),
} }
impl<I: Iterator<Item = SocketAddr>> Future for ToSocketAddrsFuture<I> { impl<I: Iterator<Item = SocketAddr>> Future for ToSocketAddrsFuture<I> {
type Output = crate::io::Result<I>; type Output = io::Result<I>;
fn poll( fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self: Pin<&mut Self>,
cx: &mut crate::task::Context<'_>,
) -> crate::task::Poll<Self::Output> {
match self.get_mut() { match self.get_mut() {
ToSocketAddrsFuture::Join(f) => Pin::new(&mut *f).poll(cx), ToSocketAddrsFuture::Join(f) => Pin::new(&mut *f).poll(cx),
ToSocketAddrsFuture::Ready(f) => Pin::new(&mut *f).poll(cx), ToSocketAddrsFuture::Ready(f) => Pin::new(&mut *f).poll(cx),
@ -116,7 +115,7 @@ impl ToSocketAddrs for (&str, u16) {
fn to_socket_addrs(&self) -> ret!(ToSocketAddrsFuture, Self::Iter) { fn to_socket_addrs(&self) -> ret!(ToSocketAddrsFuture, Self::Iter) {
let host = self.0.to_string(); let host = self.0.to_string();
let port = self.1; let port = self.1;
let join = crate::task::blocking::spawn(async move { let join = blocking::spawn(async move {
std::net::ToSocketAddrs::to_socket_addrs(&(host.as_str(), port)) std::net::ToSocketAddrs::to_socket_addrs(&(host.as_str(), port))
}); });
ToSocketAddrsFuture::Join(join) ToSocketAddrsFuture::Join(join)
@ -128,9 +127,8 @@ impl ToSocketAddrs for str {
fn to_socket_addrs(&self) -> ret!(ToSocketAddrsFuture, Self::Iter) { fn to_socket_addrs(&self) -> ret!(ToSocketAddrsFuture, Self::Iter) {
let socket_addrs = self.to_string(); let socket_addrs = self.to_string();
let join = crate::task::blocking::spawn(async move { let join =
std::net::ToSocketAddrs::to_socket_addrs(&socket_addrs) blocking::spawn(async move { std::net::ToSocketAddrs::to_socket_addrs(&socket_addrs) });
});
ToSocketAddrsFuture::Join(join) ToSocketAddrsFuture::Join(join)
} }
} }

@ -29,7 +29,9 @@
//! # }) } //! # }) }
//! ``` //! ```
pub use addr::{SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs}; pub use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6};
pub use addr::ToSocketAddrs;
pub use tcp::{Incoming, TcpListener, TcpStream}; pub use tcp::{Incoming, TcpListener, TcpStream};
pub use udp::UdpSocket; pub use udp::UdpSocket;

@ -4,8 +4,8 @@ use cfg_if::cfg_if;
use futures::future; use futures::future;
use std::net::{Ipv4Addr, Ipv6Addr}; use std::net::{Ipv4Addr, Ipv6Addr};
use crate::net::addr::{SocketAddr, ToSocketAddrs};
use crate::net::driver::IoHandle; use crate::net::driver::IoHandle;
use crate::net::{SocketAddr, ToSocketAddrs};
use crate::task::Poll; use crate::task::Poll;
/// A UDP socket. /// A UDP socket.

Loading…
Cancel
Save