pub struct Ipv4Range { /* private fields */ }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
impl Ipv4Range
Sourcepub const fn start(&self) -> Ipv4Addr
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));Sourcepub const fn end(&self) -> Ipv4Addr
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));Sourcepub fn contains(&self, ip: &Ipv4Addr) -> bool
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)));Sourcepub fn num_addresses(&self) -> u32
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);Sourcepub fn overlaps(&self, other: &Self) -> bool
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));Sourcepub fn contains_range(&self, other: &Self) -> bool
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));Sourcepub fn is_adjacent_to(&self, other: &Self) -> bool
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));Sourcepub fn merge(&self, other: &Self) -> Option<Self>
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");Sourcepub fn is_cidr_compatible(&self) -> bool
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());Sourcepub fn cidr_prefix_len(&self) -> Option<u8>
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));