mod address
srcIP address and socket address types.
Both IpAddr and SocketAddr are copyable value types β they hold
no OS resources, so copying is safe and cheap.
IP address parsing uses the OS inet_pton(3) function for correctness
on both IPv4 and IPv6; formatting uses inet_ntop(3).
Structs
struct IpAddr
struct IpAddr
An IP address: either IPv4 or IPv6.
The address is stored as a validated string produced by inet_ntop
after successful inet_pton parsing. This guarantees all instances
hold a canonical, valid representation.
Fields:
_addr: Canonical string form (e.g. "127.0.0.1" or "::1").
_is_v6: True for IPv6, False for IPv4.
Example:
var lo = IpAddr.parse("127.0.0.1")
print(lo) # 127.0.0.1
print(lo.is_loopback()) # True
Methods
fn parse static
parse(s: String) -> Self
Parse and validate an IP address string.
Accepts dotted-decimal IPv4 ("192.168.1.1") and colon-separated
IPv6 ("::1", "2001:db8::1"). Uses inet_pton for
correctness, then inet_ntop to obtain the canonical form.
Example:
var v4 = IpAddr.parse("192.168.0.1")
var v6 = IpAddr.parse("::1")
Args
The address string to parse.
Returns
A validated IpAddr.
β οΈ Raises
AddressParseError: If s is not a valid IPv4 or IPv6 address.
fn localhost static
localhost() -> Self
Return the IPv4 loopback address ``127.0.0.1``.
Returns
IpAddr for 127.0.0.1.
fn localhost_v6 static
localhost_v6() -> Self
Return the IPv6 loopback address ``::1``.
Returns
IpAddr for ::1.
fn unspecified static
unspecified() -> Self
Return the IPv4 wildcard address ``0.0.0.0`` (all interfaces).
Returns
IpAddr for 0.0.0.0.
fn unspecified_v6 static
unspecified_v6() -> Self
Return the IPv6 wildcard address ``::`` (all interfaces).
Returns
IpAddr for "::"
fn write_to
write_to[W: Writer](self, mut writer: W)
Write the canonical address string to ``writer``.
Type Parameters
Args
Destination writer.
struct SocketAddr
struct SocketAddr
A socket address: an IP address combined with a port number.
Fields: ip: The IP address component. port: The port number (0β65535).
Example:
var addr = SocketAddr(IpAddr.localhost(), 8080)
print(addr) # 127.0.0.1:8080
var v6 = SocketAddr(IpAddr.localhost_v6(), 443)
print(v6) # [::1]:443
Methods
fn parse static
parse(s: String) -> Self
Parse a ``"host:port"`` or ``"[ipv6]:port"`` string.
Example:
var a = SocketAddr.parse("127.0.0.1:9000")
var b = SocketAddr.parse("[::1]:9000")
Args
Address string in one of these forms:
"1.2.3.4:8080"for IPv4."[::1]:443"for IPv6.
Returns
A SocketAddr.
β οΈ Raises
AddressParseError: If the string cannot be parsed.
fn write_to
write_to[W: Writer](self, mut writer: W)
Write ``"ip:port"`` or ``"[ip]:port"`` to ``writer``.
Type Parameters
Args
Destination writer.