Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
domain-key
High-performance, type-safe, domain-driven key system for Rust applications
Never mix up keys from different domains again!
What is domain-key?
domain-key brings Domain-Driven Design principles to key management in Rust. It provides compile-time guarantees that keys from different business domains cannot be accidentally mixed or compared, while delivering exceptional performance through advanced optimizations.
use ;
// Define your business domains
;
;
// Create domain-specific key types
type UserKey = ;
type OrderKey = ;
Key Features
- Type Safety: Different key types cannot be mixed at compile time
- High Performance: Up to 75% performance improvements through advanced optimizations
- Domain Agnostic: No built-in assumptions about specific domains
- Memory Efficient: Smart string handling with stack allocation for short keys
- DoS Resistant: Optional protection against HashDoS attacks
- Extensible: Easy to add new domains and validation rules
- Zero-Cost Abstractions: No runtime overhead for type separation
- Cross-Platform: Works on all major platforms including WebAssembly
Quick Start
Add to your Cargo.toml:
[]
= "0.1"
# For maximum performance
= { = "0.1", = ["fast"] }
# For security-critical applications
= { = "0.1", = ["secure"] }
Define a domain and create keys:
use ;
// 1. Define your domain
;
// 2. Create a type alias
type UserKey = ;
// 3. Use it!
let user_key = new?;
let composed_key = from_parts?;
println!;
println!; // O(1) with optimizations
println!;
# Ok::
// Or use macros for less boilerplate:
use ;
define_domain!;
key_type!;
Identifier Types
domain-key provides three typed identifier wrappers:
| Type | Storage | Use case |
|---|---|---|
Key<D> |
SmartString |
Human-readable keys with validation |
Id<D> |
NonZeroU64 |
Numeric database IDs (8 bytes, Copy) |
Uuid<D> |
uuid::Uuid |
UUID identifiers (16 bytes, Copy, feature uuid) |
use *;
// Numeric IDs — one macro does it all
define_id!;
let id = new.unwrap;
assert_eq!;
// UUID identifiers (requires `uuid` feature)
// define_uuid!(OrderUuidDomain => OrderUuid);
// let uuid = OrderUuid::v4();
All three types are domain-typed: UserId and OrderId are incompatible at compile time even though both wrap a NonZeroU64.
Performance Features
Feature-Based Optimization Profiles
# Maximum performance (modern CPUs with AES-NI)
= ["fast"]
# DoS protection + good performance
= ["secure"]
# Cryptographic security
= ["crypto"]
# All optimizations enabled
= ["fast", "std", "serde"]
Build for Maximum Performance
# Enable CPU-specific optimizations
RUSTFLAGS="-C target-cpu=native"
# For Apple Silicon Macs
RUSTFLAGS="-C target-cpu=native -C target-feature=+aes,+neon"
Performance Improvements
| Operation | Standard | Optimized | Improvement |
|---|---|---|---|
| Key Creation (short) | 100ns | 72ns | 28% faster |
| String Operations | 100% baseline | 175% | 75% faster |
| Hash Operations | 25ns | 15ns | 40% faster |
| Length Access | O(n) | O(1) | Constant time |
| Collection Lookup | 35ns | 21ns | 40% faster |
Advanced Examples
E-commerce Domain
use ;
;
;
type ProductKey = ;
type CartKey = ;
// Use in your application
let product = new?;
let cart = from_parts?;
# Ok::
Multi-tenant SaaS
use ;
use Cow;
;
type TenantKey = ;
let tenant = new?;
assert_eq!; // normalized
# Ok::
Advanced Validation
use ;
;
type EmailKey = ;
let email = new?;
assert_eq!;
// This will fail validation
let invalid = new;
assert!;
# Ok::
Feature Flags Reference
Hash Algorithm Features (choose one for best results)
fast- GxHash (40% faster, requires modern CPU with AES-NI)secure- AHash (DoS protection, balanced performance)crypto- Blake3 (cryptographically secure)- Default - Standard hasher (good compatibility)
Core Features
std- Standard library support (enabled by default)serde- Serialization support (enabled by default)no_std- No standard library supportuuid- Typed UUID identifiers (Uuid<D>)uuid-v4- UUID v4 random generationuuid-v7- UUID v7 time-ordered generation
Security Considerations
domain-key provides multiple levels of security depending on your needs:
- DoS Protection: Use
securefeature for AHash with DoS resistance - Cryptographic Security: Use
cryptofeature for Blake3 cryptographic hashing - Input Validation: Comprehensive validation pipeline with custom rules
- Type Safety: Compile-time prevention of key type mixing
- Memory Safety: Rust's ownership system + additional optimizations
See SECURITY.md for detailed security information.
Documentation
- User Guide - Comprehensive usage guide
- API Documentation - Complete API reference
- Examples - Real-world usage examples
- Migration Guide - Migrating from string keys
- Performance Guide - Optimization strategies
- Security Policy - Security considerations and reporting
Testing
Run the comprehensive test suite:
# All tests with all features
# Property-based tests
# Benchmarks
# Security audit
Benchmarks
# Run realistic benchmarks
# Memory usage analysis
# Cross-platform performance
Migration from String Keys
Before (String-based)
let user_id: String = "user_123".to_string;
let order_id: String = "order_456".to_string;
// Dangerous - no compile-time protection!
if user_id == order_id
let cache_key = format!;
After (domain-key)
type UserKey = ;
type OrderKey = ;
type CacheKey = ;
let user_id = new?;
let order_id = new?;
// This won't compile - type safety!
// if user_id == order_id { } // Compile error!
let cache_key = from_parts?;
# Ok::
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
Quick Development Setup
# Install development dependencies
# Run tests
Platform Support
| Platform | Status | Hash Algorithm | Notes |
|---|---|---|---|
| Linux x86_64 | Full | GxHash/AHash | Best performance with AES-NI |
| Windows x86_64 | Full | GxHash/AHash | Full feature support |
| macOS Intel | Full | GxHash/AHash | All features supported |
| macOS Apple Silicon | Full | GxHash/AHash | Requires explicit AES+NEON flags |
| WebAssembly | Core | DefaultHasher | no_std support |
| ARM64 Linux | Full | GxHash/AHash | Server deployments |
| ARM Embedded | Core | FNV-1a | no_std + no_alloc |
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Inspired by Domain-Driven Design principles by Eric Evans
- Built on the excellent
smartstringcrate for memory efficiency - Performance-focused hash algorithms from the Rust ecosystem:
Project Stats
- Lines of Code: ~3,000 (including comprehensive tests)
- Test Coverage: >95%
- Documentation Coverage: >98%
- Benchmark Coverage: 20+ realistic scenarios
- no_std Support: Yes
- MSRV: Rust 1.75+
- Platforms: 7+ supported targets
domain-key - Because your keys should know their place in your domain!