Trait net_types::ip::Ip

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

    const VERSION: IpVersion;
    const VERSION_MARKER: IpVersionMarker<Self>;
    const UNSPECIFIED_ADDRESS: Self::Addr;
    const LOOPBACK_ADDRESS: SpecifiedAddr<Self::Addr>;
    const LOOPBACK_SUBNET: Subnet<Self::Addr>;
    const MULTICAST_SUBNET: Subnet<Self::Addr>;
    const LINK_LOCAL_UNICAST_SUBNET: Subnet<Self::Addr>;
    const NAME: &'static str;
    const MINIMUM_LINK_MTU: Mtu;

    // Required method
    fn map_ip<In: GenericOverIp<Self, Type = In> + GenericOverIp<Ipv4> + GenericOverIp<Ipv6>, Out: GenericOverIp<Self, Type = Out> + GenericOverIp<Ipv4> + GenericOverIp<Ipv6>>(
        input: In,
        v4: impl FnOnce(<In as GenericOverIp<Ipv4>>::Type) -> <Out as GenericOverIp<Ipv4>>::Type,
        v6: impl FnOnce(<In as GenericOverIp<Ipv6>>::Type) -> <Out as GenericOverIp<Ipv6>>::Type
    ) -> Out;
}
Expand description

A trait for IP protocol versions.

Ip encapsulates the details of a version of the IP protocol. It includes a runtime representation of the protocol version (VERSION), the type of addresses for this version (Addr), and a number of constants which exist in both protocol versions. This trait 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.

Note that the implementors of this trait cannot be instantiated; they only exist at the type level.

Required Associated Types§

source

type Addr: IpAddress<Version = Self> + GenericOverIp<Self, Type = Self::Addr> + GenericOverIp<Ipv4, Type = Ipv4Addr> + GenericOverIp<Ipv6, Type = Ipv6Addr>

The address type for this IP version.

Ipv4Addr for IPv4 and Ipv6Addr for IPv6.

Required Associated Constants§

source

const VERSION: IpVersion

The IP version.

V4 for IPv4 and V6 for IPv6.

source

const VERSION_MARKER: IpVersionMarker<Self>

The zero-sized-type IP version marker.

source

const UNSPECIFIED_ADDRESS: Self::Addr

The unspecified address.

This is 0.0.0.0 for IPv4 and :: for IPv6.

source

const LOOPBACK_ADDRESS: SpecifiedAddr<Self::Addr>

The default loopback address.

When sending packets to a loopback interface, this address is used as the source address. It is an address in the LOOPBACK_SUBNET.

source

const LOOPBACK_SUBNET: Subnet<Self::Addr>

The subnet of loopback addresses.

Addresses in this subnet must not appear outside a host, and may only be used for loopback interfaces.

source

const MULTICAST_SUBNET: Subnet<Self::Addr>

The subnet of multicast addresses.

The subnet of link-local unicast addresses.

Note that some multicast addresses are also link-local. In IPv4, these are contained in the link-local multicast subnet. In IPv6, the link-local multicast addresses are not organized into a single subnet; instead, whether a multicast IPv6 address is link-local is a function of its scope.

source

const NAME: &'static str

“IPv4” or “IPv6”.

The minimum link MTU for this version.

Every internet link supporting this IP version must have a maximum transmission unit (MTU) of at least this many bytes. This MTU applies to the size of an IP packet, and does not include any extra bytes used by encapsulating packets (Ethernet frames, GRE packets, etc).

Required Methods§

source

fn map_ip<In: GenericOverIp<Self, Type = In> + GenericOverIp<Ipv4> + GenericOverIp<Ipv6>, Out: GenericOverIp<Self, Type = Out> + GenericOverIp<Ipv4> + GenericOverIp<Ipv6>>( input: In, v4: impl FnOnce(<In as GenericOverIp<Ipv4>>::Type) -> <Out as GenericOverIp<Ipv4>>::Type, v6: impl FnOnce(<In as GenericOverIp<Ipv6>>::Type) -> <Out as GenericOverIp<Ipv6>>::Type ) -> Out

Apply one of the given functions to the input and return the result.

This makes it possible to implement specialized behavior for IPv4 and IPv6 versions that is more versatile than matching on Ip::VERSION. With a match expression, all branches must produce a value of the same type. map_ip relaxes that restriction by instead requiring that inputs and outputs are GenericOverIp.

Using map_ip, you can write generic code with specialized implementations for different IP versions where some or all of the input and output arguments have a type parameter I: Ip. As an example, consider the following:

// Swaps the order of the addresses only if `I=Ipv4`.
fn swap_only_if_ipv4<I: Ip>(addrs: (I::Addr, I::Addr)) -> (I::Addr, I::Addr) {
   I::map_ip::<(I::Addr, I::Addr), (I::Addr, I::Addr)>(
       addrs,
       |(a, b): (Ipv4Addr, Ipv4Addr)| (b, a),
       |ab: (Ipv6Addr, Ipv6Addr)| ab
   )
}

Note that the input and output arguments both depend on the type parameter I, but the closures take an Ipv4Addr or Ipv6Addr.

Types that don’t implement GenericOverIp can be wrapped in IpInvariant, which implements GenericOverIp assuming the type inside doesn’t have any IP-related components.

Object Safety§

This trait is not object safe.

Implementors§