Enum net_adds::IpAddrRange[][src]

pub enum IpAddrRange {
    V4(Ipv4AddrRange),
    V6(Ipv6AddrRange),
}
Expand description

An IP address range, either IPv4 or IPv6.

This enum can contain either an Ipv4AddrRange or an Ipv6AddrRange, see their respective documentation for more details.

The size of an IpAddrRange struct may vary depending on the target operating system.

Textual representation

IpAddrRange provides a FromStr implementation. The two parts are divided by -. For IPv4, the two parts must contains an IPv4. For IPv6, the two parts must contains an IPv6.

Examples

use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

use net_adds::{IpAddrRange, Ipv4AddrRange, Ipv6AddrRange};

let range = IpAddrRange::V4(Ipv4AddrRange::new(Ipv4Addr::new(192, 168, 0, 0), Ipv4Addr::new(192, 168, 0, 255)));

assert_eq!(Ok(range), "192.168.0.0-192.168.0.255".parse());

let range = IpAddrRange::V6(Ipv6AddrRange::new(Ipv6Addr::from(0x1), Ipv6Addr::from(0xFFFF)));

assert_eq!(Ok(range), "::1-::ffff".parse());

Variants

V4(Ipv4AddrRange)
V6(Ipv6AddrRange)

Implementations

Returns the first ip.

Returns the last ip.

Returns all ips included in the range.

Examples:

use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

use net_adds::{IpAddrRange, Ipv4AddrRange, Ipv6AddrRange};

let a = Ipv4Addr::new(0, 0, 0, 0);
let b = Ipv4Addr::new(0, 0, 0, 1);
let c = Ipv4Addr::new(0, 0, 0, 2);

assert_eq!(IpAddrRange::V4(Ipv4AddrRange::new(a, c)).all(), vec![
    IpAddr::V4(a),
    IpAddr::V4(b),
    IpAddr::V4(c)
]);

let a = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
let b = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
let c = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 2);

assert_eq!(IpAddrRange::V6(Ipv6AddrRange::new(c, a)).all(), vec![
    IpAddr::V6(c),
    IpAddr::V6(b),
    IpAddr::V6(a)
]);

Returns the number of ip’s included in the range.

Examples:

use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

use net_adds::{IpAddrRange, Ipv4AddrRange, Ipv6AddrRange};

let range = IpAddrRange::V4(Ipv4AddrRange::new(Ipv4Addr::from(0), Ipv4Addr::from(0xA)));
assert_eq!(range.size(), 11);

let range = IpAddrRange::V6(Ipv6AddrRange::new(Ipv6Addr::from(0), Ipv6Addr::from(0xA)));
assert_eq!(range.size(), 11);

Returns true if the ip argument is included in the range, else returns false.

Panic if IPv4 and IPv6 are mixed.

Examples:

use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

use net_adds::{IpAddrRange, Ipv4AddrRange, Ipv6AddrRange};

let range = IpAddrRange::V4(Ipv4AddrRange::new(Ipv4Addr::from(0), Ipv4Addr::from(0xA)));

assert!(range.has(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))));
assert!(range.has(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 5))));
assert!(range.has(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 10))));

assert!(!range.has(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 11))));

let range = IpAddrRange::V6(Ipv6AddrRange::new(Ipv6Addr::from(0), Ipv6Addr::from(0xA)));

assert!(range.has(IpAddr::V6(Ipv6Addr::from(0x1))));
assert!(range.has(IpAddr::V6(Ipv6Addr::from(0x5))));
assert!(range.has(IpAddr::V6(Ipv6Addr::from(0xA))));

assert!(!range.has(IpAddr::V6(Ipv6Addr::from(0xFFFF))));

Returns true if the range contains IPv4, else return false.

Examples:

use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

use net_adds::{IpAddrRange, Ipv4AddrRange, Ipv6AddrRange};

let a = Ipv4Addr::new(0, 0, 0, 0);
let b = Ipv4Addr::new(0, 0, 0, 2);

assert_eq!(IpAddrRange::V4(Ipv4AddrRange::new(a, b)).is_ipv4(), true);

let a = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
let b = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 2);

