use super::*;
use std::hash::Hash;
pub trait ToSocketAddrs {
type Iter: Iterator<Item = Self::SocketAddr>;
type SocketAddr: SocketAddrExt + Copy;
type Error: core::fmt::Debug;
fn to_socket_addrs(&self) -> Result<Self::Iter, Self::Error>;
}
#[cfg(feature = "std")]
impl<T, I> ToSocketAddrs for T
where
T: std::net::ToSocketAddrs<Iter = I>,
I: Iterator<Item = std::net::SocketAddr>,
{
type Iter = I;
type SocketAddr = std::net::SocketAddr;
type Error = std::io::Error;
fn to_socket_addrs(&self) -> Result<Self::Iter, Self::Error> {
std::net::ToSocketAddrs::to_socket_addrs(self)
}
}
pub trait SocketAddrExt:
Sized + ToSocketAddrs + Copy + core::fmt::Display + core::fmt::Debug + Send + Eq + Hash
{
fn is_multicast(&self) -> bool;
fn port(&self) -> u16;
#[allow(unused_variables)]
fn conforming_to(&self, local: Self) -> Option<Self> {
Some(*self)
}
fn addr_to_string(&self) -> String;
fn as_uri_buf(&self, scheme: &str) -> UriBuf {
UriBuf::from_scheme_host_port(scheme, self.addr_to_string(), Some(self.port()))
}
}
#[cfg(feature = "std")]
impl SocketAddrExt for std::net::SocketAddr {
fn is_multicast(&self) -> bool {
self.ip().is_multicast()
|| if let std::net::IpAddr::V4(addr) = self.ip() {
addr.is_broadcast()
} else {
false
}
}
fn port(&self) -> u16 {
self.port()
}
fn conforming_to(&self, local: Self) -> Option<Self> {
if self.is_ipv6() == local.is_ipv6() {
Some(*self)
} else if let std::net::SocketAddr::V4(v4) = self {
Some((v4.ip().to_ipv6_mapped(), v4.port()).into())
} else {
None
}
}
fn addr_to_string(&self) -> String {
self.ip().to_string()
}
fn as_uri_buf(&self, scheme: &str) -> UriBuf {
UriBuf::from_scheme_host_port(scheme, self.ip().to_string(), Some(self.port()))
}
}