use core::net::{SocketAddr, SocketAddrV4, SocketAddrV6};
pub trait Address: Sized + Clone {
fn same_host(&self, other: &Self) -> bool;
fn same(&self, other: &Self) -> bool;
fn is_broadcast(&self) -> bool;
}
impl Address for () {
fn same_host(&self, _other: &()) -> bool {
true
}
fn same(&self, _other: &()) -> bool {
true
}
fn is_broadcast(&self) -> bool {
false
}
}
impl Address for SocketAddrV4 {
fn same_host(&self, other: &Self) -> bool {
self.ip() == other.ip()
}
fn same(&self, other: &Self) -> bool {
*self == *other
}
fn is_broadcast(&self) -> bool {
self.ip().is_broadcast()
}
}
impl Address for SocketAddrV6 {
fn same_host(&self, other: &Self) -> bool {
self.ip() == other.ip()
}
fn same(&self, other: &Self) -> bool {
*self == *other
}
fn is_broadcast(&self) -> bool {
false
}
}
impl Address for SocketAddr {
fn same_host(&self, other: &SocketAddr) -> bool {
self.ip() == other.ip()
}
fn same(&self, other: &SocketAddr) -> bool {
*self == *other
}
fn is_broadcast(&self) -> bool {
match self {
SocketAddr::V4(self_addr_v4) => self_addr_v4.ip().is_broadcast(),
_ => false,
}
}
}