Type-safe, validated DNS domain names, hosts, and host addresses for Rust.
English | 简体中文
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 - 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 |
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;
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) 2025 Al Liu.