hamon 0.1.1

A zero-cost, type-level static decorator and pipeline builder.
Documentation
  • Coverage
  • 65.22%
    15 out of 23 items documented1 out of 20 items with examples
  • Size
  • Source code size: 31.89 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.52 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 52s Average build duration of successful builds.
  • all releases: 1m 16s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • TonyNg147

Hamon (刃文)

License: MIT

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 Decorator structs 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();
}