inline-fn

Crates.ioinline-fn
lib.rsinline-fn
version0.0.1
created_at2025-03-11 21:52:57.605561+00
updated_at2025-03-11 21:52:57.605561+00
descriptionInline the content of a function without duplicating code. Useful when duplicating code is desired but you are too embarrassed to do it manually.
homepage
repositoryhttps://github.com/mustakimali/inline-fn
max_upload_size
id1588659
size27,479
Mohammad Mustakim Ali (mustakimali)

documentation

https://docs.rs/inline-fn/

README

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);
}
Commit count: 0

cargo fmt