funfun 0.2.2

Macros for working with closures.
Documentation

funfun

Build Status Coverage Status Codecov Coverage Status Crates.io

spawn_fn!

spawn_fn! Takes a closure or function and its arguments, runs the closure or function with the passed arguments in a new thread, and returns the thread's hook.

box_fn!

box_fn! Boxes a closure.

Usage:


// use of boxed functions in structs

type T = BoxFn<Fn(&str) -> String>;

struct F {
    c: T
}

let c: T = box_fn!(|s: &str| -> String {s.to_string()});

let mut f = F { c };

f.c = box_fn!(
    |d: &str| -> String {"reassign once".to_string()}
);

f.c = box_fn!(
    |_: &str| {"and again".to_string()}
);


// multithreading with spawn_fn!

let eg = box_fn!(|x: i32| -> i32 {x + 2});
let also = box_fn!(|x: i32, y: i32| -> i32 {x + y});

let mut v1 = Vec::new();
for i1 in 0..10000 {
    let i2 = i1 + 10;
    v1.push(spawn_fn!(eg, i1));
    v1.push(spawn_fn!(also, i1, i2)); // woohoo multi-arity!
}
v1.push(spawn_fn!(||{println!("accepts closures to run in their own thread!"); 1}));

for res in v1.into_iter() {
    res.join();
}