Skip to main content

Crate hamon

Crate hamon 

Source
Expand description

§Hamon (刃文) - Zero-Cost Static Decorators

A high-performance library that enables composing data processing pipelines resolved entirely at compile time through Rust’s type system.

§Philosophy

Traditional decorator patterns sacrifice performance for modularity through dynamic dispatch. Hamon achieves both by leveraging monomorphization - the compiler generates specialized machine code for each pipeline, eliminating indirection while preserving clean, composable APIs.

§Performance Advantages

  • Compile Time: Generic recursion builds type-level pipeline structure
  • Runtime: Direct function calls with zero abstraction overhead
  • Memory: Stack-first design avoids heap fragmentation

§Primary Model

The Decorator<T, O> trait represents a fallible transformation edge in the pipeline. Through recursive generics, complex chains become nested types that LLVM can optimize into flat, efficient assembly.

It’s noteworthy the crate relies on the lazy computation which also means that any operations are stacked up until it’s consumed by the caller. The flexibility this model equips you with deferred logic processing requests so as to the latter emergence can augment the former computations by discarding unnecessary steps.

§Common Usage

Delve into practice, when the output requires a trivial steps of transformation (pipeline) given each is considered distinct. In that case, each should define a respective logic for its own flavor then add up to the final outcome.

use hamon::errors::Result;
use hamon::prelude::*;
// A decorator that adds up an amout to the current value (as i32)
struct Add(i32);

// A decorator that multiplies up an amout to the current value (as i32)
struct Multiply(i32);

// A decorator converts the integer value to String value
struct ToString;

impl Decorator<i32, i32> for Add {
    fn produce(&mut self, input: i32) -> Result<i32> {
        println!("{:<10}: previous value was {}", "[ADD]", input);
        Ok(self.0 + input)
    }
}

impl Decorator<i32, i32> for Multiply {
    fn produce(&mut self, input: i32) -> Result<i32> {
        println!("{:<10}: previous value was {}", "[MULTIPLY]", input);
        Ok(self.0 * input)
    }
}

impl Decorator<i32, String> for ToString {
    fn produce(&mut self, input: i32) -> Result<String> {
        println!("{:<10}: previous value was {}", "[TOSTRING]", input);
        Ok(format!("{}", input))
    }
}

fn main() {
    let engine = Builder::new(10)
        .step(Add(2))
        .step(Multiply(10))
        .step(ToString);

    println!("{:<10}: {}", "[DEPTH]", engine.get_index());
    println!("{:<10}: {:?}", "[FINAL]", engine.collect());
}

Modules§

builder
Dedicated to collecting the pipelines output
errors
Custom error handling for pipeline processing.
ext
Extensions for the customizability on usage of Step.
step
The representation of the Pipeline in system.

Macros§

hamon
Consitute an error that conforms with current system error handling.

Traits§

Collector
This trait imposes the standard behavior which Steps should follow.
Decorator
A trait for types that can transform an input T into an output O.