assert_eq!(IpAddrRange::V6(Ipv6AddrRange::new(a, b)).is_ipv4(), false);

Returns true if the range contains IPv6, else return false.

Examples:

use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

use net_adds::{IpAddrRange, Ipv4AddrRange, Ipv6AddrRange};

let a = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
let b = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 2);

assert_eq!(IpAddrRange::V6(Ipv6AddrRange::new(a, b)).is_ipv6(), true);

let a = Ipv4Addr::new(0, 0, 0, 0);
let b = Ipv4Addr::new(0, 0, 0, 2);

assert_eq!(IpAddrRange::V4(Ipv4AddrRange::new(a, b)).is_ipv6(), false);

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Formats the value using the given formatter. Read more

Create an IpAddrRange::V4 from an Ipv4AddrRange.

Examples:

use std::net::Ipv4Addr;

use net_adds::{IpAddrRange, Ipv4AddrRange};

let a = Ipv4Addr::new(0, 0, 0, 0);
let b = Ipv4Addr::new(0, 0, 0, 10);

assert_eq!(IpAddrRange::from(Ipv4AddrRange::new(a, b)), IpAddrRange::V4(Ipv4AddrRange::new(a, b)));

Create an IpAddrRange::V6 from an Ipv6AddrRange.

Examples:

use std::net::Ipv6Addr;

use net_adds::{IpAddrRange, Ipv6AddrRange};

let a = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
let b = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0xa);
let range = Ipv6AddrRange::new(a, b);

assert_eq!(IpAddrRange::from(range), IpAddrRange::V6(range));

Parse a string as IpAddrRange.

If the string representation is not valid return an NetAddsError::RangeAddrParse(RangeAddrParseError).

Examples:

use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

use net_adds::{IpAddrRange, Ipv4AddrRange, Ipv6AddrRange};

let a = Ipv4Addr::new(192, 168, 0, 0);
let b = Ipv4Addr::new(192, 168, 0, 255);
let range = IpAddrRange::V4(Ipv4AddrRange::new(a, b));

assert_eq!("192.168.0.0-192.168.0.255".parse(), Ok(range));

let a = Ipv6Addr::new(0xFFFF, 0, 0, 0, 0, 0, 0, 0xFF);
let b = Ipv6Addr::new(0xFFFF, 0, 0, 0, 0, 0, 0, 0xFFFF);
let range = IpAddrRange::V6(Ipv6AddrRange::new(a, b));

assert_eq!("ffff::ff-ffff::ffff".parse(), Ok(range));

The associated error which can be returned from parsing.

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

Create a IpAddr iterator.

Examples:

use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

use net_adds::{IpAddrRange, Ipv4AddrRange, Ipv6AddrRange};

let a = Ipv4Addr::new(0, 0, 0, 0);
let b = Ipv4Addr::new(0, 0, 0, 1);
let mut iter = IpAddrRange::V4(Ipv4AddrRange::new(a, b)).into_iter();

assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))));
assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 1))));
assert_eq!(iter.next(), None);

let a = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
let b = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
let mut iter = IpAddrRange::V6(Ipv6AddrRange::new(a, b)).into_iter();

assert_eq!(iter.next(), Some(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0))));
assert_eq!(iter.next(), Some(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1))));
assert_eq!(iter.next(), None);

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Create a smart IpAddr iterator.

Examples:

use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

use net_adds::{IpAddrRange, Ipv4AddrRange, Ipv6AddrRange, IntoSmartIterator};

let a = Ipv4Addr::new(0, 0, 0, 0);
let b = Ipv4Addr::new(0, 0, 0, 1);
let mut iter = IpAddrRange::V4(Ipv4AddrRange::new(a, b)).into_smart_iter();

assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))));
assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 1))));
assert_eq!(iter.next(), None);

let a = Ipv6Addr::from(0x0);
let b = Ipv6Addr::from(0x1);
let mut iter = IpAddrRange::V6(Ipv6AddrRange::new(a, b)).into_smart_iter();

assert_eq!(iter.next(), Some(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0))));
assert_eq!(iter.next(), Some(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1))));
assert_eq!(iter.next(), None);

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.