Trait ipnet::IpSub

source ·
pub trait IpSub<RHS = Self> {
    type Output;

    // Required method
    fn saturating_sub(self, rhs: RHS) -> Self::Output;
Expand description

Provides a saturating_sub() method for Ipv4Addr and Ipv6Addr.

Subtracting an integer from an IP address returns the modified IP address. A u32 may be subtracted from an IPv4 address and a u128 may be subtracted from an IPv6 address.

Subtracting an IP address from another IP address of the same type returns an integer of the appropriate width. A u32 for IPv4 and a u128 for IPv6. Subtracting IP addresses is useful for getting the range between two IP addresses.


use std::net::{Ipv4Addr, Ipv6Addr};
use ipnet::IpSub;

let min: Ipv4Addr = "".parse().unwrap();
let ip1: Ipv4Addr = "".parse().unwrap();
let ip2: Ipv4Addr = "".parse().unwrap();

assert_eq!(min.saturating_sub(ip1), 0);
assert_eq!(ip2.saturating_sub(ip1), 95);
assert_eq!(min.saturating_sub(5), min);
assert_eq!(ip2.saturating_sub(95), ip1);
let min: Ipv6Addr = "::".parse().unwrap();
let ip1: Ipv6Addr = "fd00::5".parse().unwrap();
let ip2: Ipv6Addr = "fd00::64".parse().unwrap();

assert_eq!(min.saturating_sub(ip1), 0);
assert_eq!(ip2.saturating_sub(ip1), 95);
assert_eq!(min.saturating_sub(5u128), min);
assert_eq!(ip2.saturating_sub(95u128), ip1);

Required Associated Types§

Required Methods§


fn saturating_sub(self, rhs: RHS) -> Self::Output

Implementations on Foreign Types§


impl IpSub for Ipv4Addr


impl IpSub for Ipv6Addr


impl IpSub<u32> for Ipv4Addr


impl IpSub<u128> for Ipv6Addr