[][src]Attribute Macro mula_proc_macro::mula

#[mula]

Makes the function run only once at a time for a given input. Any calls made with the same argument after the first will block and wait for it to finish and get the result once it is available.

Functions can only be annotated with this macro if they take exactly one argument and return exactly one value. This limitation may be lifted in the future.

Both the input and output types must implement Clone and Send, and the function itself needs to be Sync, since it will be executed by a separate thread, potentially multiple threads if several different inputs are given.

Example

The following example will only run the computation closure twice, once for each of the distinct inputs. The two function calls for "burro" will both be serviced by the same computation.

use mula::mula;

#[mula]
fn delayed_uppercase(input: &'static str) -> String {
    std::thread::sleep(std::time::Duration::from_secs(2));
    input.to_uppercase()
}

let thread1 = std::thread::spawn(move || {
    let upper = delayed_uppercase("mula");
    assert_eq!(upper, "MULA".to_string());
});

let thread2 = std::thread::spawn(move || {
    let upper = delayed_uppercase("burro");
    assert_eq!(upper, "BURRO".to_string());
});

let thread3 = std::thread::spawn(move || {
    let upper = delayed_uppercase("burro");
    assert_eq!(upper, "BURRO".to_string());
});

thread1.join();
thread2.join();
thread3.join();