Crate hft_jobs

Crate hft_jobs 

Source
Expand description

A lightweight, allocation-free job system for executing type-erased closures.

This crate provides the Job type, which stores closures inline inside a fixed-size buffer and executes them without heap allocation. Each job embeds its own call, clone, and drop functions via type-erased function pointers, enabling predictable, low-latency execution suitable for high-frequency or real-time workloads.

Job is generic over the inline capacity N, the closure’s return type R, and an optional context type C. The inline storage size N has no default; callers must choose a capacity explicitly. R defaults to (), and C defaults to ().

§Example: Dispatching Jobs to a Worker Thread

use std::sync::mpsc;
use std::thread;
use hft_jobs::Job;

// Create a channel for sending jobs.
let (tx, rx) = mpsc::channel::<Job<24, String>>();

// A worker thread that receives and runs jobs.
thread::spawn(move || {
    while let Ok(job) = rx.recv() {
        let s = job.run();
        println!("{}", s);
    }
});

// Send a simple job.
let job = Job::<24, _>::new(|| "Hello from a job!".to_string());
tx.send(job).unwrap();

// A convenience macro that enqueues a logging job.
macro_rules! log {
    ($tx:expr, $fmt:literal $(, $arg:expr)* $(,)?) => {{
        let job = Job::<24, String>::new(move || {
            format!($fmt $(, $arg)*)
        });
        let _ = $tx.send(job);
    }};
}

// Use the `log!` macro to enqueue a println job.
log!(tx, "Logging from thread: {}", 42);

This model provides a minimal, fast job runtime suitable for embedded systems, schedulers, executors, or lightweight logging systems.

Structs§

Job
A type-erased, fixed-size job container for storing and invoking closures.