hostaddr-0.2.0 has been yanked.
Extended types (Domain, Host, HostAddr) for working with net in Rust.
English | 简体中文
Features
no_stdandno-alloccompatible: Use theBuffertype for stack-allocated domains- Generic storage: Works with
String,Arc<str>,Box<str>,Vec<u8>, and many more - IDNA/Punycode support: Automatic conversion of international domain names (requires
allocorstd) - Type-safe parsing: Compile-time guarantees for valid domains and hosts
- Percent-encoding: Automatic decoding of percent-encoded domains
Installation
[]
= "0.2"
Cargo Features
std(default): Enables standard library supportalloc: Enables allocation support withoutstdserde: Serialize/deserialize supportarbitrary: Support for fuzzing witharbitrarycheap-clone: ImplementCheapClonefor smart pointer types- String types:
smol_str,triomphe,bytes,tinyvec,smallvec
API Overview
This library provides three main types:
Domain<S>: A validated DNS domain name (e.g.,example.com,測試.中國)Host<S>: Either a domain name or an IP addressHostAddr<S>: A host with an optional port number
Examples
Basic Usage
use HostAddr;
// Parse domain with String storage
let addr: = "example.com".parse.unwrap;
assert!;
assert_eq!;
// Parse domain with port
let addr: = "example.com:8080".parse.unwrap;
assert_eq!;
// Parse IP address with port
let addr: = "127.0.0.1:8080".parse.unwrap;
assert!;
assert_eq!;
// Parse IPv6 address with port
let addr: = "[::1]:8080".parse.unwrap;
assert!;
Using Different Storage Types
use HostAddr;
use Arc;
// Using Arc<str> for shared ownership
let addr: = "example.com:8080".parse.unwrap;
// Using Box<str> for heap allocation
let addr: = "example.com:8080".parse.unwrap;
// Using Vec<u8> for byte storage
let addr: = "example.com".parse.unwrap;
Using Buffer for no_std/no-alloc
use ;
// Stack-allocated domain (no heap allocation)
let domain: = try_from.unwrap;
// Works in no_std environments
let addr: = try_from.unwrap;
Working with Domains
use Domain;
// International domain names are automatically converted to punycode
let domain: = "測試.中國".parse.unwrap;
assert_eq!;
// Parse ASCII-only domains without punycode conversion
let domain = try_from_ascii_str.unwrap;
assert_eq!;
// Fully-qualified domain names (FQDN) are supported
let domain: = "example.com.".parse.unwrap;
assert_eq!;
Working with Hosts
use Host;
use IpAddr;
// Parse a host (domain or IP)
let host: = "example.com".parse.unwrap;
assert!;
let host: = "127.0.0.1".parse.unwrap;
assert!;
// Convert between types
let ip: IpAddr = "127.0.0.1".parse.unwrap;
let host: = from;
Manipulating HostAddr
use HostAddr;
// Create and modify
let mut addr: = "example.com".parse.unwrap;
addr.set_port;
assert_eq!;
// Builder-style API
let addr = from_domain
.with_port;
assert_eq!;
// Check host type
if addr.is_domain
Percent Encoding
use Domain;
// Percent-encoded domains are automatically decoded
let domain: = "example%2Ecom".parse.unwrap;
assert_eq!;
// Works with international domains too
let domain: = "測試%2E中國".parse.unwrap;
assert_eq!;
Verification Functions
use ;
// Verify any domain (including international)
assert!;
assert!;
// Verify ASCII-only domains
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.