cidr/
family.rs

1use std::net::{
2	IpAddr,
3	Ipv4Addr,
4	Ipv6Addr,
5};
6
7/// Represents the type of an IP address
8#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
9pub enum Family {
10	/// IPv4
11	Ipv4,
12	/// IPv6
13	Ipv6,
14}
15
16impl Family {
17	/// The length of an address (as bitstring) in the given family
18	#[allow(clippy::len_without_is_empty)]
19	pub const fn len(&self) -> u8 {
20		match self {
21			Self::Ipv4 => 32,
22			Self::Ipv6 => 128,
23		}
24	}
25
26	/// The "unspecified" address (all zero) of the given family
27	pub const fn unspecified_address(&self) -> IpAddr {
28		match self {
29			Self::Ipv4 => IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
30			Self::Ipv6 => IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)),
31		}
32	}
33
34	/// The "loopback" address (`127.0.0.1` or `::1`) of the given family
35	pub const fn loopback_address(&self) -> IpAddr {
36		match self {
37			Self::Ipv4 => IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
38			Self::Ipv6 => IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)),
39		}
40	}
41}