use std::net::{Ipv4Addr, Ipv6Addr};
pub trait IpAddress:
crate::private::Sealed
+ PartialOrd
+ Copy
+ std::fmt::Display
+ std::fmt::Debug
+ PartialEq
+ Eq
+ std::hash::Hash
{
const BITS: u8;
fn is_unspecified(&self) -> bool;
fn to_u128(self) -> u128;
fn from_u128(bits: u128) -> Self;
fn parse_addr(s: &str) -> Option<Self>;
}
impl IpAddress for Ipv4Addr {
const BITS: u8 = 32;
fn is_unspecified(&self) -> bool {
Ipv4Addr::is_unspecified(self)
}
fn to_u128(self) -> u128 {
self.to_bits() as u128
}
fn from_u128(bits: u128) -> Self {
Ipv4Addr::from_bits(bits as u32)
}
fn parse_addr(s: &str) -> Option<Self> {
s.parse().ok()
}
}
impl IpAddress for Ipv6Addr {
const BITS: u8 = 128;
fn is_unspecified(&self) -> bool {
Ipv6Addr::is_unspecified(self)
}
fn to_u128(self) -> u128 {
self.to_bits()
}
fn from_u128(bits: u128) -> Self {
Ipv6Addr::from_bits(bits)
}
fn parse_addr(s: &str) -> Option<Self> {
s.parse().ok()
}
}