net_types::ip

Trait 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;

    // Provided methods
    fn map_ip_in<In: GenericOverIp<Self, Type = In> + GenericOverIp<Ipv4> + GenericOverIp<Ipv6>, Out>(
        input: In,
        v4: impl FnOnce(<In as GenericOverIp<Ipv4>>::Type) -> Out,
        v6: impl FnOnce(<In as GenericOverIp<Ipv6>>::Type) -> Out,
    ) -> Out { ... }
    fn map_ip_out<In, Out: GenericOverIp<Self, Type = Out> + GenericOverIp<Ipv4> + GenericOverIp<Ipv6>>(
        input: In,
        v4: impl FnOnce(In) -> <Out as GenericOverIp<Ipv4>>::Type,
        v6: impl FnOnce(In) -> <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 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 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 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.

Provided Methods§

Source

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

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

This is similar to map_ip, except only the input type is required to be GenericOverIp, while the output type is invariant. This allows callers to more conveniently write this use case without having to wrap the result in a type like IpInvariant.

Source

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

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

This is similar to map_ip, except only the output type is required to be GenericOverIp, while the input type is invariant. This allows callers to more conveniently write this use case without having to wrap the input in a type like IpInvariant.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§