Stdlibnet::addr
ip
import std::net::addr::ip; · source
IpV4Addr
type struct IpV4Addr {
a: u8;
b: u8;
c: u8;
d: u8;
static new(a: u8, b: u8, c: u8, d: u8) -> IpV4Addr;
static unspecified() -> IpV4Addr;
static localhost() -> IpV4Addr;
static broadcast() -> IpV4Addr;
octet(&this, index: u64) -> u8;
is_loopback(&this) -> boolean;
is_unspecified(&this) -> boolean;
to_u32(&this) -> u32;
static parse(source: Str) -> Result<IpV4Addr, ConversionError>;
static parse_opt(source: Str) -> Option<IpV4Addr>;
}
new(a, b, c, d) builds an address from its octets; localhost(), unspecified(), and broadcast() are the named ones. parse reads dotted-quad text as a Result, parse_opt as an Option. to_u32 gives the address in host byte order, which is what the raw socket API wants after htonl.
IpV6Addr
type struct IpV6Addr {
segments: u8[16];
static unspecified() -> IpV6Addr;
static localhost() -> IpV6Addr;
static from_buf(p: u8*) -> IpV6Addr;
group_at(&this, group: u64) -> u16;
is_loopback(&this) -> boolean;
is_unspecified(&this) -> boolean;
static parse(source: Str) -> Result<IpV6Addr, ConversionError>;
static parse_opt(source: Str) -> Option<IpV6Addr>;
}
The same shape over sixteen bytes, with group_at reading one 16-bit group. parse reads the canonical text forms: full a:b:c:d:e:f:g:h, one ::-compressed run, and a trailing embedded IPv4 (::ffff:1.2.3.4).
IpAddr
type enum IpAddr {
V4(IpV4Addr);
V6(IpV6Addr);
}
implement enum IpAddr {
is_ipv4(&this) -> boolean;
is_ipv6(&this) -> boolean;
static parse(source: Str) -> Result<IpAddr, ConversionError>;
static parse_opt(source: Str) -> Option<IpAddr>;
}
The enum over the two, with the same parse pair: text containing a : goes to the v6 parser, anything else to v4.
import std::net::addr::ip;
const local: IpAddr = IpAddr::V4(IpV4Addr::localhost());
match (IpAddr::parse(Str::new("2001:db8::1"))) {
Result::Ok(addr) => { /* addr.is_ipv6() */ }
Result::Err(e) => { /* not an address */ }
}
All three are POD — copied by value, nothing to drop.