pub struct IpAddr(/* private fields */);Available on crate feature
net only.Expand description
An IP address (IPv4 or IPv6).
This type wraps the standard library’s IpAddr to provide type safety
and consistent API with other types in this crate. It uses the newtype
pattern with #[repr(transparent)] for zero-cost abstraction.
§Invariants
- The inner value is always a valid IP address
- IPv4 addresses are 4 octets (0-255 each)
- IPv6 addresses are 8 groups of 16 bits
§Examples
use bare_types::net::IpAddr;
use core::net::{Ipv4Addr, Ipv6Addr};
// Create an IPv4 address
let v4 = IpAddr::new(core::net::IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
assert!(v4.is_ipv4());
assert!(v4.is_loopback());
// Create an IPv6 address
let v6 = IpAddr::new(core::net::IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)));
assert!(v6.is_ipv6());
assert!(v6.is_loopback());
// Access the inner value
let inner: core::net::IpAddr = v4.into_inner();
assert_eq!(inner, core::net::IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
// Parse from string
let addr: IpAddr = "192.168.1.1".parse()?;
assert!(addr.is_ipv4());Implementations§
Source§impl IpAddr
impl IpAddr
Sourcepub const fn new(inner: IpAddr) -> Self
pub const fn new(inner: IpAddr) -> Self
Creates a new IP address from a standard library IpAddr.
§Examples
use bare_types::net::IpAddr;
use core::net::Ipv4Addr;
let addr = IpAddr::new(core::net::IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
assert!(addr.is_ipv4());Sourcepub const fn as_inner(&self) -> &IpAddr
pub const fn as_inner(&self) -> &IpAddr
Returns a reference to the underlying IpAddr.
§Examples
use bare_types::net::IpAddr;
use core::net::Ipv4Addr;
let addr = IpAddr::new(core::net::IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
let inner = addr.as_inner();
assert_eq!(inner, &core::net::IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));Sourcepub const fn into_inner(self) -> IpAddr
pub const fn into_inner(self) -> IpAddr
Consumes this IP address and returns the underlying IpAddr.
§Examples
use bare_types::net::IpAddr;
use core::net::Ipv4Addr;
let addr = IpAddr::new(core::net::IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
let inner: core::net::IpAddr = addr.into_inner();
assert_eq!(inner, core::net::IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));Sourcepub const fn is_ipv4(&self) -> bool
pub const fn is_ipv4(&self) -> bool
Returns true if this is an IPv4 address.
§Examples
use bare_types::net::IpAddr;
use core::net::{Ipv4Addr, Ipv6Addr};
let v4 = IpAddr::new(core::net::IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
let v6 = IpAddr::new(core::net::IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)));
assert!(v4.is_ipv4());
assert!(!v6.is_ipv4());Sourcepub const fn is_ipv6(&self) -> bool
pub const fn is_ipv6(&self) -> bool
Returns true if this is an IPv6 address.
§Examples
use bare_types::net::IpAddr;
use core::net::{Ipv4Addr, Ipv6Addr};
let v4 = IpAddr::new(core::net::IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
let v6 = IpAddr::new(core::net::IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)));
assert!(!v4.is_ipv6());
assert!(v6.is_ipv6());Sourcepub const fn is_loopback(&self) -> bool
pub const fn is_loopback(&self) -> bool
Returns true if this is a loopback address.
For IPv4, this is 127.0.0.1.
For IPv6, this is ::1.
§Examples
use bare_types::net::IpAddr;
use core::net::{Ipv4Addr, Ipv6Addr};
let v4_loopback = IpAddr::new(core::net::IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
let v6_loopback = IpAddr::new(core::net::IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)));
assert!(v4_loopback.is_loopback());
assert!(v6_loopback.is_loopback());Sourcepub const fn is_unspecified(&self) -> bool
pub const fn is_unspecified(&self) -> bool
Returns true if this is an unspecified address.
For IPv4, this is 0.0.0.0. For IPv6, this is ::.
§Examples
use bare_types::net::IpAddr;
use core::net::{Ipv4Addr, Ipv6Addr};
let v4_unspecified = IpAddr::new(core::net::IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)));
let v6_unspecified = IpAddr::new(core::net::IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)));
assert!(v4_unspecified.is_unspecified());
assert!(v6_unspecified.is_unspecified());Sourcepub const fn is_multicast(&self) -> bool
pub const fn is_multicast(&self) -> bool
Returns true if this is a multicast address.
§Examples
use bare_types::net::IpAddr;
use core::net::{Ipv4Addr, Ipv6Addr};
let v4_multicast = IpAddr::new(core::net::IpAddr::V4(Ipv4Addr::new(224, 0, 0, 1)));
let v6_multicast = IpAddr::new(core::net::IpAddr::V6(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0)));
assert!(v4_multicast.is_multicast());
assert!(v6_multicast.is_multicast());Trait Implementations§
Source§impl<'arbitrary> Arbitrary<'arbitrary> for IpAddr
impl<'arbitrary> Arbitrary<'arbitrary> for IpAddr
Source§fn arbitrary(u: &mut Unstructured<'arbitrary>) -> Result<Self>
fn arbitrary(u: &mut Unstructured<'arbitrary>) -> Result<Self>
Generate an arbitrary value of
Self from the given unstructured data. Read moreSource§fn arbitrary_take_rest(u: Unstructured<'arbitrary>) -> Result<Self>
fn arbitrary_take_rest(u: Unstructured<'arbitrary>) -> Result<Self>
Generate an arbitrary value of
Self from the entirety of the given
unstructured data. Read moreSource§fn size_hint(depth: usize) -> (usize, Option<usize>)
fn size_hint(depth: usize) -> (usize, Option<usize>)
Get a size hint for how many bytes out of an
Unstructured this type
needs to construct itself. Read moreSource§fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), MaxRecursionReached>
fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>
Get a size hint for how many bytes out of an
Unstructured this type
needs to construct itself. Read moreSource§impl<'de> Deserialize<'de> for IpAddr
impl<'de> Deserialize<'de> for IpAddr
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
Source§impl Ord for IpAddr
impl Ord for IpAddr
Source§impl PartialOrd for IpAddr
impl PartialOrd for IpAddr
impl Copy for IpAddr
impl Eq for IpAddr
impl StructuralPartialEq for IpAddr
Auto Trait Implementations§
impl Freeze for IpAddr
impl RefUnwindSafe for IpAddr
impl Send for IpAddr
impl Sync for IpAddr
impl Unpin for IpAddr
impl UnwindSafe for IpAddr
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