use std::future; use std::io; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
/// Converts or resolves without blocking to one or more `SocketAddr` values. /// /// # DNS /// /// Implementations of `ToSocketAddrs` for string types require a DNS lookup. /// /// # Calling /// /// Currently, this trait is only used as an argument to Tokio functions that /// need to reference a target socket address. To perform a `SocketAddr` /// conversion directly, use [`lookup_host()`](super::lookup_host()). /// /// This trait is sealed and is intended to be opaque. The details of the trait /// will change. Stabilization is pending enhancements to the Rust language. pubtrait ToSocketAddrs: sealed::ToSocketAddrsPriv {}
type ReadyFuture<T> = future::Ready<io::Result<T>>;
// This uses a helper method because clippy doesn't like the `to_vec()` // call here (it will allocate, whereas `self.iter().copied()` would // not), but it's actually necessary in order to ensure that the // returned iterator is valid for the `'static` lifetime, which the // borrowed `slice::Iter` iterator would not be. // // Note that we can't actually add an `allow` attribute for // `clippy::unnecessary_to_owned` here, as Tokio's CI runs clippy lints // on Rust 1.52 to avoid breaking LTS releases of Tokio. Users of newer // Rust versions who see this lint should just ignore it. let iter = slice_to_vec(self).into_iter();
future::ready(Ok(iter))
}
}
cfg_net! { // ===== impl str =====
impl ToSocketAddrs for str {}
impl sealed::ToSocketAddrsPriv for str { type Iter = sealed::OneOrMore; type Future = sealed::MaybeReady;
fn to_socket_addrs(&self, _: sealed::Internal) -> Self::Future { usecrate::blocking::spawn_blocking; use sealed::MaybeReady;
// First check if the input parses as a socket address let res: Result<SocketAddr, _> = self.parse();
iflet Ok(addr) = res { return MaybeReady(sealed::State::Ready(Some(addr)));
}
// Run DNS lookup on the blocking pool let s = self.to_owned();
impl sealed::ToSocketAddrsPriv for (&str, u16) { type Iter = sealed::OneOrMore; type Future = sealed::MaybeReady;
fn to_socket_addrs(&self, _: sealed::Internal) -> Self::Future { usecrate::blocking::spawn_blocking; use sealed::MaybeReady;
let (host, port) = *self;
// try to parse the host as a regular IP address first iflet Ok(addr) = host.parse::<Ipv4Addr>() { let addr = SocketAddrV4::new(addr, port); let addr = SocketAddr::V4(addr);
impl sealed::ToSocketAddrsPriv for String { type Iter = <str as sealed::ToSocketAddrsPriv>::Iter; type Future = <str as sealed::ToSocketAddrsPriv>::Future;
pub(crate) mod sealed { //! The contents of this trait are intended to remain private and __not__ //! part of the `ToSocketAddrs` public API. The details will change over //! time.
use std::future::Future; use std::io; use std::net::SocketAddr;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.