https://github.com/dominicburkart/funfun
Macros for working with closures in Rust.
https://github.com/dominicburkart/funfun
functional-programming memory-allocation parallel rust
Last synced: 5 months ago
JSON representation
Macros for working with closures in Rust.
- Host: GitHub
- URL: https://github.com/dominicburkart/funfun
- Owner: DominicBurkart
- Created: 2018-08-11T18:19:30.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-08-29T21:29:21.000Z (over 7 years ago)
- Last Synced: 2025-01-11T08:48:15.988Z (about 1 year ago)
- Topics: functional-programming, memory-allocation, parallel, rust
- Language: Rust
- Homepage:
- Size: 30.3 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# funfun
[](https://travis-ci.org/DominicBurkart/funfun)
[](https://coveralls.io/github/DominicBurkart/funfun?branch=master)
[](https://codecov.io/gh/DominicBurkart/funfun)
[](https://crates.io/crates/funfun)
[](https://docs.rs/funfun)
### 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.
``` rust
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();
}
```
### box_fn!
```box_fn!``` Boxes a closure and returns an Rc pointer.
``` rust
type T = BoxFn 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()}
);
```
### arc_fn!
```arc_fn!``` Boxes a closure and returns an Arc pointer. Slower than
an Rc pointer, but allows derivation of traits like Clone.
``` rust
type T = ArcFn String>;
#[derive(Clone)]
struct F {
c: T
}
let c: T = arc_fn!(|s: &str| -> String {s.to_string()});
let mut f = F { c };
f.c = arc_fn!(
|d: &str| -> String {"reassign once".to_string()}
);
f.c = arc_fn!(
|_: &str| {"and again".to_string()}
);
```