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 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
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));
type Err = NetAddsError
type Err = NetAddsError
The associated error which can be returned from parsing.
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);
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);
type IntoSmartIter = IpAddrSmartIterator
type IntoSmartIter = IpAddrSmartIterator
Which kind of iterator are we turning this into?
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
Auto Trait Implementations
impl RefUnwindSafe for IpAddrRange
impl Send for IpAddrRange
impl Sync for IpAddrRange
impl Unpin for IpAddrRange
impl UnwindSafe for IpAddrRange
Blanket Implementations
Mutably borrows from an owned value. Read more