#inline #content #relative #file #workspace #duplicating #embarrassed

macro inline-fn

Inline the content of a function without duplicating code. Useful when duplicating code is desired but you are too embarrassed to do it manually.

1 unstable release

new 0.0.1 Mar 11, 2025

#38 in #relative

Download history 60/week @ 2025-03-06

60 downloads per month

MIT/Apache

18KB
435 lines

inline-fn

Inline the content of a function without duplicating code. Useful when duplicating code is desired but you are too embarrassed to do it manually.

Usage

You have a function that you want to inline

// src/lib.rs
fn world() -> String {
    "world".to_string()
}

Use the inline-fn crate to inline the content of a function without duplicating code.

// Option 1: Specify the path to the file containing the function
// relative to the workspace root.
let greetings = format!("Hello, {}", inline_fn!(world, "src/lib.rs"));

// Option 2: Recursively search all files in the workspace root.
// This has an impact on performance.
let greetings = format!("Hello, {}", inline_fn!(world));

Option 2 has an impact on performance until rust-lang#54725 is implemented

Why

It's particularly useful for tests where one test is built on top of another.

#[test]
fn payment_works() {
    let payment = payment(100);
    assert_eq!(payment.value, 100);
    // (very long function body)
}

#[test]
fn refund_works() {
    inline_fn!(payment_works, "src/lib.rs"); // expands to the content of the previous fn

    let refund = refund(payment.id);
    assert_eq!(refund.value, payment.value);
}

Dependencies

~6–14MB
~174K SLoC