pub struct Ipv6Range { /* private fields */ }Available on crate feature
net only.Expand description
An IPv6 address range (start to end inclusive).
§Examples
use bare_types::net::Ipv6Range;
use core::net::Ipv6Addr;
// Parse range notation
let range: Ipv6Range = "2001:db8::1-2001:db8::ffff".parse().unwrap();
// Check if IP is in range
assert!(range.contains(&Ipv6Addr::new(0x2001, 0x0db8, 0, 0, 0, 0, 0, 0x100)));Implementations§
Source§impl Ipv6Range
impl Ipv6Range
Sourcepub fn new(start: Ipv6Addr, end: Ipv6Addr) -> Result<Self, Ipv6RangeError>
pub fn new(start: Ipv6Addr, end: Ipv6Addr) -> Result<Self, Ipv6RangeError>
Creates a new IPv6 range from start and end addresses.
§Errors
Returns Ipv6RangeError::InvalidRange if start > end.
§Examples
use bare_types::net::Ipv6Range;
use core::net::Ipv6Addr;
let range = Ipv6Range::new(
Ipv6Addr::new(0x2001, 0x0db8, 0, 0, 0, 0, 0, 1),
Ipv6Addr::new(0x2001, 0x0db8, 0, 0, 0, 0, 0, 0xffff),
).unwrap();Sourcepub const fn start(&self) -> Ipv6Addr
pub const fn start(&self) -> Ipv6Addr
Returns the start address.
§Examples
use bare_types::net::Ipv6Range;
use core::net::Ipv6Addr;
let range: Ipv6Range = "2001:db8::1-2001:db8::ffff".parse().unwrap();
assert_eq!(range.start(), Ipv6Addr::new(0x2001, 0x0db8, 0, 0, 0, 0, 0, 1));Sourcepub const fn end(&self) -> Ipv6Addr
pub const fn end(&self) -> Ipv6Addr
Returns the end address.
§Examples
use bare_types::net::Ipv6Range;
use core::net::Ipv6Addr;
let range: Ipv6Range = "2001:db8::1-2001:db8::ffff".parse().unwrap();
assert_eq!(range.end(), Ipv6Addr::new(0x2001, 0x0db8, 0, 0, 0, 0, 0, 0xffff));Sourcepub fn contains(&self, ip: &Ipv6Addr) -> bool
pub fn contains(&self, ip: &Ipv6Addr) -> bool
Returns true if this range contains the given IP address.
§Examples
use bare_types::net::Ipv6Range;
use core::net::Ipv6Addr;
let range: Ipv6Range = "2001:db8::1-2001:db8::ffff".parse().unwrap();
assert!(range.contains(&Ipv6Addr::new(0x2001, 0x0db8, 0, 0, 0, 0, 0, 0x100)));
assert!(!range.contains(&Ipv6Addr::new(0x2001, 0x0db9, 0, 0, 0, 0, 0, 1)));Sourcepub fn num_addresses(&self) -> u128
pub fn num_addresses(&self) -> u128
Returns the number of addresses in this range.
§Examples
use bare_types::net::Ipv6Range;
let range: Ipv6Range = "2001:db8::1-2001:db8::ffff".parse().unwrap();
assert_eq!(range.num_addresses(), 0xffff);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::Ipv6Range;
let range1: Ipv6Range = "2001:db8::1-2001:db8::ffff".parse().unwrap();
let range2: Ipv6Range = "2001:db8::100-2001:db9::1".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::Ipv6Range;
let range1: Ipv6Range = "2001:db8::1-2001:db8::ffff".parse().unwrap();
let range2: Ipv6Range = "2001:db8::100-2001:db8::200".parse().unwrap();
assert!(range1.contains_range(&range2));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.
§Examples
use bare_types::net::Ipv6Range;
use core::net::Ipv6Addr;
let range1 = Ipv6Range::new(
Ipv6Addr::new(0x2001, 0x0db8, 0, 0, 0, 0, 0, 0),
Ipv6Addr::new(0x2001, 0x0db8, 0, 0, 0, 0, 0, 10),
).unwrap();
let range2 = Ipv6Range::new(
Ipv6Addr::new(0x2001, 0x0db8, 0, 0, 0, 0, 0, 11),
Ipv6Addr::new(0x2001, 0x0db8, 0, 0, 0, 0, 0, 20),
).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.
§Examples
use bare_types::net::Ipv6Range;
use core::net::Ipv6Addr;
let range1 = Ipv6Range::new(
Ipv6Addr::new(0x2001, 0x0db8, 0, 0, 0, 0, 0, 0),
Ipv6Addr::new(0x2001, 0x0db8, 0, 0, 0, 0, 0, 10),
).unwrap();
let range2 = Ipv6Range::new(
Ipv6Addr::new(0x2001, 0x0db8, 0, 0, 0, 0, 0, 11),
Ipv6Addr::new(0x2001, 0x0db8, 0, 0, 0, 0, 0, 20),
).unwrap();
let merged = range1.merge(&range2).unwrap();
assert_eq!(merged.start(), Ipv6Addr::new(0x2001, 0x0db8, 0, 0, 0, 0, 0, 0));
assert_eq!(merged.end(), Ipv6Addr::new(0x2001, 0x0db8, 0, 0, 0, 0, 0, 20));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.
§Examples
use bare_types::net::Ipv6Range;
use core::net::Ipv6Addr;
// /112 prefix: 65536 addresses (2^16)
let range = Ipv6Range::new(
Ipv6Addr::new(0x2001, 0x0db8, 0, 0, 0, 0, 0, 0),
Ipv6Addr::new(0x2001, 0x0db8, 0, 0, 0, 0, 0, 0xffff),
).unwrap();
assert!(range.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::Ipv6Range;
use core::net::Ipv6Addr;
// /112 prefix: 65536 addresses (2^16)
let range = Ipv6Range::new(
Ipv6Addr::new(0x2001, 0x0db8, 0, 0, 0, 0, 0, 0),
Ipv6Addr::new(0x2001, 0x0db8, 0, 0, 0, 0, 0, 0xffff),
).unwrap();
assert_eq!(range.cidr_prefix_len(), Some(112));Trait Implementations§
Source§impl<'de> Deserialize<'de> for Ipv6Range
impl<'de> Deserialize<'de> for Ipv6Range
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
impl Copy for Ipv6Range
impl Eq for Ipv6Range
impl StructuralPartialEq for Ipv6Range
Auto Trait Implementations§
impl Freeze for Ipv6Range
impl RefUnwindSafe for Ipv6Range
impl Send for Ipv6Range
impl Sync for Ipv6Range
impl Unpin for Ipv6Range
impl UnsafeUnpin for Ipv6Range
impl UnwindSafe for Ipv6Range
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more