Trait net_types::ip::IpAddress

source ·
pub trait IpAddress: Sized + Eq + PartialEq + PartialOrd + Ord + Hash + Copy + Display + Debug + Default + Sync + Send + LinkLocalAddress + ScopeableAddress + GenericOverIp<Self::Version, Type = Self> + GenericOverIp<Ipv4, Type = Ipv4Addr> + GenericOverIp<Ipv6, Type = Ipv6Addr> + Sealed + 'static {
    type Version: Ip<Addr = Self>;

    const BYTES: u8;

    // Required methods
    fn bytes(&self) -> &[u8] ;
    fn mask(&self, bits: u8) -> Self;
    fn to_ip_addr(&self) -> IpAddr;
    fn common_prefix_len(&self, other: &Self) -> u8;
    fn is_unicast_in_subnet(&self, subnet: &Subnet<Self>) -> bool;

    // Provided method
    fn is_loopback(&self) -> bool { ... }
Expand description

An IPv4 or IPv6 address.

IpAddress is implemented by Ipv4Addr and Ipv6Addr. It is sealed, and there are guaranteed to be no other implementors besides these. Code - including unsafe code - may rely on this assumption for its correctness and soundness.

Required Associated Types§


type Version: Ip<Addr = Self>

The IP version type of this address.

Ipv4 for Ipv4Addr and Ipv6 for Ipv6Addr.

Required Associated Constants§


const BYTES: u8

The number of bytes in an address of this type.

4 for IPv4 and 16 for IPv6.

Required Methods§


fn bytes(&self) -> &[u8]

Gets the underlying bytes of the address.


fn mask(&self, bits: u8) -> Self

Masks off the top bits of the address.

Returns a copy of self where all but the top bits bits are set to 0.


mask panics if bits is out of range - if it is greater than 32 for IPv4 or greater than 128 for IPv6.


fn to_ip_addr(&self) -> IpAddr

Converts a statically-typed IP address into a dynamically-typed one.


fn common_prefix_len(&self, other: &Self) -> u8

Calculates the common prefix length between this address and other.


fn is_unicast_in_subnet(&self, subnet: &Subnet<Self>) -> bool

Is this a unicast address contained in the given subnet?

is_unicast_in_subnet returns true if the given subnet contains this address and the address is none of:

  • a multicast address
  • the IPv4 limited broadcast address
  • the IPv4 subnet-specific broadcast address for the given subnet
  • an IPv4 address whose host bits (those bits following the network prefix) are all 0
  • the unspecified address
  • an IPv4 Class E address

Note two exceptions to these rules: If subnet is an IPv4 /32, then the single unicast address in the subnet is also technically the subnet broadcast address. If subnet is an IPv4 /31, then both addresses in that subnet are broadcast addresses. In either case, the “no subnet-specific broadcast” and “no address with a host part of all zeroes” rules don’t apply. Note further that this exception doesn’t apply to the unspecified address, which is never considered a unicast address regardless of what subnet it’s in.

§RFC Deep Dive
§IPv4 addresses ending in zeroes

In this section, we justify the rule that IPv4 addresses whose host bits are all 0 are not considered unicast addresses.

In earlier standards, an IPv4 address whose bits were all 0 after the network prefix (e.g., in the subnet were a form of “network-prefix-directed” broadcast addresses. Similarly, was considered a form of “limited broadcast address” (equivalent to These have since been deprecated (in the case of, it is now considered the “unspecified” address).

As evidence that this deprecation is official, consider RFC 1812 Section 5.3.5. In reference to these types of addresses, it states that “packets addressed to any of these addresses SHOULD be silently discarded [by routers]”. This not only deprecates them as broadcast addresses, but also as unicast addresses (after all, unicast addresses are not particularly useful if packets destined to them are discarded by routers).

§IPv4 /31 and /32 exceptions

In this section, we justify the exceptions that all addresses in IPv4 /31 and /32 subnets are considered unicast.

For /31 subnets, the case is easy. [RFC 3021 Section 2.1] states that both addresses in a /31 subnet “MUST be interpreted as host addresses.”

For /32, the case is a bit more vague. RFC 3021 makes no mention of /32 subnets. However, the same reasoning applies - if an exception is not made, then there do not exist any host addresses in a /32 subnet. RFC 4632 Section 3.1 also vaguely implies this interpretation by referring to addresses in /32 subnets as “host routes.”

Provided Methods§


fn is_loopback(&self) -> bool

Is this a loopback address?

is_loopback returns true if this address is a member of the LOOPBACK_SUBNET.

Object Safety§

This trait is not object safe.