Module net_types::ip

source ·
Expand description

Internet Protocol (IP) types.

This module provides support for various types and traits relating to IPv4 and IPv6, including a number of mechanisms for abstracting over details which are shared between IPv4 and IPv6.

§Ip and IpAddress

The most important traits are Ip and IpAddress.

Ip represents a version of the IP protocol - either IPv4 or IPv6 - and is implemented by Ipv4 and Ipv6. These types exist only at the type level - they cannot be constructed at runtime. They provide a place to put constants and functionality which are not associated with a particular type, and they allow code to be written which is generic over the version of the IP protocol. For example:

struct Entry<A: IpAddress> {
    subnet: Subnet<A>,
    dest: Destination<A>,

enum Destination<A: IpAddress> {
    Local { device_id: usize },
    Remote { dst: A },

struct ForwardingTable<I: Ip> {
    entries: Vec<Entry<I::Addr>>,

See also IpVersionMarker.

The IpAddress trait is implemented by the concrete Ipv4Addr and Ipv6Addr types.

§Runtime types

Sometimes, it is not known at compile time which version of a given type - IPv4 or IPv6 - is present. For these cases, enums are provided with variants for both IPv4 and IPv6. These are IpAddr, SubnetEither, and AddrSubnetEither.

§Composite types

This modules also provides composite types such as Subnet and AddrSubnet.


  • An address and that address’s subnet.
  • Wrapper type that implements GenericOverIp with Type<I: Ip>=Self.
  • A wrapper structure to add an IP version marker to an IP-invariant type.
  • A zero-sized type that carries IP version information.
  • An IPv4 address.
  • An IPv6 address.
  • The maximum transmit unit, i.e., the maximum size of an entire IP packet one link can transmit.
  • An IP address was provided which is not a valid subnet mask (the address has set bits after the first unset bit).
  • An IP prefix length.
  • A prefix was provided which is longer than the number of bits in the address (32 for IPv4/128 for IPv6).
  • An IP subnet.



Derive Macros§

  • Implements a derive macro for [net_types::ip::GenericOverIp]. Requires that #[derive(GenericOverIp)] invocations explicitly specify which type parameter is the generic-over-ip one with the #[generic_over_ip] attribute, rather than inferring it from the bounds on the struct generics.