Skip to main content

IpSub

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

§Examples

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

let min: Ipv4Addr = "0.0.0.0".parse().unwrap();
let ip1: Ipv4Addr = "192.168.1.5".parse().unwrap();
let ip2: Ipv4Addr = "192.168.1.100".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§

Source

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

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl IpSub for Ipv4Addr

Source§

impl IpSub for Ipv6Addr

Source§

impl IpSub<u32> for Ipv4Addr

Source§

impl IpSub<u128> for Ipv6Addr

Implementors§