#macro #arguments #context #shared #typed

magic-params

Macro to call functions with typed arguments derived from a shared context

6 releases

Uses new Rust 2024

0.1.1 Oct 9, 2025
0.1.0 Oct 8, 2025
0.0.4 Aug 31, 2025

#1402 in Rust patterns


Used in plyne

BSD-3-Clause

5KB
60 lines

Magic Params

call functions with typed arguments derived from a shared context.

Reference: axum-style-magic-function-param

Usage

cargo add magic-params

Examples

define_context!(MyContext {
    value1: i32,
    value2: u32,
    value3: String,
});
context_as_params!(MyContext, 3);

fn main() {
    let ctx = MyContext {
        value1: 42,
        value2: 100,
        value3: "Hello".to_string(),
    };

    let handler = |v1: i32, v2: u32, v3: &String| {
        assert_eq!(v1, 42);
        assert_eq!(v2, 100);
        assert_eq!(v3, "Hello");
        "Handler called successfully"
    };

    assert_eq!(handler.call(&ctx), "Handler called successfully");
}

Dependencies

~6KB