Type-safe, validated DNS domain names, hosts, and host addresses for Rust.
Overview
hostaddr provides type-safe, validated types for working with network host identifiers:
Domain<S>-- A validated DNS domain name, with IDNA/punycode and percent-encoding supportHost<S>-- Either a domain name or an IP address (v4/v6)HostAddr<S>-- A host with an optional port number (e.g.example.com:8080,[::1]:443)
All types are generic over their storage backend S, allowing you to choose the representation that best fits your use case -- from zero-alloc stack buffers (Buffer) to shared smart pointers (Arc<str>).
Features
no_stdandno-alloccompatible: Use theBuffertype for stack-allocated domains without any heap allocation- Generic storage: Works with
String,Arc<str>,Box<str>,Vec<u8>,SmolStr,Bytes, and more - IDNA/Punycode support: Automatic conversion of international domain names (e.g.
测试.中国to punycode) - Type-safe validation: Domain names are validated at construction time per RFC 1035 rules
- Percent-encoding: Transparent decoding of percent-encoded domains
- IPv4/IPv6: Full support for IP addresses, including proper
[::1]:portbracket syntax - Semantic IP/socket wrappers:
LoopbackIpAddr/LoopbackAddr,PrivateIpAddr/PrivateAddr, and other validated address classes - Transport taxonomy:
Addr,IpcAddr, andLocalAddrmodel host, IPC, and local-only addresses - Serde: Optional serialization/deserialization support
- Fuzzing: Built-in
arbitraryandquickcheckgenerators for property-based testing
Installation
[]
= "0.2"
Feature Flags
| Feature | Description |
|---|---|
std (default) |
Standard library support, enables IDNA and percent-decoding |
vsock (default) |
Linux VM socket address support through VsockAddr and IpcAddr::Vsock |
alloc |
Allocation support without std |
serde |
Serialize/deserialize support |
arbitrary |
Fuzzing with arbitrary crate |
cheap-clone |
CheapClone trait for smart pointer types |
smol_str |
SmolStr storage support |
bytes |
Bytes storage support |
triomphe |
triomphe::Arc storage support |
tinyvec |
TinyVec storage support |
smallvec |
SmallVec storage support |
Quick Start
use HostAddr;
// Domain with port
let addr: = "example.com:8080".parse.unwrap;
assert!;
assert_eq!;
// IPv4 with port
let addr: = "127.0.0.1:3000".parse.unwrap;
assert!;
// IPv6 with port (bracket syntax)
let addr: = "[::1]:443".parse.unwrap;
assert!;
assert_eq!;
// International domain names (auto punycode)
let addr: = "测试.中国:80".parse.unwrap;
Address Taxonomy
Addr separates Internet host addresses from IPC transports while preserving
generic storage for each address family. LocalAddr::Loopback is validated as a
loopback IP socket address, while LocalAddr::Ipc groups non-IP IPC transport
addresses without treating every IPC endpoint as a same-machine trust boundary.
Semantic address classes are available in IP-only and socket-with-port forms.
The *IpAddr types wrap core::net::IpAddr; the matching *Addr types wrap
core::net::SocketAddr and validate the socket IP component. Common classes
include Loopback, Private, LinkLocal, Documentation, Benchmark,
Shared, Multicast, Unspecified, and Broadcast.
The transparent IPC wrappers support borrowed DST storage such as
&UnixAddr<Path> on Unix and &AbstractAddr<[u8]> on Linux. Windows named-pipe
addresses are exposed through NamedPipeAddr; Linux VsockAddr support is
available through the default-enabled vsock feature.
use ;
let host = from;
let addr: = host.into;
let private = try_from.unwrap;
let local_ip = try_from.unwrap;
let local: = local_ip.into;
Examples
Working with Domains
use Domain;
// Validated at construction -- invalid names are rejected
assert!;
assert!;
// International domain names are converted to punycode
let domain: = "测试.中国".parse.unwrap;
assert_eq!;
// FQDN support
let domain: = "example.com.".parse.unwrap;
assert!;
// ASCII-only fast path (no punycode or percent-decoding)
let domain = try_from_ascii_str.unwrap;
Choosing a Storage Type
use HostAddr;
use Arc;
// Heap-allocated String
let addr: = "example.com:8080".parse.unwrap;
// Shared ownership with Arc<str>
let addr: = "example.com:8080".parse.unwrap;
// Byte storage
let addr: = "example.com".parse.unwrap;
Stack-Allocated Domains (no_std / no-alloc)
use ;
// No heap allocation -- 255-byte stack buffer
let domain: = try_from.unwrap;
let addr: = try_from.unwrap;
Manipulating Addresses
use HostAddr;
let mut addr: = "example.com".parse.unwrap;
addr.set_port;
assert_eq!;
// Builder-style with default port
let addr = "example.com"..unwrap
.with_default_port;
assert_eq!;
Verification Functions
use ;
assert!;
assert!;
assert!;
assert!;
License
hostaddr is under the terms of both the MIT license and the
Apache License (Version 2.0).
See LICENSE-APACHE, LICENSE-MIT for details.
Copyright (c) 2026 Al Liu.