2 releases
| new 0.1.1 | Apr 18, 2026 |
|---|---|
| 0.1.0 | Apr 16, 2026 |
#1055 in Rust patterns
12KB
118 lines
Hamon (刃文)
Hamon is a zero-cost, type-level static decorator library for Rust. It allows you to compose complex data processing pipelines that are resolved entirely at compile time, eliminating the performance overhead of dynamic dispatch and heap allocation.
⚔️ The Philosophy
In high-performance systems programming, every instruction counts. Traditional decorator patterns often rely on Box<dyn Decorator>, which incurs a "vtable tax"—the performance penalty of pointer chasing and inhibited compiler optimizations.
Hamon leverages Monomorphization. By using recursive generics, the entire execution chain is baked into the type system. This allows the LLVM optimizer to "see through" the abstraction, inlining logic and generating machine code that is as fast as a hand-written monolithic function.
🚀 Outstanding Features
- Zero-Cost Abstractions: No dynamic dispatch. Direct function calls are resolved at compile time.
- Type-Level Validation: The compiler verifies your pipeline. If a decorator doesn't fit the lineage, it won't compile.
- Stack-First Efficiency: Avoids heap fragmentation. Data remains in CPU registers and the stack for maximum throughput.
- Modular Maintainability: Break 500-line "spaghetti" functions into small, isolated, and testable
Decoratorstructs without losing performance.
📊 Performance Dojo
Benchmarks comparing a 50-step pipeline across three implementation strategies:
| Implementation | Average Time | Memory Allocations | Speed Delta |
|---|---|---|---|
| Monolithic Function | 4.36 µs | 1 (Final Result) | — |
| Hamon (Static) | 4.98 µs | 1 (Final Result) | Baseline |
| Traditional (Dynamic) | 10.14 µs | 51 (Boxes + Vec) | ~2.0x Slower |
Hamon provides the modularity of a dynamic system with the raw speed of hand-optimized code.
🛠️ Usage
Hamon uses a conservative, familiar API based on the Builder and Decorator patterns.
use hamon::prelude::*;
// 1. Define your logic units
struct Sanitize;
impl Decorator for Sanitize {
fn produce(&self) -> String {
"sanitized_data".to_string()
}
}
struct Logger { id: u32 }
impl Decorator for Logger {
fn produce(&self) -> String {
format!("Log ID: {}", self.id)
}
}
fn main() {
// 2. Construct the pipeline (Resolved at compile time)
let pipeline = Builder::new()
.add(Sanitize)
.add(Logger { id: 101 });
// 3. Execute the chain
let results = pipeline.build();
}
Dependencies
~110–480KB
~11K SLoC