Skip to main content

Ipv4Range

Struct Ipv4Range 

Source
pub struct Ipv4Range { /* private fields */ }
Available on crate feature net only.
Expand description

An IPv4 address range (start to end inclusive).

§Examples

use bare_types::net::Ipv4Range;
use core::net::Ipv4Addr;

// Parse range notation
let range: Ipv4Range = "192.168.1.0-192.168.1.255".parse().unwrap();

// Check if IP is in range
assert!(range.contains(&Ipv4Addr::new(192, 168, 1, 100)));

// Get number of addresses
assert_eq!(range.num_addresses(), 256);

Implementations§

Source§

impl Ipv4Range

Source

pub fn new(start: Ipv4Addr, end: Ipv4Addr) -> Result<Self, Ipv4RangeError>

Creates a new IPv4 range from start and end addresses.

§Errors

Returns Ipv4RangeError::InvalidRange if start > end.

§Examples
use bare_types::net::Ipv4Range;
use core::net::Ipv4Addr;

let range = Ipv4Range::new(
    Ipv4Addr::new(192, 168, 1, 0),
    Ipv4Addr::new(192, 168, 1, 255)
).unwrap();
Source

pub const fn start(&self) -> Ipv4Addr

Returns the start address.

§Examples
use bare_types::net::Ipv4Range;
use core::net::Ipv4Addr;

let range: Ipv4Range = "192.168.1.0-192.168.1.255".parse().unwrap();
assert_eq!(range.start(), Ipv4Addr::new(192, 168, 1, 0));
Source

pub const fn end(&self) -> Ipv4Addr

Returns the end address.

§Examples
use bare_types::net::Ipv4Range;
use core::net::Ipv4Addr;

let range: Ipv4Range = "192.168.1.0-192.168.1.255".parse().unwrap();
assert_eq!(range.end(), Ipv4Addr::new(192, 168, 1, 255));
Source

pub fn contains(&self, ip: &Ipv4Addr) -> bool

Returns true if this range contains the given IP address.

§Examples
use bare_types::net::Ipv4Range;
use core::net::Ipv4Addr;

let range: Ipv4Range = "192.168.1.0-192.168.1.255".parse().unwrap();
assert!(range.contains(&Ipv4Addr::new(192, 168, 1, 100)));
assert!(!range.contains(&Ipv4Addr::new(192, 168, 2, 1)));
Source

pub fn num_addresses(&self) -> u32

Returns the number of addresses in this range.

§Examples
use bare_types::net::Ipv4Range;

let range: Ipv4Range = "192.168.1.0-192.168.1.255".parse().unwrap();
assert_eq!(range.num_addresses(), 256);
Source

pub fn overlaps(&self, other: &Self) -> bool

Returns true if this range overlaps with another range.

§Examples
use bare_types::net::Ipv4Range;

let range1: Ipv4Range = "192.168.1.0-192.168.1.255".parse().unwrap();
let range2: Ipv4Range = "192.168.1.100-192.168.2.50".parse().unwrap();
assert!(range1.overlaps(&range2));
Source

pub fn contains_range(&self, other: &Self) -> bool

Returns true if this range fully contains another range.

§Examples
use bare_types::net::Ipv4Range;

let range1: Ipv4Range = "192.168.1.0-192.168.1.255".parse().unwrap();
let range2: Ipv4Range = "192.168.1.100-192.168.1.200".parse().unwrap();
assert!(range1.contains_range(&range2));
assert!(!range2.contains_range(&range1));
Source

pub fn is_adjacent_to(&self, other: &Self) -> bool

Returns true if this range is adjacent to another range.

Two ranges are adjacent if one ends exactly where the other starts, or they can be merged into a single continuous range.

§Examples
use bare_types::net::Ipv4Range;

let range1: Ipv4Range = "192.168.1.0-192.168.1.255".parse().unwrap();
let range2: Ipv4Range = "192.168.2.0-192.168.2.255".parse().unwrap();
assert!(range1.is_adjacent_to(&range2));
Source

pub fn merge(&self, other: &Self) -> Option<Self>

Returns a merged range if this range overlaps or is adjacent to another range.

§Errors

Returns None if the ranges cannot be merged (not overlapping or adjacent).

§Examples
use bare_types::net::Ipv4Range;

let range1: Ipv4Range = "192.168.1.0-192.168.1.255".parse().unwrap();
let range2: Ipv4Range = "192.168.2.0-192.168.2.255".parse().unwrap();
let merged = range1.merge(&range2).unwrap();
assert_eq!(merged.to_string(), "192.168.1.0-192.168.2.255");
Source

pub fn is_cidr_compatible(&self) -> bool

Returns true if this range can be represented as a CIDR block.

A range can be represented as a CIDR if:

  • It has a power of 2 number of addresses, AND
  • The start address is aligned to that power of 2
§Examples
use bare_types::net::Ipv4Range;

let range: Ipv4Range = "192.168.1.0-192.168.1.255".parse().unwrap();
assert!(range.is_cidr_compatible());

let non_cidr: Ipv4Range = "192.168.1.0-192.168.1.100".parse().unwrap();
assert!(!non_cidr.is_cidr_compatible());
Source

pub fn cidr_prefix_len(&self) -> Option<u8>

Returns the prefix length if this range can be represented as a CIDR block.

§Examples
use bare_types::net::Ipv4Range;

let range: Ipv4Range = "192.168.1.0-192.168.1.255".parse().unwrap();
assert_eq!(range.cidr_prefix_len(), Some(24));

Trait Implementations§

Source§

impl Clone for Ipv4Range

Source§

fn clone(&self) -> Ipv4Range

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Ipv4Range

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Ipv4Range

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Ipv4Range

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl FromStr for Ipv4Range

Source§

type Err = Ipv4RangeError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Ipv4Range

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

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

impl PartialEq for Ipv4Range

Source§

fn eq(&self, other: &Ipv4Range) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Ipv4Range

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Copy for Ipv4Range

Source§

impl Eq for Ipv4Range

Source§

impl StructuralPartialEq for Ipv4Range

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,